3

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;
    }

4 Answers 4

22

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).

4
  • thanks for this. A couple days ago i had the app running and when I walked outside of the my building i got a notification for a status change. it looks like for locationlistener, if you are never able to get a gps fix in the first place, on status change wont be called. Jan 10, 2013 at 16:16
  • 1
    Sorry let me clarify for anyone else who's looking at this. The notification I received was from having a gps lock to temporarily unavailable. NOT from no gps lock (when I was in the building) to having a gps lock. In other words, I had to have a GPS lock first before I started receiving any status change notifications. Jan 22, 2013 at 5:28
  • Sorry for bit of bump but how would I use your solution in background service? I mean I have LocationListener in background service which has no onCreate
    – arleitiss
    Jul 27, 2014 at 0:29
  • @arleitiss You should still be able to register the GPSStatus.Listener in onCreate() or onStartCommand() of your Service. Basically, whenever your Service starts and you execute some code, run the same mService = getSystemService... and mService.addGpsStatus... lines that are in the above answer. Jul 27, 2014 at 2:03
1

maybe you need to check this link GpsStatus.Listener this listener is used to know when GPS status has changed.

and as for the method you pointed, it is used to know when the provider status changed as mentioned in the documentation LocationListener

0

I worked on the GPS for past several week and whatever I noticed from my testing, there could be a possibility to get the GPS signals inside a building. If you are in a basement area, then you will never get. I tried from my home and 95% of time get my latitude and longtitude always.

You can't control this. GpsLocationProvider automatically uses assisted SUPL if there is an internet or WiFi connection available. The standalone case is only used when internet isn't accessible. Assisted data help in locking on to the GPS satellite signal. In indoor cases, you can see 2-3 satellites (it depends on the quality of your chipset). In case no satellite signal can be scanned, you can't get a fix.

Thanks, Ramesh S

2
  • in my house i can get gps fine, but in the building im in i cannot. Is there somethign wrong with my code? why am I not getting notices for GPS status? Jan 8, 2013 at 20:04
  • how do you make sure, that you didnt receive the callback for Out of Service. Since you are showing Toast and also for short duration, there might be a chance, it would have shown and you might be missed.I tried similar in the past and it used to show the respective status Available when I am moving and once I reached a mall or my office, it used not Out of service or Not availablle.. I think you can vibrate at the time of showing the toast, so you can quickly look into your mboile for the status Jan 8, 2013 at 21:42
0

Android documentation says that onStatusChanged method is just deprecated:

This callback will never be invoked on Android Q and above, and providers can be considered as always in the LocationProvider#AVAILABLE state.

But I found out that it still needs to be implemented even though Android Studio doesn't complain about it, otherwise the app will crash

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.