Androids onStatusChanged not working

╄→尐↘猪︶ㄣ 提交于 2019-11-27 13:23:19

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!