在 Android 中使用计时器每 10 分钟跟踪一次 GPS
我正在开发一个 Android 应用程序,并且正在使用一项每 10 分钟通过 GPS 跟踪一次位置的服务。但是当我将位置侦听器放入计时器任务中时,它会抛出异常:
无法将代码放入未调用
looper.prepare()
的线程内。
有人对这个问题有任何见解吗?
I am developing an android application and I am using a service that tracks location via GPS every 10 minutes. But when I put the location listener in a timer task, it throws an exception:
Cannot put code inside thread that has not called
looper.prepare()
.
Does anyone have any insight on this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无需每 10 分钟启动一项服务来接收位置更新
相反,
它会每 10 分钟自动向监听器发送一次更新
当你遇到像 Looper 这样的错误时, 。这意味着您不允许从主 UI 工作线程之外的其他线程执行某些操作。
您正在运行什么代码并收到此错误。从主 UI 线程执行代码。或者
通过主线程在主类中创建一个 Handler 对象
然后在任何其他线程中执行类似的操作
You need not to start a service for recieving locationUpdate every 10 min
instead do something like this
this will automatically send update to the listener every 10 min
When ever you got error like Looper . that mean you are not allowed to do something from other then Main UI Worker thread.
What so code you are running and getting this error . execute the code from Main UI Thread. or
create a Handler object in your main class by Main Thread
Then in any of your other thread do something like this
这个方法很简单,可以更新UI线程:
This method is easy and can update UI thread: