1

Is there any Code I can use, so that I do not have to implement it on my own to change automatically from GPS_PROVIDER to NETWORK_PROVIDER?

actually I use this code:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

But I want to do the following:
Try to use GPS, as long as it is not available use the NETWORK_PROVIDER. But when GPS get available switch to it. When the GPS is lost switch back to the NETWORK_PROVIDER.

4

4 回答 4

2
Location loc =locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);    
    if(loc == null)
    {
        loc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if(loc != null)
        {
            //do stuff on network provider
        }

    } else {
        // do stuff on gps provider
    }
于 2013-05-22T09:07:15.383 回答
1

You should have a look at the new fused location provider in Google Play Services. It was presented at the Google IO 2013.

It removes the hassle of selecting a certain LocationProvider, provides better results and much more.

For an overview have a look here or watch the Video. For fused location provider code example tune in at 16:00.

于 2013-05-22T09:10:22.630 回答
1

I will suggest a better option, try use a Criteria to select the best Provider. It will switch automatically switch for the best provider.

String provider = lm.getBestProvider(criteria, true);

Get a look here for more details.

Good luck

于 2013-05-22T09:22:42.377 回答
0

use locationmanager.getProviders as mentioned here. this way each time your location listener will be entered, you'll be able to pick your location from available providers.

于 2013-05-22T09:08:06.130 回答