views:

120

answers:

1

Hey mates, 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, 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

+2  A: 

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.

Dave Webb
hi dave,what you pointed out seems to be logical ;-)I already implemented a working Locationlistener, but it updates just ervery 1-5 sec (param).how can I tell it to refresh before the code above is beeing executed?
poeschlorn
I don't think you can force it to refresh. The call doesn't block so if GPS is taking a while to get a lock the getLastKnownLocation() will return `null` and your app will have to handle this situation.
Dave Webb

related questions