Listen to location access disable/enable in settings
Asked Answered
D

4

8

In Android OS, in "Settings" --> "Location services", there is a toggle button named "Access to my location" which can be used to disable & enable location info access from apps.

Currently, I am developing a location service application. I am wondering, how can I listen to this setting in my Android project? Is there any broadcast receiver I can use to know right away when user disable or enable "Access to my location" ?

If there isn't any broadcast receiver for it, how can I listen to this change in my Android project?

Dibasic answered 28/11, 2013 at 12:16 Comment(2)
@ GrlsHu, no, I don't want to do that. The key point of my question is how to listen to this location access setting.Dibasic
https://mcmap.net/q/366418/-how-to-trigger-broadcast-receiver-when-gps-is-turn-on-off and https://mcmap.net/q/1469652/-receive-a-notification-via-broadcastreceiver-when-gps-or-location-services-is-started-stopped will helpThapsus
A
6

This works for me:

Add receiver to the Manifest file:

<receiver
        android:name="com.eegeo.location.LocationProviderChangedReceiver">
        <intent-filter>
            <action android:name="android.location.PROVIDERS_CHANGED" />
        </intent-filter>
    </receiver>

Check both location providers in receiver:

public class LocationProviderChangedReceiver  extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {

        boolean anyLocationProv = false;
        LocationManager locationManager = (LocationManager) MyMainActivity.context.getSystemService(Context.LOCATION_SERVICE);

        anyLocationProv |= locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        anyLocationProv |=  locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        Log.i("", "Location service status" + anyLocationProv);


    }

}

Though this receiver is called more than once due to obvious reasons, but this will tell you the status.

Algebraist answered 11/3, 2015 at 7:9 Comment(2)
The manifest broadcast receiver should be used only if the app needs location in the background. It should use a runtime broadcast receiver otherwiseBigod
This won't work if your app targets Android 8.0 (API level 26) or above: Registering broadcast receivers for implicit broadcasts in the manifest has been disabledPruchno
H
1

You can do it much simpler this way. In your BroadcastReceiver's onReceive() method:

    ContentResolver contentResolver = getContext().getContentResolver();
    // Find out what the settings say about which providers are enabled
    int mode = Settings.Secure.getInt(
            contentResolver, Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);

    if (mode == Settings.Secure.LOCATION_MODE_OFF) {
        // Location is turned OFF!
    } else {
        // Location is turned ON!
    }

~Thanks to this code: LocationManagerTest.java

Huh answered 20/3, 2015 at 20:44 Comment(2)
To all: this was valid then. Today, all of this is deprecated.Herta
@Herta what's the current solution?Huh
S
0

You can use the below code to check whether Location Service Enable or not

 LocationManager lm = null;
 boolean gps_enabled,network_enabled;
    if(lm==null)
        lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    try{
    gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }catch(Exception ex){}
    try{
    network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    }catch(Exception ex){}

   if(!gps_enabled && !network_enabled){
        dialog = new AlertDialog.Builder(context);
        dialog.setMessage(context.getResources().getString(R.string.gps_network_not_enabled));
        dialog.setPositiveButton(context.getResources().getString(R.string.open_location_settings), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                // TODO Auto-generated method stub
                Intent myIntent = new Intent( Settings.ACTION_SECURITY_SETTINGS );
                context.startActivity(myIntent);
                //get gps
            }
        });
        dialog.setNegativeButton(context.getString(R.string.Cancel), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                // TODO Auto-generated method stub

            }
        });
        dialog.show();

    }
Scarbrough answered 28/11, 2013 at 12:18 Comment(2)
Hi, my question is not about how to check the value, but about how to listen to the change, meaning that how to get a notification in code that user has changed this location access in settings & then get the changed value.Dibasic
@GrIsHu That is incorrect. Have you tried usman's solution below?Huh
H
0

2023: This solution listens to, and reacts, when user toggles the location in curtain menu. The hard part was figuring out which "intent" filter flag to use (LocationManager.PROVIDERS_CHANGED_ACTION).

In my example I use @Inject constructor (Hilt). If you don't, then just send the context as parameter to register() and unregister(): register(context: Context) etc.

An extension on Context:

fun Context.isLocationEnabled(): Boolean {
  val locationManager = this.getSystemService(Context.LOCATION_SERVICE) as LocationManager
  return LocationManagerCompat.isLocationEnabled(locationManager)
}

Create a BroadcastReceiver:

class LocationBroadcastReceiver @Inject constructor(private val context: Context) : BroadcastReceiver() {

  private val locationStateChange: BehaviorRelay<Boolean> = BehaviorRelay.createDefault(context.isLocationEnabled())

  override fun onReceive(context: Context, intent: Intent) {
    val action = intent.action

    if (action == LocationManager.PROVIDERS_CHANGED_ACTION) {
      val state = context.isLocationEnabled()
      locationStateChange.accept(state)
    }
  }

  fun subscribeState(): Flowable<Boolean> {
    return locationStateChange.toFlowable(BackpressureStrategy.LATEST)
  }

  /**
  * Listen to subscribeState() to receive updates
  */
  fun register() {
    val filter = IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION)
    val receiverFlags = ContextCompat.RECEIVER_NOT_EXPORTED
    ContextCompat.registerReceiver(context, this, filter, receiverFlags)
  }

  fun unregister() {
    context.unregisterReceiver(this)
  }

}

Now you can subscribe to

subscribeState()

from your viewModel or compose class and do what you must.

Herta answered 14/4, 2023 at 14:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.