问题描述
WorkManager.getInstance(activity).enqueueUniquePeriodicWork("test_work",
ExistingPeriodicWorkPolicy.KEEP,
PeriodicWorkRequest.Builder(MyWorker::class.java,
15, TimeUnit.MINUTES)
.build())
现在我正在使用PeriodicWork,并且即使我第一次尝试取消了uniquettask,现在doWork()
也被调用了两次.我得到了两次Notification(通知),并且也已登录Log(记录两次).
Now I'm using PeriodicWork and now doWork()
called twice even I cancelled uniquettask at first attempt.I'm getting Notification twice and Checked in Log also It's called twice.
override fun doWork(): Result {
if (checkTaskFinished()) {
Logger.e("checkXXX----Hurrayyyy ☻♥☺")
val notificationUtils: NotificationUtils = NotificationUtils(applicationContext)
notificationUtils.showNotificationMessage("Readddd", "rwaeaeae.", 2)
WorkManager.getInstance(context).cancelUniqueWork("test_work")
}
return Result.success()
}
OLD
我遇到了 WorkManager OneTimeWorkRequest setInitialDelay 的问题.
OLD
I'm facing problem with WorkManager OneTimeWorkRequest setInitialDelay.
当应用程序位于前景或最近列表中时,它可以很好地工作.但是,当我从最近的列表中删除App时,一切都搞砸了.
It's work fine when app is in forground or in recent list. But When I remove App from recent list everything is messed up.
我想实现什么?-我想在几个任务挂起后几个小时后向用户发送通知,因此在进行一些R& D开始使用 WorkManager 进行工作之后,因为它可以调度任务而没有后台服务限制.
What I want to achieve ?- I want send notification to user after few hours when some task is pending ,so after some R&D start work using WorkManager because of It's ability to Schedule Tasks without background service limitation.
Constraints constraints = new Constraints.Builder()
.setRequiresBatteryNotLow(true)
.build();
final OneTimeWorkRequest simpleRequest = new OneTimeWorkRequest.Builder(MyWorker.class)
.setInitialDelay(3, TimeUnit.MINUTES)
.setConstraints(constraints)
.addTag("simple_work")
.build();
WorkManager workManager = WorkManager.getInstance();
workManager.beginUniqueWork("simple_work", ExistingWorkPolicy.KEEP, simpleRequest).enqueue();
工人阶级
public class MyWorker extends Worker {
private NotificationUtils notificationUtils;
public static final String EXTRA_OUTPUT_MESSAGE = "output_message";
public MyWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
notificationUtils = new NotificationUtils(getApplicationContext());
notificationUtils.showNotificationMessage("TITLE", "This is a MESSEAGE",2);
Data output = new Data.Builder()
.putString(EXTRA_OUTPUT_MESSAGE, "I have come from MyWorker!")
.build();
setOutputData(output);
return Result.SUCCESS;
}
}
所以,请指导我做错了什么,或者不是我在一次设置的特定时间的计划任务.完成后,我不想保留重复的工作.我要在完成第一个OneTimeWorkRequest之后创建新的,所以它必须按照记录的 beginUniqueWork 正常工作.
So ,Please guide me what I do wrong or It's not for schedule task at specific time which I set for once. I don't want to keep repeat work after It's done. I'm create just new after finish first OneTimeWorkRequest so It has to work fine as per documented beginUniqueWork.
英语不是我的母语,所以请原谅我在语法上的错误:)
English isn't my native language so pardon me for grammatically mistake:)
推荐答案
您的问题尚不清楚,尽管请将最新的工作管理器库更新为android.arch.work:work-runtime:1.0.0-beta03
You problem is not clear though please update latest work manager lib to android.arch.work:work-runtime:1.0.0-beta03
您可以向我解释为什么您需要征集独特的作品?
And you can explain me why do u need to enque the unique work ?
workManager.beginUniqueWork("simple_work", ExistingWorkPolicy.KEEP, simpleRequest).enqueue();
尝试将其替换为workManager.enque(simpleRequest);
,并让我知道是否可行
try to replace it with workManager.enque(simpleRequest);
and let me know if worked
这篇关于WorkManager OneTimeWorkRequest InitialDelay在设定的时间之后工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!