質問

I have been working on a location based app.

I am testing on both Samsung Galaxy S4 and Samsung Tab 7.

When calling getBestProvider() it usually returns GPS if it was enabled and network otherwise, and that was the case on my Galaxy S4.

However, when I tried to run the application on the tab, the GPS provider was never returned by getBestProvider() even when it was enabled.

The S4 has android 4.2.2 while the tab has 4.1.1, could that be the problem, if not, what is?

役に立ちましたか?

解決

Regardless of what provider you use GPS, NETWORK or best, all work on the same principle of getting the location from a local (on the device) cache.

If you want GPS instantly you need to hack the system to update the cache and then read it. On Samsung phones this hack is needed. On HTC and other models you dont need this hack. I (fortunately) have found the way to trigger that cache update with a hack.

Just call the following code before your getLastKnownLocation.

<YourActivity>.getLocationManager().requestLocationUpdates(
        LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
        @Override
        public void onProviderEnabled(String provider) {
        }
            @Override
        public void onProviderDisabled(String provider) {
        }
        @Override
        public void onLocationChanged(final Location location) {
    }
});

OR

Use LocationClient. The new API's (just in) for location updates. These API's dont need you to find the best provider instead do all this shit underneath.

他のヒント

getBestProvider() won't always return GPS. Because GPS is not always the best provider. Is the tab connected to wifi? Location from wifi is quite accurate so that may be returned if you are connected to wifi.

Also, you should switch to the new Location service which is part of Google Play Services. Using the fuse location provider will take all the hassle out of managing the providers and give you the best result.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top