Android 工作管理器:如何每月将作业入队一次 [英] Android Work Manager: How to enqueue Jobs once a month
问题描述
按照这个问题,我研究了 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 分钟,但有一个问题:没有可用的时间单位数月,只有数天.由于显而易见的原因,每 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.
推荐答案
您可以按分钟完成:
只需将天数转换为分钟:
Just convert days to minutes:
此处: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 点之间每 30 分钟执行一次任务
UPDATE:
As per my requirement, I wanted to perform task every 30 minutes in between 8 am to 6 pm
在我为您制作的那个工人的帮助下,您需要每个月的第一天,上午 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 小时,以便每两小时检查一次)
不知道会不会对性能有影响,但我认为没有其他解决方案.
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 工作管理器:如何每月将作业入队一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!