Androids onStatusChanged not working

2019-01-11 09:05发布

问题:

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

回答1:

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



回答2:

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



回答3:

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