I have some problems with the location API. In some devices, sometimes it won`t give the location with any provider, though rebooting the phone solves the problem. On the other hand, when not getting the location, Google Maps places you in the map instantly with no problems.

Here is the code:

    Intent intent = new Intent(Constants.LOCATION_BROADCAST);
    pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
    Criteria criteria = new Criteria();
    criteria.setPowerRequirement(Criteria.POWER_HIGH);
    if(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.FROYO){
        criteria.setAccuracy(Criteria.ACCURACY_LOW);
        locationManager.requestSingleUpdate(locationManager.getBestProvider(criteria, true), pendingIntent);
    }
    else {
        criteria.setAccuracy(Criteria.ACCURACY_COARSE);
        locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria, true), 0, 0, pendingIntent);
    }

Any thoughts?

有帮助吗?

解决方案

Try using all the location providers and see if it solves your problem, i.e. something like

LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
List<String> allproviders = lm.getAllProviders();
for(String provider : allproviders) {
    myLocationManager.requestLocationUpdates(provider, 5000, 0, this);
}

If this code always works, then you'll just need to work out why your code is ignoring a provider that my code finds. Could easily be something to do with your power or accuracy criteia.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top