2

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

Luís Henriques
  • 604
  • 1
  • 10
  • 30

1 Answers1

3

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
user -1
  • 110
  • 8
  • It works perfectly. Thank you. Sorry for taking so long to answer, but at that moment I decided to leave that problem for later. Now I finally found some time to tackle it. – Luís Henriques May 29 '20 at 12:52
  • Do you happen to know how to identify when the user disconnects from the internet, whether through deactivating his wifi or through his mobile data? – Luís Henriques May 29 '20 at 13:00
  • 2
    @LuísHenriques not through intent filters sorry. but you can use `ConnectivityManager.getActiveNetworkInfo` to check if internet is available or not. I hope that helps – user -1 May 30 '20 at 22:35