使用Work Manager API每秒执行一次任务 [英] Execute task every second using Work Manager API

查看:459
本文介绍了使用Work Manager API每秒执行一次任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Work Manager是一个新的API,我尝试每秒执行一次任务,但是它不起作用.

Work Manager is a new API and I try to execute task every second, but it doesn't work.

这是我的工人班

class TestingWorker : Worker(){
    override fun doWork(): Result {
        Log.i("CheckWorker","Result here")
        return Result.SUCCESS
    }
}

这就是我叫它的地方.

 val recurringWork: PeriodicWorkRequest = PeriodicWorkRequest.Builder(TestingWorker::class.java, 1, TimeUnit.SECONDS).build()
 WorkManager.getInstance()?.enqueue(recurringWork)

推荐答案

它不起作用,因为两个定期工作请求之间的最小间隔为15分钟,这由

Its not working because, the minimum interval between two periodic work request is 15 min which is defined by MIN_PERIODIC_INTERVAL_MILLIS.

基于

创建一个PeriodicWorkRequest以每个间隔定期运行一次 时期.保证PeriodicWorkRequest只能运行一次 在此间隔内. 间隔Millis必须大于或 等于PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS .有可能 立即运行,在周期结束时或在此之间的任何时间运行 只要当时满足其他条件即可.

Creates a PeriodicWorkRequest to run periodically once every interval period. The PeriodicWorkRequest is guaranteed to run exactly one time during this interval. The intervalMillis must be greater than or equal to PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS. It may run immediately, at the end of the period, or any time in between so long as the other conditions are satisfied at the time.

我建议您避免如此频繁地安排工作.最终会消耗更多资源,并最终影响电池寿命.

I would recommend you to avoid scheduling work so frequently. This will end up in consuming more resources and eventually impacting the battery life.

这篇关于使用Work Manager API每秒执行一次任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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