使用 WorkManager 在特定时间安排工作 [英] Schedule a work on a specific time with WorkManager

查看:37
本文介绍了使用 WorkManager 在特定时间安排工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WorkManager 是一个用于排队工作的库,保证在满足其约束后执行.

WorkManager is a library used to enqueue work that is guaranteed to execute after its constraints are met.

因此,经过 Constraints 类后,我没有找到任何功能来增加工作的时间限制.例如,我想在上午 8:00 开始一项工作(工作可以是两种类型中的任何一种 OneTimeWorkRequestPeriodicWorkRequest) 在早上.如何使用 WorkManager 添加约束来安排这项工作.

Hence, After going though the Constraints class I haven't found any function to add time constraint on the work. For like example, I want to start a work to perform at 8:00am (The work can be any of two types OneTimeWorkRequest or PeriodicWorkRequest) in the morning. How can I add constraint to schedule this work with WorkManager.

推荐答案

很遗憾,您目前无法在特定时间安排工作.如果您有时间紧迫的实现,那么您应该使用 AlarmManager 来设置可以在打盹时触发的警报,方法是使用 setAndAllowWhileIdle()setExactAndAllowWhileIdle().

Unfortunately, you cannot schedule a work at specific time as of now. If you have time critical implementation then you should use AlarmManager to set alarm that can fire while in Doze to by using setAndAllowWhileIdle() or setExactAndAllowWhileIdle().

您可以使用 WorkManager 按如下方式安排工作,具有一次性初始延迟或定期执行:

You can schedule a work, with onetime initial delay or execute it periodically, using the WorkManager as follows:

创建 Worker 类:

public class MyWorker extends Worker {
    @Override
    public Worker.WorkerResult doWork() {

        // Do the work here

        // Indicate success or failure with your return value:
        return WorkerResult.SUCCESS;

        // (Returning RETRY tells WorkManager to try this task again
        // later; FAILURE says not to try again.)
    }
}

然后按如下方式安排OneTimeWorkRequest:

OneTimeWorkRequest mywork=
        new OneTimeWorkRequest.Builder(MyWorker.class)
        .setInitialDelay(<duration>, <TimeUnit>)// Use this when you want to add initial delay or schedule initial work to `OneTimeWorkRequest` e.g. setInitialDelay(2, TimeUnit.HOURS)
        .build();
WorkManager.getInstance().enqueue(mywork);

您可以按如下方式设置其他约束:

// Create a Constraints that defines when the task should run
Constraints myConstraints = new Constraints.Builder()
    .setRequiresDeviceIdle(true)
    .setRequiresCharging(true)
    // Many other constraints are available, see the
    // Constraints.Builder reference
     .build();

然后创建一个使用这些约束的 OneTimeWorkRequest

OneTimeWorkRequest mywork=
                new OneTimeWorkRequest.Builder(MyWorker.class)
     .setConstraints(myConstraints)
     .build();
WorkManager.getInstance().enqueue(mywork);

PeriodicWorkRequest 可以创建如下:

 PeriodicWorkRequest periodicWork = new PeriodicWorkRequest.Builder(MyWorker.class, 12, TimeUnit.HOURS)
                                   .build();
  WorkManager.getInstance().enqueue(periodicWork);

这会创建一个 PeriodicWorkRequest,每 12 小时定期运行一次.

This creates a PeriodicWorkRequest to run periodically once every 12 hours.

这篇关于使用 WorkManager 在特定时间安排工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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