Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen to location access disable/enable in settings

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?

like image 618
Mellon Avatar asked Nov 28 '13 12:11

Mellon


People also ask

How do I enable location access?

Open your phone's Settings app. Under "Personal," tap Location access. At the top of the screen, turn Access to my location on or off.

How do I change my location on Android Google?

Add, change, or delete a location On your Android phone or tablet, say "Hey Google, open Assistant settings." Or, go to Assistant settings. Your places. Add, change, or delete an address.


1 Answers

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.

like image 161
M. Usman Khan Avatar answered Sep 30 '22 13:09

M. Usman Khan