Android Position reset after Restart? -> Serious Problem
is it possible, that android cleares the last known location after a restart? Yesterday my code worked very fine, but today after rebooting my phone (AND emulator) it seems that the .getLastKnownLocation (see below) returns null,开发者_Python百科 which leads to a nullPointerException... Can you confirm that? How can I avoid this problem? I'm desperately searching for an answer
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
...
Location locUser = lm
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
posUser = new GeoPoint((int) (locUser.getLatitude() * 1E6),
(int) (locUser.getLongitude() * 1E6));
Would be great if someone coult give me a hint or point out my mistake.
Nice greetings, Poeschlorn
The call to getLastKnownLocation()
doesn't block - which means it will return null
if no position is currently available, which is very likely after a device restart. I would be surprised if Android caches the current location when a user switches a device off since it is likely that the device will move before it is switched back on again.
You'll need to pass a LocationListener
to the requestLocationUpdates()
method instead, which will give you asynchronous updates of your location.
Have a look at this question for an example of using a LocationListener
.
精彩评论