且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何保持后台gps服务活着

更新时间:2023-10-23 19:35:34

但是我意识到,几个小时后,gps 服务被终止,我再也找不到位置了.

However I realized that after a couple of hours the gps service gets killed and I can't get anymore locations.

首先,让 GPS 始终处于开启状态是一个非常糟糕的主意,因为用户的电池寿命会受到很大影响.您的应用程序需要提供巨大的价值(例如 Google 导航)来保证这种电力成本.

First, it is an exceptionally bad idea to keep GPS powered on all of the time, as the user's battery life will suffer greatly. Your application needs to offer tremendous value (e.g., Google Navigation) to warrant this power cost.

其次,永远不要从 IntentService 注册监听器.一旦 onHandleIntent() 结束,服务就会关闭……但你会泄露你注册的监听器.这有效地保持了后台线程的运行.但是,由于您没有活动组件,Android 最终会终止您的进程.

Second, never register a listener from an IntentService. Once onHandleIntent() ends, the service shuts down... but you leak your registered listener. This effectively keeps a background thread going. However, since you have no active components, Android eventually will terminate your process.