Wednesday, April 10, 2013

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


I wish to receive a notification when the user enables or disabled either Network or GPS locations, and importantly I wish to know which one they have changed and how. I have a broadcast receiver for the android.location.PROVIDERS_CHANGED broadcast intent and this is receiving the correct broadcast.


I now need to try and determine what action has occurred i.e. enable or disable and which provider has been changed. 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. The broadcast intent does not seem to have any extras to indicate which provider has changed.


This is the code I have currently.



public class LocationProviderChangedReceiver extends BroadcastReceiver {
private final static String TAG = "LocationProviderChangedReceiver";

@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().matches("android.location.PROVIDERS_CHANGED"))
{
Log.i(TAG,"Location Providers changed");
Bundle bundle = intent.getExtras();
if (bundle == null) {
Log.d(TAG, "No extras data");
} else {
Log.d(TAG, "Bundle received of size " + bundle.size);
}
}
}
}


And this is a small extract from my Manifest






android:name=".LocationProviderChangedReceiver"
android:exported="false">







Is anyone aware of any mechanism by which I can determine what state has changed without maintaining my own state variables.


In an ideal world I would monitor for changes constantly but only listen for location changes occasionally. I would like to avoid constantly monitoring for location changes.



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...