Location service onProviderEnabled never called
I have a service to update location in my app. When I start my app with GPS disable, go back to android menu and enable GPS, and finally go back to my app (the service has not been destroyed), onProviderEnabled is never called. Anybody could help?
UPDATE: if I restarts the app the provider is enabled. Only onProviderEnabled is not called...
In every activity in which I need location I do
super.onCreate(savedInstanceState);
//....
// Bind location service
bindService(new Intent(this, LocationService.class), mConnection, Context.BIND_AUTO_CREATE);
//....
and
@Override
protected void onDestroy() {
super.onDestroy();
// Unbind LocationService
ItemDetail.this.unbindService(mConnection);
}
and the service is
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);
}
}
In your LocationService.onCreate()
method your code checks if the GPS provider is disabled.
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){...}
But you only start the subscription if it is already enabled. Note that your listener is then never registered with the LocationManager
and will not receive any updates of location providers being disabled or enabled. Rather change your onCreate()
method to always call
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5 * 60 * 1000, 100, this);
精彩评论