我需要在我的应用程序中执行以下逻辑: 每分钟,我都希望能够收听 GPS 位置 20 秒以获得 GEO 定位修复。
这显然需要运行与 UI 线程不同的线程。
然而,我在结合使用 looper 和 timertask 时遇到了一些问题......
实现此目标的最佳方法是什么?
最佳答案
我猜你需要使用 Service :
安卓文档:
A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.
所以服务启动LocationManager使用 requestLocationUpdates()并实现 LocationListener .
那么你有(至少)两个解决方案(取决于你想做什么):
- 将事件分发给需要使用 Location 的 Activity(例如使用 Broadcast)
- Activity 可以访问该服务并使用 getter 获取最新位置。
关于android - TimerTask、Geolocation 和 Looper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7777629/