I am writing an app that uses GPS for localization services. When the user tries to use the app with a disabled GPS, a dialog pops up. Now, the user can minimize the app and change the GPS settings. When he reopens the app, it detects in onResume() that the settings were changed and closes the dialog. That works fine, but the user has a second way to enable GPS: the status bar. When the status bar is opened and closed, neither onResume() nor onWindowFocusChanged() gets called, so that I don't detect when the user enabled GPS.
Is there a way to detect the opening of the status bar?
I could do it via the LocationListener, but I would like to do it how I wrote above.
Thanks in advance!
I found help at android-dev IRC. Now, I am using an Observer to notice setting changes like here. So, I detect all setting changes, even if they are done via the notifications bar, without requesting updates from the LocationManager. Thanks for your help!
For listening to GPS that user has enabled, using a status bar, you can setup a listener for changes via the location manager:
private class DisabledGPSListener implements LocationListener{
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
locationManager.removeUpdates(this);
locationManager.addGpsStatusListener(gpsStatusListener);
}
set this listener inside onCreate()
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
locationManager.addGpsStatusListener(gpsStatusListener);
}else{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, new DisabledGPSListener());
}
where gpsStatusListener is instance of:
private class GPSStatuslistener implements GpsStatus.Listener {
@Override
public void onGpsStatusChanged(int event) {
switch (event) {
case GpsStatus.GPS_EVENT_STARTED:
Log.d(TAG, "ongpsstatus changed started");
//TODO: your code that get location updates, e.g. set active location listener
break;
case GpsStatus.GPS_EVENT_STOPPED:
Log.d(TAG, "ongpsstatus changed stopped");
createGpsDisabledAlert();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With