如何在Android中从WorkManager取消工作? [英] How to cancel Work from WorkManager in Android?

查看:657
本文介绍了如何在Android中从WorkManager取消工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将转换为String的WorkManager UUID保存在Realm数据库中.

I have saved the WorkManager UUID converted to String in Realm database.

这是代码-

Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build();
            Data inputData = new Data.Builder().putString("downloadUrl", downloadUrl).
                    putString("destinationFolder", destinationFolder).
                    putInt("suraNumber", Integer.parseInt(suraNumber)).
                    putString("fileName", fileName).
                    putBoolean("downloadFileTypeBangla", downloadFileTypeBangla).
                    putBoolean("downloadFileTypeArabic", downloadFileTypeArabic).
                    putBoolean("downloadFileTypeArabicWithBangla", downloadFileTypeArabicWithBangla).build();
            OneTimeWorkRequest downloadWork = new OneTimeWorkRequest.Builder(DownloadWorker.class).setConstraints(constraints).setInputData(inputData).build();
            WorkManager.getInstance().enqueue(downloadWork);

            Sura sura = dbOperations.getSuraById(Integer.parseInt(suraNumber));
            if(sura != null){
                dbOperations.updateSura(sura, Integer.parseInt(suraNumber), sura.getBnAudioDownloadStatus(), sura.getArAudioDownloadStatus(), 1);
                realm.beginTransaction();
                DownloadStatusModel downloadStatusModel = new DownloadStatusModel();
                downloadStatusModel.setId(new RealmCommonService(realm).newId(DownloadStatusModel.class));
                downloadStatusModel.setDownloadFileType("ArabicWithBangla");
                downloadStatusModel.setActiveStatus(true);
                downloadStatusModel.setDownloadDate(new Date());
                downloadStatusModel.setDownloadedSuraNo(sura.getSuraNo());
                downloadStatusModel.setDownloadFileSize(sura.getArBnAudioFileSize());
                downloadStatusModel.setDownloadReferenceId(downloadWork.getId().toString());
                downloadStatusModel.setDownloadedSuraNameBangla(sura.getSuraNameBangla());
                downloadStatusModel.setDownloadStatus(1);
                realm.copyToRealm(downloadStatusModel);
                realm.commitTransaction();
            }

现在,我正在尝试使用此代码行取消Work,但是没有用.

Now I'm trying to cancel the Work using this line of code but didn't work.

WorkManager.getInstance().cancelWorkById(UUID.fromString(downloadStatusModel.getDownloadReferenceId()));

任何帮助将不胜感激

谢谢

推荐答案

如果您使用的是Worker,则需要覆盖 isStopped() 检查取消.

If you're using Worker, you need to override the onStopped() method and use that as the signal for your worker to cancel its ongoing work. Within your doWork() method, you can also use isStopped() to check for cancellation.

这篇关于如何在Android中从WorkManager取消工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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