androidgpslocationlocation-provider

Location issue in Android


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?


Solution

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