Android - Timer not running in perodic intervals when app is in locked or sleep state

爷,独闯天下 提交于 2019-12-11 06:14:59

问题


In my android project I have periodic task to update API for every 60 seconds for that i am using TimerTask everything is working fine when app is in background or foreground when app is in locked state or sleep state timer milliseconds is lagging every minute, such as first api timing is 10.00.00 second api timing is 10.01.05 for every count milliseconds is getting increased

I have tried Work Manager but my perodic interval is 60 Sec,I need an answer which runs perodic Jobs for every 60 Sec without any time lag

I refered this code https://github.com/codexpedia/android_sync_adapter its working fine I have one more issue in SyncAdapter, Some devices Sync Adapter skips Eg: - If I set 60 Sec interval it skips 1 min for every 5 mins could you help me

If app is in doze mode any other solution which works for 7 & 8 api version


回答1:


Since your requirement is to sync the data between app and server you can use the native feature of Android called SyncAdapter, this will sync the data either way based on a time interval. You can read about it here in the docs. I would suggest this approach instead of TimerTask.



来源:https://stackoverflow.com/questions/50753823/android-timer-not-running-in-perodic-intervals-when-app-is-in-locked-or-sleep

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