Is it possible to get a notification when any location provider is enabled/disabled and ascertain what action occurred?

旧街凉风 提交于 2019-12-04 02:18:29
saschoar

I hope that this doesn't contradict your requirement

I know that I could keep the state of each provider and then when I receive notification that they have changed then I could work out what has changed, I am looking for a more "standard" method of doing this.

...but I would think the best, "standard", and most flexible way of doing this is to let your LocationProviderChangedReceiver implement the LocationListener interface and then implement onProviderEnabled() and onProviderDisabled() like so:

public void onProviderEnabled(String provider) {
  if(provider.equals(LocationManager.GPS_PROVIDER)){
    ...
  } else if (provider.equals(LocationManager.NETWORK_PROVIDER)){
    ...
  }
}

Note that you should add the ACCESS_FINE_LOCATION permission to your manifest if you haven't already. Also, other providers (beyond "Network" and "GPS") may apply depending on the context, like PASSIVE_PROVIDER or even FUSED_PROVIDER.

Updated answer:
If constantly listening for location changes is no option for you, the LocationManager also knows which provider is currently enabled:

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

...so that you could use these checks together with your BroadcastReceiver to avoid manually holding any enabled/disabled flags. Beyond that, there is an intent called android.location.GPS_ENABLED_CHANGE which you could try to receive (and process) as well. But since this intent is hidden in the official documentation, it might not be safe to use it, as mentioned in this answer.

Even another approach:
Android's default settings toggle widget (you know what I mean) displays the GPS enabled/disabled state by subscribing to the android.location.PROVIDERS_CHANGED intent (like you do) to trigger a request of the GPS state via the user settings:

@Override
public int getActualState(Context context) {
  ContentResolver resolver = context.getContentResolver();
  boolean on = Settings.Secure.isLocationProviderEnabled(
                 resolver, LocationManager.GPS_PROVIDER);
  return on ? STATE_ENABLED : STATE_DISABLED;
}

(from the official sources).
You could adapt this approach for other location providers as well.

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.

Thanks to @usman 's answer we can apply the solution without extends the class by the following way;

AndroidManifest.xml;

<receiver android:name="com.eegeo.location.LocationProviderChangedReceiver">
    <intent-filter>
        <action android:name="android.location.PROVIDERS_CHANGED" />
    </intent-filter>
</receiver>

class (I never tested);

public class LocationProviderChangedReceiver {

    private mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String ability = (isGPSProviderEnabled() && isNetworkProviderEnabled()) ? "CAN" : "CANNOT";
            Log.d("", "The location " + ability + " be caught in high accuracy!");
        }
    };


    public startProviderChangeObserving () {
        registerReceiver(mReceiver, new IntentFilter(LocationManager.PROVIDERS_CHANGED_ACTION));

    }


    public stopProviderChangeObserving () {
        unregisterReceiver(mReceiver);
    }   


    /**
    * Method to verify that the location providers wheter on or not on the device
    **/
    public boolean isGPSProviderEnabled() {
        return (Boolean) mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    }

    public boolean isNetworkProviderEnabled() {
        return (Boolean) mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    }

}

On the Receiver you could simply check which providers are enabled:

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE)
List<String> providers = lm.getProviders(true);

In this way you obtain all the enable providers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!