2011-06-23 44 views
1

我有一个服务来更新我的应用程序中的位置。当我用GPS禁用启动我的应用程序时,返回到android菜单并启用GPS,最后返回到我的应用程序(服务尚未销毁),onProviderEnabled从不被调用。任何人都可以帮忙?位置服务onProviderEnabled从来没有叫

更新:如果我重新启动应用程序提供程序启用。只有onProviderEnabled不叫......

在每一次活动中,我需要的位置我做

super.onCreate(savedInstanceState); 

    //.... 

    // Bind location service 
    bindService(new Intent(this, LocationService.class), mConnection, Context.BIND_AUTO_CREATE); 

    //.... 

@Override 
protected void onDestroy() { 
    super.onDestroy();  
    // Unbind LocationService 
    ItemDetail.this.unbindService(mConnection); 
} 

和服务

public class LocationService extends Service implements LocationListener { 
    LocationManager locationManager; 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() {   
     locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

     if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){   

      // Update after minimum 5 minutes and if user has moved at least 100 meters. 
      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5 * 60 * 1000, 100, this);   

      Location loc = getBestLocation(locationManager); 
      if(loc!=null){ 
       GlobalVars.lat = (Double) (loc.getLatitude()); 
       GlobalVars.lng = (Double) (loc.getLongitude()); 
      } 
     } 
    } 

    public void onLocationChanged(Location loc) {   
     GlobalVars.lat = (Double) (loc.getLatitude()); 
     GlobalVars.lng = (Double) (loc.getLongitude()); 
    } 

    public static Location getBestLocation(LocationManager locationManager) { 

     Location location_gps = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     Location location_network = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

     // If both are available, get the most recent 
     if(location_gps!=null && location_network !=null) { 
      return (location_gps.getTime() > location_network.getTime())?location_gps:location_network; 
     } 
     else if(location_gps==null && location_network ==null){ 
      return null; 
     } 
     else 
      return (location_gps==null)?location_network:location_gps; 

    } 

    public void onProviderEnabled(String s){ 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5 * 60 * 1000, 100, this); 
    } 
    public void onProviderDisabled(String s){ 
     locationManager.removeUpdates(this); 
     GlobalVars.lat = null; 
     GlobalVars.lng = null; 
    } 
    public void onStatusChanged(String s, int i, Bundle b){} 

    @Override 
    public void onDestroy() {  
     locationManager.removeUpdates(this); 
    } 
} 
+0

喜!你是否解决了这个问题?我正在寻找像你一样的东西 - 所以我很乐意将你的代码作为参考材料。 我仍然困惑如何使用位置管理器的服务来绕过运行到ANR ... – Chris

回答

2

在你LocationService.onCreate()您的代码检查GPS提供者是否被禁用的方法。

if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){

但你只有在已经启用了启动订阅。请注意,您的监听程序从未从LocationManager注册过,也不会收到任何正在解除或启用的位置提供程序的更新。而是改变你的onCreate方法来随时拨打

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5 * 60 * 1000, 100, this);