当我需要位置 android 时出错

发布于 2024-12-01 05:37:41 字数 1724 浏览 1 评论 0原文

我有一个问题。在我的应用程序中,我需要用户的当前位置。当我们有一个位置时(当我使用 locationManager.getLastKnownLocation 获取位置时)它工作正常,但是当我重新启动手机并且位置消失(我认为现金是空的)时,我得到此日志并强制关闭应用程序按钮。我点击一个按钮,我的应用程序重新启动,然后它工作正常...... 我现在的问题是返回 null 的位置... 这是日志:

08-25 10:02:55.000: ERROR/GPS(2006): gen_timer_add_locked
08-25 10:02:55.449: DEBUG/libgps(2006): onUnsol: cmd 0x04 plen 57
08-25 10:02:55.449: DEBUG/libgps(2006): oem_unsol_gps_measurement: num_sv 0
08-25 10:02:55.449: DEBUG/libgps(2006): oem_unsol_gps_measurement: used_in_fix_mask 00000000 
08-25 10:02:55.503: DEBUG/libgps(2006): request_location_fix_callback: session status 1
08-25 10:02:55.503: DEBUG/libgps(2006): request_location_fix_callback: GPS session is ongoing.
08-25 10:02:55.503: ERROR/GPS(2006): gen_timer_add_locked
08-25 10:02:56.006: DEBUG/libgps(2006): request_location_fix_callback: session status 1
08-25 10:02:56.006: DEBUG/libgps(2006): request_location_fix_callback: GPS session is ongoing.
08-25 10:02:56.006: ERROR/GPS(2006): gen_timer_add_locked
08-25 10:02:56.449: DEBUG/libgps(2006): onUnsol: cmd 0x04 plen 57
08-25 10:02:56.449: DEBUG/libgps(2006): oem_unsol_gps_measurement: num_sv 0
08-25 10:02:56.449: DEBUG/libgps(2006): oem_unsol_gps_measurement: used_in_fix_mask 00000000 
08-25 10:02:56.505: DEBUG/libgps(2006): request_location_fix_callback: session status 1
08-25 10:02:56.505: DEBUG/libgps(2006): request_location_fix_callback: GPS session is ongoing.
08-25 10:02:56.505: ERROR/GPS(2006): gen_timer_add_locked
08-25 10:02:57.009: DEBUG/libgps(2006): request_location_fix_callback: session status 1
08-25 10:02:57.009: DEBUG/libgps(2006): request_location_fix_callback: GPS session is ongoing.
08-25 10:02:57.009: ERROR/GPS(2006): gen_timer_add_locked

I have a problem. In my app i need current location from a user. It works fine when we have a location(when i get a location usig method locationManager.getLastKnownLocation), but when i restart my phone and location disappear (cash is empty i presume) i get this log and force close app button. I click on a button, my app restarts and then it works fine...
I now that a problem is with location that return null...
This is the log:

08-25 10:02:55.000: ERROR/GPS(2006): gen_timer_add_locked
08-25 10:02:55.449: DEBUG/libgps(2006): onUnsol: cmd 0x04 plen 57
08-25 10:02:55.449: DEBUG/libgps(2006): oem_unsol_gps_measurement: num_sv 0
08-25 10:02:55.449: DEBUG/libgps(2006): oem_unsol_gps_measurement: used_in_fix_mask 00000000 
08-25 10:02:55.503: DEBUG/libgps(2006): request_location_fix_callback: session status 1
08-25 10:02:55.503: DEBUG/libgps(2006): request_location_fix_callback: GPS session is ongoing.
08-25 10:02:55.503: ERROR/GPS(2006): gen_timer_add_locked
08-25 10:02:56.006: DEBUG/libgps(2006): request_location_fix_callback: session status 1
08-25 10:02:56.006: DEBUG/libgps(2006): request_location_fix_callback: GPS session is ongoing.
08-25 10:02:56.006: ERROR/GPS(2006): gen_timer_add_locked
08-25 10:02:56.449: DEBUG/libgps(2006): onUnsol: cmd 0x04 plen 57
08-25 10:02:56.449: DEBUG/libgps(2006): oem_unsol_gps_measurement: num_sv 0
08-25 10:02:56.449: DEBUG/libgps(2006): oem_unsol_gps_measurement: used_in_fix_mask 00000000 
08-25 10:02:56.505: DEBUG/libgps(2006): request_location_fix_callback: session status 1
08-25 10:02:56.505: DEBUG/libgps(2006): request_location_fix_callback: GPS session is ongoing.
08-25 10:02:56.505: ERROR/GPS(2006): gen_timer_add_locked
08-25 10:02:57.009: DEBUG/libgps(2006): request_location_fix_callback: session status 1
08-25 10:02:57.009: DEBUG/libgps(2006): request_location_fix_callback: GPS session is ongoing.
08-25 10:02:57.009: ERROR/GPS(2006): gen_timer_add_locked

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

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

发布评论

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

评论(3

抱着落日 2024-12-08 05:37:41

当您重新启动手机时,所有非持久数据将像变量一样被清除。如果您想保存您的位置,那么您应该使用database/sharedPreferences。
另外,您可能会获取 null 位置。因此您应该继续检查以进行处理。

编辑

您可以使用 Timer 类。

尝试这样:

 Timer timer1;
  LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
  timer1=new Timer();
  timer1.schedule(new GetLastLocation(), 20000);

以下代码将在 20 秒后执行。

class GetLastLocation extends TimerTask {
        @Override
        public void run() {
             timer1.cancel();
             lm.removeUpdates(locationListener)
             Location loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        }
}

when you restart your phone then all the non-persistent data will be cleared like variable.If you want to save your location then you should use database/sharedPreferences.
Also,you can get null location.So you should keep checking to handle.

Edited

You can use Timer class.

Try like this:

 Timer timer1;
  LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
  lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
  timer1=new Timer();
  timer1.schedule(new GetLastLocation(), 20000);

Following code will be executed after 20 sec.

class GetLastLocation extends TimerTask {
        @Override
        public void run() {
             timer1.cancel();
             lm.removeUpdates(locationListener)
             Location loc=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        }
}
摘星┃星的人 2024-12-08 05:37:41

查看深入了解位置几乎了解如何获取位置的所有信息。

Have a look at A Deep Dive Into Location to know pretty much all about how to get a location.

盛夏已如深秋| 2024-12-08 05:37:41

我设法解决了这个问题。我创建了没有按钮的 AlertDialog 并启动了 requestLocationUpdates。在 locationListener 中,当位置更改时,我取消了 AlertDialog,这解决了我的问题:D
谢谢杰纳尔和托斯顿......

I managed to solve the problem. I created AlertDialog with no buttons and started requestLocationUpdates. In locationListener, when location changed i canceled AlertDialog and that solved my problem :D
THANKS jainal and Torsten....

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