2010-10-27 44 views
0

朋友,如何获得更新的位置并在Android中获取它后禁用GPS?

我用下面的代码获取GPS位置

它造成了两个问题

1)当我的位置有所改变/我将我的手机从一个区域到另一个我不明白更新GPS位置(经度/纬度)

2)获得GPS位置后,我的GPS手机启用如何禁用它?

double MyDeviceLatitude,MyDeviceLongitude; 

public void GetGPSLocation() 
{ 

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

         LocationListener myLocationListener = new CurrentLocationListener(); 

         locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30, 30, myLocationListener); 

        Location currentLocation = locationManager.getLastKnownLocation("gps"); 

if(currentLocation != null) 
         { 

         MyDeviceLatitude = currentLocation.getLatitude(); 
         MyDeviceLongitude = currentLocation.getLongitude(); 
}else 
{ 

currentLocation = locationManager.getLastKnownLocation("network"); 
MyDeviceLatitude = currentLocation.getLatitude(); 
MyDeviceLongitude = currentLocation.getLongitude(); 



} 



public class CurrentLocationListener implements LocationListener{ 

      public void onLocationChanged(Location argLocation) 
      { 
       if (argLocation != null) 
       { 
        MyDeviceLatitude = argLocation.getLatitude(); 
        MyDeviceLongitude = argLocation.getLongitude(); 
       } 

      } 
      public void onProviderDisabled(String provider) { 
      } 

      public void onProviderEnabled(String provider) { 
      } 

      public void onStatusChanged(String provider, int status, Bundle arg2) { 
      } 
      } 

}

任何帮助,将不胜感激。

回答

0

UMMA做检查使用的广播接收机,多数民众赞成似乎解决您的问题Android: How to get location information from intent bundle extras when using LocationManager.requestLocationUpdates()

+0

例子是不完整的,所以我无法实现这个:( – UMAR 2010-10-29 07:46:41

+0

@UMMA你需要创建一个LocationReceiver(扩展广播接收器),这个类似的帖子并将其添加到链接中提到的清单中。请阅读有关如何将此BroadcastReceiver与您的活动/服务集成的指南,您可以将获得的位置传递给您的活动/服务。 – 100rabh 2010-10-29 08:39:22