2

I am working in an app that manages wifi connections, I have the problem that in android 6.0 the wifi scanning is off when the location services are turned off by the user.

What I want is to know if there is an action to which I can subscribe to listen when the user turns off and on the location services. I have searched in Android documentation and there are two possible actions, but I am not sure which one to use.

The actions are

android.location.PROVIDERS_CHANGED

android.location.MODE_CHANGED

The Documentation is not helping me.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
acabezas
  • 731
  • 1
  • 7
  • 20
  • Possible duplicate of [Get GPS Location in a Broadcast Receiver/or Service to Broadcast Receiver data transfer](http://stackoverflow.com/questions/7709030/get-gps-location-in-a-broadcast-receiver-or-service-to-broadcast-receiver-data-t) – Zahid Rasheed Mar 01 '17 at 12:25
  • Possible duplicate of [How to? Listen for Location Setting being turned ON (Android App)](http://stackoverflow.com/questions/36325088/how-to-listen-for-location-setting-being-turned-on-android-app) – maciekjanusz Mar 01 '17 at 12:35

2 Answers2

0

Manifest:

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


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

And then....

   public class ObserveGpsState extends BroadcastReceiver {        
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().matches(LocationManager.PROVIDERS_CHANGED_ACTION)) {
               ......... 
            }
        }
    }
Zahid Rasheed
  • 1,536
  • 1
  • 10
  • 28
0

This is useful when user want to trigger any action on turn On/Off location provides

You should add this action in manifest

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

and after add this action you can trigger your broadcast receiver

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

And in your BroadcastReceiver class you have to implement LocationListener in that class which is given following below..

  public class GpsLocationReceiver extends BroadcastReceiver {        
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().matches("android.location.PROVIDERS_CHANGED")) {
                Toast.makeText(context, "in android.location.PROVIDERS_CHANGED",
                Toast.LENGTH_SHORT).show();
           LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    if( !locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) ) {
       //NO GPS
    }else{
//Gps is on
     }
            }
        }
    }
Rissmon Suresh
  • 13,173
  • 5
  • 29
  • 38