WorkManager.getInstance().cancelAllWorkByTag() 不停止定期作业 [英] WorkManager.getInstance().cancelAllWorkByTag() not stopping the periodic job

查看:39
本文介绍了WorkManager.getInstance().cancelAllWorkByTag() 不停止定期作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于定期任务,我将工作经理用作:

for periodic task I am using work manager as:

PeriodicWorkRequest.Builder wifiWorkBuilder =
                            new PeriodicWorkRequest.Builder(FileUpload.class, 15,
                                    TimeUnit.MINUTES)
                                    .setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.METERED).build());
                    PeriodicWorkRequest wifiWork = wifiWorkBuilder.build();
                    WorkManager.getInstance().enqueueUniquePeriodicWork("wifiJob", ExistingPeriodicWorkPolicy.REPLACE, wifiWork);

我在该活动中有一项活动,如果用户取消选中复选框,那么我想停止这项工作.所以我要停止工作:

I have one activity in that activity one check box is there if user unselect checkbox then I want to stop this job. So I am stopping job like:

WorkManager.getInstance().cancelAllWorkByTag("wifiJob");

但是在停止工作后,我的任务也在执行.以前我认为可能会执行下一个作业,然后它会停止,但在过去 1 小时内它执行了 4 次,但作业仍在后台运行.停止工作的其他方法或正确方法是什么.

But after stopping job also my task is executing. Previously I thought might be next job will be executed and after that it will stop but in last 1 hour it executed 4 times and still job is running in background. What is other way or correct way to stop job.

根据文档:

用给定的标签取消所有未完成的工作.请注意,取消是一种尽力而为的政策,已经在执行的工作可能会继续运行.

Cancels all unfinished work with the given tag. Note that cancellation is a best-effort policy and work that is already executing may continue to run.

这是什么意思 - 取消是一种尽力而为的策略,已经在执行的工作可能会继续运行.那么阻止这种情况的正确方法是什么

What is the meaning of this - cancellation is a best-effort policy and work that is already executing may continue to run. So what is the correct way to stop this

我正在使用版本 implementation "android.arch.work:work-runtime:1.0.0-alpha08"提前致谢

I am using version implementation "android.arch.work:work-runtime:1.0.0-alpha08" Thanks in advance

推荐答案

在创建 PeriodicWorkRequest 时,我添加了 addTag() 并取消使用相同标签名称的任务,使用它取消挂起的工作.所以我的代码是这样的:

While creating PeriodicWorkRequest i had added addTag() and to cancel task used same tag name, using this it cancel pending work. So my code is like:

PeriodicWorkRequest.Builder wifiWorkBuilder =
                            new PeriodicWorkRequest.Builder(FileUpload.class, 15,
                                    TimeUnit.MINUTES)
                                    .addTag("WIFIJOB1")
                                    .setConstraints(new Constraints.Builder().setRequiredNetworkType(NetworkType.METERED).build());
                    wifiWork = wifiWorkBuilder.build();
                    WorkManager.getInstance().enqueueUniquePeriodicWork("wifiJob", ExistingPeriodicWorkPolicy.REPLACE, wifiWork);

停止我正在使用的作业:

To stop job I am using:

WorkManager.getInstance().cancelAllWorkByTag("WIFIJOB1");

这篇关于WorkManager.getInstance().cancelAllWorkByTag() 不停止定期作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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