简体   繁体   中英

Detecting location settings change from quick settings

I'm currently developing an app that requires the Android's location service to always be active.

Right now I'm able to detect if the user activates or deactivates the location service when he resumes the app (in the onResume() event ).

The problem is that, if the user deactivates the location service throught the quick settings menu, I am not able to detect the event.

How can I know when the user activates / deactivates the device's location through the quick settings?

Thank you.

My code:

...

BroadcastReceiver mGpsSwitchStateReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction() != null && intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
            // Make an action or refresh an already managed state.
            Log.d("vtApp", "CHANGED");
        }
    }
};

currentActivity.registerReceiver(mGpsSwitchStateReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));

References I followed:

How to trigger broadcast receiver when gps is turn on/off?

Android Quick Settings notifications?

Changing Location Settings

I had the same problem. this worked for me

intentFilter.addAction(android.location.LocationManager.PROVIDERS_CHANGED_ACTION);

not to confuse it with

Intent.ACTION_PROVIDER_CHANGED

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM