Detecting GPS on/off switch in Android phones
Asked Answered
M

4

40

I would like to detect when the user changes the GPS settings on or off for an Android phone. Meaning when user switches GPS sattelite on/off or detection via access points etc.

Marven answered 16/6, 2011 at 7:7 Comment(1)
+1, 5 marked as favorite question and only 1 upvoted?!Clarissaclarisse
M
43

As I have found out the best way to do this is to attach to the

<action android:name="android.location.PROVIDERS_CHANGED" />

intent.

For instance:

<receiver android:name=".gps.GpsLocationReceiver">
        <intent-filter>
            <action android:name="android.location.PROVIDERS_CHANGED" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

And then in the code:

public class GpsLocationReceiver extends BroadcastReceiver implements LocationListener 
...

@Override
public void onReceive(Context context, Intent intent)
{
        if (intent.getAction().matches("android.location.PROVIDERS_CHANGED"))
        { 
            // react on GPS provider change action 
        }
}
Marven answered 21/6, 2011 at 13:23 Comment(8)
Works not only for GPS but also for network location services.Revers
Upvoted, but why do you need <category android:name="android.intent.category.DEFAULT" />?Oxidation
4 years ago it was needed, I think ... who would know. Try it without if it works so much better.Marven
for what purpose you are using .matches() method instead of equals()/equalsIgnoreCase()???Doubleripper
Probably it was copy pasted from some working code where .matches() was appropriate. In this case .equals() is better. But both will work. Hey it's 4+ years since then ... who would now.Marven
you don't need implements LocationListenerVintner
@Vintner this question is 7 years old ... in 2011 you had toMarven
For API level 26 and above, this solution doesn't work due to restrictions in implicit broadcast. It fails with "BroadcastQueue: Background execution not allowed: receiving Intent { act=android.location.PROVIDERS_CHANGED } " Read More at: developer.android.com/guide/components/broadcast-exceptions and developer.android.com/about/versions/oreo/backgroundManipulator
P
21

Here is a code sample for a BroadcastReceiver detecting GPS location ON/OFF events:

private BroadcastReceiver locationSwitchStateReceiver = new BroadcastReceiver() {

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

            if (LocationManager.PROVIDERS_CHANGED_ACTION.equals(intent.getAction())) {

                LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
                boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
                boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

                if (isGpsEnabled || isNetworkEnabled) {
                    //location is enabled
                } else {
                    //location is disabled
                }
            }
        }
    };

Instead of changing your manifest file, you can register your BroadcastReceiver dynamically:

IntentFilter filter = new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION);
filter.addAction(Intent.ACTION_PROVIDER_CHANGED);
mActivity.registerReceiver(locationSwitchStateReceiver, filter);

Don't forget to unregister the receiver in your onPause() method:

mActivity.unregisterReceiver(locationSwitchStateReceiver);
Phonemic answered 8/3, 2019 at 9:33 Comment(2)
It works. But why onReceive comes multiple times?Apicella
Different actions maybe. What it the value of intent.getAction() when onReceive is called ?Phonemic
M
4

Try this,

try {
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

            Log.i("About GPS", "GPS is Enabled in your devide");
        } else {
            //showAlert
        }
Mercury answered 16/6, 2011 at 7:15 Comment(1)
I know how to detect if a provider is on or off. But I would like to detect the change, when the user switches a provider on or off in a setting dialog.Marven
B
3

Impement android.location.LocationListener, there you have two functions

public void onProviderEnabled(String provider);
public void onProviderDisabled(String provider);

Using this you can find out when the requested provider is turned on or off

Bonedry answered 16/6, 2011 at 8:42 Comment(2)
This should be the accepted answer. The accepted answer is already using LocationListener, so why not use the given methods for the task.Bailable
You can detect on/off state but you can't detect the change (switch)Marven

© 2022 - 2024 — McMap. All rights reserved.