Android Work Manager:如何每月一次入队 [英] Android Work Manager: How to enqueue Jobs once a month

查看:160
本文介绍了Android Work Manager:如何每月一次入队的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题之后,我研究了Android Job Scheduling,如果我想每月进行一次操作,似乎Work Manager是最新的工具.我建立了一个Worker类,并在我的活动中使用以下代码片段激活了它:

following this question, I looked into Android Job Scheduling and it seems like Work Manager is the latest tool if I want to do something once every month. I built a Worker class and activated it with this snippet in my activity:

PeriodicWorkRequest myWorkRequest =
                new PeriodicWorkRequest.Builder(MonthlyWorker.class, 20, TimeUnit.MINUTES)
                        .build();
WorkManager.getInstance().enqueueUniquePeriodicWork("my fancy test",ExistingPeriodicWorkPolicy.KEEP, myWorkRequest);

虽然我成功测试了20分钟的时间,但是存在一个问题:没有几个月可用的TimeUnit,只有几天.由于显而易见的原因,每30天执行一次工作将不会成功,因为我需要在每月的第一天执行该任务.所以我该怎么做?我必须使用其他API吗?谢谢.

While i tested the 20 minutes period successfully, there is a problem: There are no TimeUnits for months available, only days. Doing the job every 30 days wont work for obvious reasons, since I need the task to be executed on the first day of each month. So what should i do? Do I have to use a different API? Thank you.

我可以做类似if (dayOfMonth==1) do Function的操作,但是我认为这样做破坏了调度程序的作用.

I could do something like if (dayOfMonth==1) do Function, but I think that undermines the purpose of a scheduler.

推荐答案

您可以自己按分钟完成操作:

You can do by minutes itself:

只需将天转换为分钟:

此处: 30天= 43200分钟= 1个月

PeriodicWorkRequest myWorkRequest =
            new PeriodicWorkRequest.Builder(MonthlyWorker.class, 43200, TimeUnit.MINUTES)
                    .build();
WorkManager.getInstance().enqueueUniquePeriodicWork("my fancy test",ExistingPeriodicWorkPolicy.KEEP, myWorkRequest);

更新:

根据我的要求,我希望在上午8点至下午6点

在您要求的每月的第一天,上午12点至凌晨2点下,我为您提供了帮助.

With help of that worker I made for you as you required 1st of every month in between 12 am to 2 am below:

public class TestWorker extends Worker {

    private static final String DEFAULT_START_TIME = "00:01"; //12:01 am
    private static final String DEFAULT_END_TIME = "03:00"; // 03:00 am

    private static final String TAG = "TestWorker";

    public TestWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {

        /**
         * Its for time
         */
        DateFormat dateFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());

        /**
         * Get Current Calendar Instance
         */
        Calendar c = Calendar.getInstance();

        // Get current day of month
        int day = c.get(Calendar.DAY_OF_MONTH);

        /**
         * Get current time and compare with our start and end date.
         */
        Date date = c.getTime();
        String formattedDate = dateFormat.format(date);

        try {
            Date currentDate = dateFormat.parse(formattedDate);
            Date startDate = dateFormat.parse(DEFAULT_START_TIME);
            Date endDate = dateFormat.parse(DEFAULT_END_TIME);

            /**
             * This condition will check that day is 1, and time interval in between give time.
             */         
            if (day == 1 && currentDate.after(startDate) && currentDate.before(endDate)) {
                // Do your work here
            }
        } catch (ParseException ignored) {

        }

        return Result.success();
    }
}

您可以每隔X个小时开始一次此测试工作,以便每隔X个小时检查一次(例如,根据您的情况,您可以给2个小时,以便每隔2个小时检查一次)

我不知道它是否会影响性能,但是我认为没有其他解决方案.

I do not know whether it will be affected to performance or not, but I think there is no other solution.

您可以按以下步骤启动工作程序:

You can start worker as below:

PeriodicWorkRequest periodicWork = new PeriodicWorkRequest.Builder(MyWorker.class, 2, TimeUnit.HOURS)
                .addTag("MYWORK")
                .build();

WorkManager.getInstance().enqueueUniquePeriodicWork("MYWORK", ExistingPeriodicWorkPolicy.REPLACE, periodicWork);

希望这会有所帮助.

这篇关于Android Work Manager:如何每月一次入队的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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