Handler.postDelayed running for extra time

喜欢而已 提交于 2019-12-04 22:23:21
Guillermo Merino

You can check this answer and implement this behaviour with AlarmManager.

As you can see in the documentation, there can be an additional delay for the runnable to start running. Also you can check this explanation from @CommonsWare:

Android is not a real-time operating system. All postDelayed() guarantees is that it will be at least the number of milliseconds specified. Beyond that will be dependent primarily on what the main application thread is doing (if you are tying it up, it cannot process the Runnable), and secondarily on what else is going on the device (services run with background priority and therefore get less CPU time than does the foreground).

Edit: Keep in mind that Timer creates a new thread for your code to be executed. So it doesn't depend on main thread's workload. You can check a discussion about this here

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