Android 重启后位置会重置吗? ->严重问题
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对
getLastKnownLocation()
的调用不会阻塞 - 这意味着如果当前没有可用位置,它将返回null
,这很可能在设备重新启动后发生。如果 Android 在用户关闭设备时缓存当前位置,我会感到惊讶,因为设备很可能在再次打开之前会移动。您需要传递
LocationListener
到requestLocationUpdates()
方法 代替,这将为您提供位置的异步更新。看一下 这个问题的示例使用
LocationListener
。The call to
getLastKnownLocation()
doesn't block - which means it will returnnull
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 therequestLocationUpdates()
method instead, which will give you asynchronous updates of your location.Have a look at this question for an example of using a
LocationListener
.