Android - 当应用程序处于锁定或睡眠状态时,计时器不会定期运行 [英] Android - Timer not running in perodic intervals when app is in locked or sleep state

查看:49
本文介绍了Android - 当应用程序处于锁定或睡眠状态时,计时器不会定期运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 android 项目中,我有定期任务每 60 秒更新一次 API,因为我正在使用 TimerTask 当应用程序处于后台或前台时,当应用程序处于锁定状态或睡眠状态计时器毫秒每分钟滞后一次时,一切正常,例如第一个 api 计时为 10.00.00 第二个 api 计时为 10.01.05 为每计数毫秒增加

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

我尝试过工作管理器,但我的周期性间隔是 60 秒,我需要一个答案,它每 60 秒运行一次周期性作业而没有任何时间滞后

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

我参考了此代码 https://github.com/codexpedia/android_sync_adapter 其工作正常我在 SyncAdapter 中还有一个问题,某些设备同步适配器会跳过例如:-如果我设置 60 秒间隔,它每 5 分钟跳过 1 分钟,你能帮我吗

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

如果应用程序处于打盹模式,任何其他适用于 7 & 的解决方案8 api版本

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

推荐答案

由于您的要求是在应用程序和服务器之间同步数据,您可以使用 Android 的原生特性,称为 SyncAdapter,这将根据时间间隔同步数据.您可以在 docs 中阅读相关内容.我建议使用这种方法而不是 TimerTask.

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.

这篇关于Android - 当应用程序处于锁定或睡眠状态时,计时器不会定期运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆