androidgpslocationlocationmanagerlocationlistener

Androids onStatusChanged not working


I'm trying to get notifications if the status of GPS_PROVIDER changes. I found the following code here (http://hejp.co.uk/android/android-gps-example/), but I'm not getting notifications.

  1. Right now I'm in a building and can't get GPS signal, so wouldn't I get the notification "Status Changed: Out of Service"?
  2. When is onStatusChanged being called?
  3. What am I doing wrong?

Thanks,

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    /* This is called when the GPS status alters */
    switch (status) {
    case LocationProvider.OUT_OF_SERVICE:
        Log.v(tag, "Status Changed: Out of Service");
        Toast.makeText(this, "Status Changed: Out of Service",
                Toast.LENGTH_SHORT).show();
        break;
    case LocationProvider.TEMPORARILY_UNAVAILABLE:
        Log.v(tag, "Status Changed: Temporarily Unavailable");
        Toast.makeText(this, "Status Changed: Temporarily Unavailable",
                Toast.LENGTH_SHORT).show();
        break;
    case LocationProvider.AVAILABLE:
        Log.v(tag, "Status Changed: Available");
        Toast.makeText(this, "Status Changed: Available",
                Toast.LENGTH_SHORT).show();
        break;
    }

Solution

  • If your GPS status isn't changing (e.g., if you're always indoors without a GPS fix) while the app is running, some devices won't trigger the OnStatusChanged() method.

    If you change GPS statuses while the app is running (e.g., you're inside and can't get a fix and then walk outside and can get a fix, or vice versa), then the OnStatusChanged() method should fire on all devices.

    If you want a fully working open-source app to use as an example, try GPSTest (full disclosure, my app):

    GPSTest on Google Play - https://play.google.com/store/apps/details?id=com.android.gpstest

    Source code for GPSTest - https://github.com/barbeau/gpstest

    For more detailed information about GPS that is constantly updated even if your device can't get a fix, you might want to register a GPSStatus.Listener.

    In your Activity, make it implement GpsStatus.Listener, for example:

    public class GpsTestActivity extends TabActivity
        implements LocationListener, GpsStatus.Listener{
    

    Then, in your activity declare class variables:

    private LocationManager mService;
    private GpsStatus mStatus;
    

    ...and add the method to handle the GPSStatus changes:

    public void onGpsStatusChanged(int event) {
        mStatus = mService.getGpsStatus(mStatus);
        switch (event) {
            case GpsStatus.GPS_EVENT_STARTED:
                // Do Something with mStatus info
                break;
    
            case GpsStatus.GPS_EVENT_STOPPED:
                // Do Something with mStatus info
                break;
    
            case GpsStatus.GPS_EVENT_FIRST_FIX:
                // Do Something with mStatus info
                break;
    
            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                // Do Something with mStatus info
                break;
        }
    
    }
    

    Then in OnCreate() of your Activity to register the GPSStatus.Listener:

     mService = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
     mService.addGpsStatusListener(this);
    

    In the GPSTest app, the list of currently available satellites is shown on the screen with each GPSStatus.Listener update, based on this code:

    https://github.com/barbeau/gpstest/blob/master/GPSTest/src/main/java/com/android/gpstest/GpsStatusFragment.java

    This way, you'll receive active updates on the GPS status of system even if your phone can't get a GPS fix (and therefore may not trigger OnStatusChanged of the LocationListener).