重启后Android位置重置? - >严重的问题

时间:2010-05-06 13:07:59

标签: android gps location nullpointerexception

有可能,android重启后会清除最后一个已知位置吗? 昨天我的代码运行得非常好,但今天重新启动我的手机(AND模拟器)后,似乎.getLastKnownLocation(见下文)返回null,这导致nullPointerException ... 你能证实吗? 我该如何避免这个问题?我正在拼命寻找答案

lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

...

Location locUser = lm
    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
posUser = new GeoPoint((int) (locUser.getLatitude() * 1E6),
(int) (locUser.getLongitude() * 1E6));

如果有人能给我一个暗示或指出我的错误,那就太好了。

很好的问候,Poeschlorn

1 个答案:

答案 0 :(得分:2)

getLastKnownLocation()的调用不会阻止 - 这意味着如果当前没有可用的位置,它将返回null,这很可能是在设备重启后。如果Android在用户关闭设备时缓存当前位置,我会感到惊讶,因为设备可能会在再次重新打开之前移动。

您需要将LocationListener传递给requestLocationUpdates() method,这样可以为您提供位置的异步更新。

查看this question for an example of using a LocationListener

相关问题