Geolocation - Android get User Position after restart device

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

In my app I m trying to find the position from the device. My Code work s great:

boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if(isGPSEnabled || isNetworkEnabled)
            location = locationManager.getLastKnownLocation(isGPSEnabled ? LocationManager.GPS_PROVIDER : LocationManager.NETWORK_PROVIDER);

But after restart my device I don t get a location anymore. I tried it with another device. The code works, after restart I get null for my location... What is this?

Even after open google Maps, which found my position, I don t get the position...

Is it normal? Is there some workaround or something like this?

EDIT:

I don t use a LocationListener. I just use the code above in a asyntask for getting the position one time. I don t want to track the position the whole time

Answers

It seems to me that if the GPS is enabled but has not been called yet, you take it s location instead of the Network location (which is what you should take in that case). I would switch your entire block of code with that:


   Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
       if (location == null) location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/26211251/android-get-user-position-after-restart-device

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils