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

发布于 2024-08-31 12:09:01 字数 519 浏览 3 评论 0原文

android 重启后是否有可能清除最后已知的位置? 昨天我的代码工作得很好,但今天重新启动我的手机(和模拟器)后,似乎 .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));

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

问候,波斯克伦

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2024-09-07 12:09:01

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

您需要传递 LocationListenerrequestLocationUpdates() 方法 代替,这将为您提供位置的异步更新。

看一下 这个问题的示例使用LocationListener

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文