简体   繁体   中英

Using OnStatusChanged to switch to NETWORK_PROVIDER from GPS_PROVIDER?

I'm working on an app that requires an accurate GPS fix. In the event that GPS_PROVIDER is enabled, but unavailable (like when you're in a tunnel), I want my app to switch to NETWORK_PROVIDER .

Can I use locationListener's OnStatusChanged OUT_OF_SERVICE to identify if a GPS fix cannot be made?

Something like this:

public void onStatusChanged(String provider, int status, Bundle extras) {
    //0 = OUT_OF_SERVICE
    if (status == 0) {
        //try requesting updates using NETWORK_PROVIDER
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll);
        //TOAST MESSAGE: GPS UNAVAILABLE ATTEMPTING FIX WITH NETWORK
        ...

Am I'm using OnStatusChanged correctly? Is there a simple and better way of first attempting a GPS fix and if unavailable, switching to NETWORK?

Adding lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, ll); it will start fetching the location using network but once you come out from the Tunnel, it will start fetching from Network and GPS. Using location.getProvider() you should be able to differentiate to know whether it's gps or network.

If your GPS status isn't changing (eg, 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 (eg, 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 by Mike Lockwood from the Android team.

GPSTest on Google Play

Source code for 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 GPSStatusListener.

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 GPSStatusListener:

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 GPSStatusListener update, based on this code .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM