make an IntentService not sleep until it executes a handler.postDelayed runnable

一曲冷凌霜 提交于 2019-12-02 00:57:42

Don't use an IntentService. It is not designed for your scenario. Use a regular Service. Put your code in onStartCommand(). At the bottom of your run() method, call stopSelf() on the Service instance to shut it down.

You need to stop onHandleIntent from returning until the Runnable has completed. This can be achieved by using a CountdownLatch, which awaits at the end of your onHandleIntent method, and gets released at the end of the run method.

NOTE: this will cause the intent service to not process other Intents you sent it until the previous one has completed (after 20 seconds).

You may also want to obtain a partial wakelock at the start of onHandleIntent and release it at the end.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!