3

I'm trying to register a BroadcastReceiver for get the state changes of Location settings

Location settings

But I didn't find any documentation for doing it. I only found BroadcastReceiver to check the status of certain search providers (GPS and Network providers); but not for checking if this particular option (Location) is active or not in the system preferences.

Somebody can show me the right direction?


NOTE:

I used Google Play Location Service (com.google.android.gms.location.LocationListener interface).

Community
  • 1
  • 1
tomloprod
  • 7,472
  • 6
  • 48
  • 66

2 Answers2

2

Well, I found one way to check if the "Location" setting of the action bar is enabled or disabled; but without a BroadcastReceiver (a shame, really)

private static final String TAG = getClass().getName();

/* We define the LocationManager */
LocationManager location_Manager= (LocationManager) getSystemService(LOCATION_SERVICE);

/* If the GPS provider or the network provider are enabled; the Location setting is enabled.*/
if(location_Manager.isProviderEnabled(LocationManager.GPS_PROVIDER) || location_Manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
    Log.d(TAG, "The Location setting is enabled");
}else{
   /* We send the user to the "Location activity" to enable the setting */
   Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
   startActivity(intent);
}

I really did not need a BroadcastReceiver; since the "arquitecture" that has my app allows me to do without it; but I would have liked to know how to use it.

NOTICE:

If someone finds the way to make it with a BroadcastReceiver I will change my correct answer by her answer.

tomloprod
  • 7,472
  • 6
  • 48
  • 66
1

You can try to use Location Listener.

onProviderDisabled(String provider)
Called when the provider is disabled by the user.

Compare to provider String such as
provider.equalsIgnoreCase(LocationManager.GPS_PROVIDER) http://developer.android.com/reference/android/location/LocationListener.html

Pham Hung
  • 308
  • 1
  • 4
  • 14
  • Hi, Pham Hung. I can't test your answer now, but I will try it in few hours and then, if it works, I will accept your answer. But looks ok, +1 – tomloprod Jun 30 '15 at 07:06
  • I tried to use the information in your answer, but, unfortunately, I used `import com.google.android.gms.location.LocationListener` in this project, so I haven't got access to the methods of `android.location.LocationListener` interface. – tomloprod Jun 30 '15 at 08:12