I'm planning to have a button in an activity which will start a service when clicked. However, GPS needs to be enabled for the service to do its work so I'd like to have the button disabled if GPS is disabled. Is there a way to get android to notify my activity when GPS is enabled/disabled so that I can enable/disable the button accordingly?
Asked
Active
Viewed 220 times
3 Answers
1
This link describes how to create a location listener: http://blog.doityourselfandroid.com/2010/12/25/understanding-locationlistener-android/
I've copied the important parts down below in case the site goes down in the future. The first step is to create a LocationListener:
private final class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location locFromGps) {
// called when the listener is notified with a location update from the GPS
}
@Override
public void onProviderDisabled(String provider) {
// called when the GPS provider is turned off (user turning off the GPS on the phone)
// Dim the button here!
}
@Override
public void onProviderEnabled(String provider) {
// called when the GPS provider is turned on (user turning on the GPS on the phone)
// Brighten the button here!
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// called when the status of the GPS provider changes
}
}
Then you'll want to register that listener. This should probably go in your onCreate()
LocationListener locationListener = new MyLocationListener();
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, this.locationListener);
The second and third parameters in requestlocationupdates you should probably make huge so you don't get locationupdates since you don't really care about those, only provider enabled/disabled changes.

Sam Burba
- 381
- 1
- 6
-
just a little (unrelated) question... how do i get reference to the button from `MyLocationListener` to brighten/dim it? – zoran119 Nov 29 '12 at 09:21
-
The easiest way to do it is make `MyLocationListener` an inner class in your activity so it has access to all of your activities methods and fields. To make it an inner class just put the class code inside your activity code. [This site](http://viralpatel.net/blogs/inner-classes-in-java/) provides a decent intro to inner classes but if you have questions just let me know. – Sam Burba Nov 29 '12 at 10:26
-
thank you sam. i did try the inner class and that is working well. just out of interest, if i wanted to make the location listener a stand alone class (because it might be used from inside the service as well) what would be the approach to get reference to the button? is it a good idea to share the listener or just create an inner class for the service as well? – zoran119 Nov 29 '12 at 21:30
-
It would be a good idea to make the location listener a stand alone class if you want to use it in multiple places. To communicate between the listener and the activity or service have the listener define an [interface](http://docs.oracle.com/javase/tutorial/java/IandI/createinterface.html) and make any class that wants to use the listener implement that interface. Create a constructor for the listener that takes an instance of the interface as a parameter. – Sam Burba Dec 01 '12 at 00:19
0
Please use this code to get the GPS status. use this code in the onResume of the activity
private LocationManager mLocationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
boolean GPSprovider = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
and according to the GPSProvider you can enable and disable the button.

Dinesh Prajapati
- 9,274
- 5
- 30
- 47
-
That's what I was thinking, but it seems like `onResume` doesn't get fired when you pull the notification bar down (some phones have GPS shortcut on the notification bar and GPS can be enabled/disabled this way). – zoran119 Nov 29 '12 at 08:48
0
try this
final String GpsProvider = Settings.Secure.getString(
getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (GpsProvider.equals(""){
//No GPS
}else{
//GPS available
}

Mohsin Naeem
- 12,542
- 3
- 39
- 53
-
i know how to detect if GPS is on... i want my activity to notified of the change to GPS status – zoran119 Nov 29 '12 at 08:49