There are two location receivers in my Android app, one for fine and one for rough listening. I want to be able to detect when a user disables and disables their location services.
Whenever I turn on GPS phones or network location services, the onProviderDisabled method is called as expected. When I turn on GPS or network services, ONProviderEnabled is never called!
I put the code for one of the listeners below ....
Updating ...
I found that the problem is with registering listeners in onPause. When I remove the code "locationManager.removeUpdates (myFineLocationListener);" the onProviderEnabled () method is called.
Perhaps the GPS mute action (say) automatically mutes the listener? Perhaps I should unregister the listener in onDestroy, and not onPause () ???
PS. It may be appropriate to note that in my activity there is a button that goes directly to the device location settings page. It acts like a shortcut. I use this shortcut to disable / enable location services, and then use the back button to return to my application. I will put the code for this button at the very end of this post.
The code:
(The createLocListeners () method is called from onCreate ();)
void createLocListeners() { //if null, create a new listener //myFineLocationListener is a class variable if (myFineLocationListener == null) { //Listen for FINE location updates myFineLocationListener = new LocationListener(){ @Override public void onLocationChanged(Location location) { //Do something } @Override public void onProviderDisabled(String provider) { //This gets called when I change location settings (eg GPS on or off) on the phone } @Override public void onProviderEnabled(String provider) { //This DOES NOT get called when I change location settings (eg GPS on or off) on the phone } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } }; //end on location listener } //end if fine listener is null } //end createLocListener() function @Override protected void onResume() { super.onResume(); //REGISTER LOCATION LISTENERS try { locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, LISTENING_INTERVAL, LISTENING_DISTANCE, myFineLocationListener); } catch (IllegalArgumentException e){ //Tried catching exceptions but there weren't any } catch (RuntimeException e) { } }// end onResume() @Override protected void onPause() { super.onPause(); //UNREGISTER LOCATION LISTENERS locationManager.removeUpdates(myFineLocationListener); } //end onPause
Similar issue: The onProviderEnabled location service is never called
The code for the button that opens the location settings page ...
//JUMP TO LOCATION SERVICES SETTINGS ibLoc.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } }); //end jump to location services button listener