Pregunta

I have a strange issue with the GPS-component on the Samsung Galaxy Tab (GT-P7310): GPS works fine in Google Maps, but does not provide any location in my own application. Although my application works fine on my Samsung Galaxy S2.

I am calling the location-service like this:

LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Location currentLocation = locationManager.getLastKnownLocation(locationManager.getBestProvider(new Criteria(), false));
// Constantly update the location
locationManager.requestLocationUpdates(locationManager.getBestProvider(new Criteria(), false), 0, 0, listener);

but all I get is null as location and the callback-listener is never called.

The Tablet is running Android 4.2.2 with CyanogenMod 10.1-20130512-UNOFFICIAL-p5wifi. Location access is turned on (otherwise Google Maps wouldn't work too).

My app has the following permissions set in its Manifest:

  • ACCESS_FINE_LOCATION
  • ACCESS_COARSE_LOCATION
  • INTERNET

Any ideas, why I get no location on this device?

¿Fue útil?

Solución

Solution: you can use GPS instead of selecting best provider by deciding criteria.

Example:

replace

    Location currentLocation = locationManager.getLastKnownLocation(locationManager.getBestProvider(new Criteria(), false));
// Constantly update the location
locationManager.requestLocationUpdates(locationManager.getBestProvider(new Criteria(), false), 0, 0, listener);

with

    Location currentLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
// Constantly update the location
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);

Explaination:

Depending on your application's use case, you can to choose a specific location provider i.e. LocationManager.GPS_PROVIDER or LocationManager.NETWORK_PROVIDER

Alternatively, you can provide some input criteria such as accuracy, power requirement, monetary cost, and so on, and let Android decide a closest match location provider

    // Retrieve a list of location providers that have fine accuracy, no monetary cost, etc
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setCostAllowed(false);
String providerName = locManager.getBestProvider(criteria, true);
//and then you can make location update request with selected best provider
locationManager.requestLocationUpdates(providerName, 0, 0, listener); 

Have a look at how to use locationmanager , how to specify Criteria and how getBestProvider method works for reference

I hope it will be helpful !!

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top