How to cancel Work from WorkManager Android?
I have saved the work UUID converted to String in Realm.
Here is the code -
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();
}
Now I'm trying to cancel the Work using this line of code but didn't work.
WorkManager.getInstance().cancelWorkById(UUID.fromString(downloadStatusModel.getDownloadReferenceId()));
Any help would be appreciated
Thanks
|
show 2 more comments
I have saved the work UUID converted to String in Realm.
Here is the code -
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();
}
Now I'm trying to cancel the Work using this line of code but didn't work.
WorkManager.getInstance().cancelWorkById(UUID.fromString(downloadStatusModel.getDownloadReferenceId()));
Any help would be appreciated
Thanks
Try cancelling work by tagcancelAllWorkByTagand provide same tag to your worker. But remember that it works if your work is unfinished.
– Jeel Vankhede
Nov 23 '18 at 5:42
Thanks for your comment. Will it cancel my all works that I have enqueued?
– Al-Amin
Nov 23 '18 at 5:44
Yes !, it would. May be your issue is not with UUID but it's already finished (Just guessing).
– Jeel Vankhede
Nov 23 '18 at 5:45
No luck. cancelAllWorkByTag not working. My work is not finished also. I have debugged that.
– Al-Amin
Nov 23 '18 at 6:31
1
What do you mean 'it didn't work'? How are you checking that it wasn't cancelled? Does yourWorkerget a callback toonStopped()?
– ianhanniballake
Nov 23 '18 at 8:20
|
show 2 more comments
I have saved the work UUID converted to String in Realm.
Here is the code -
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();
}
Now I'm trying to cancel the Work using this line of code but didn't work.
WorkManager.getInstance().cancelWorkById(UUID.fromString(downloadStatusModel.getDownloadReferenceId()));
Any help would be appreciated
Thanks
I have saved the work UUID converted to String in Realm.
Here is the code -
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();
}
Now I'm trying to cancel the Work using this line of code but didn't work.
WorkManager.getInstance().cancelWorkById(UUID.fromString(downloadStatusModel.getDownloadReferenceId()));
Any help would be appreciated
Thanks
asked Nov 23 '18 at 5:34
Al-Amin
422313
422313
Try cancelling work by tagcancelAllWorkByTagand provide same tag to your worker. But remember that it works if your work is unfinished.
– Jeel Vankhede
Nov 23 '18 at 5:42
Thanks for your comment. Will it cancel my all works that I have enqueued?
– Al-Amin
Nov 23 '18 at 5:44
Yes !, it would. May be your issue is not with UUID but it's already finished (Just guessing).
– Jeel Vankhede
Nov 23 '18 at 5:45
No luck. cancelAllWorkByTag not working. My work is not finished also. I have debugged that.
– Al-Amin
Nov 23 '18 at 6:31
1
What do you mean 'it didn't work'? How are you checking that it wasn't cancelled? Does yourWorkerget a callback toonStopped()?
– ianhanniballake
Nov 23 '18 at 8:20
|
show 2 more comments
Try cancelling work by tagcancelAllWorkByTagand provide same tag to your worker. But remember that it works if your work is unfinished.
– Jeel Vankhede
Nov 23 '18 at 5:42
Thanks for your comment. Will it cancel my all works that I have enqueued?
– Al-Amin
Nov 23 '18 at 5:44
Yes !, it would. May be your issue is not with UUID but it's already finished (Just guessing).
– Jeel Vankhede
Nov 23 '18 at 5:45
No luck. cancelAllWorkByTag not working. My work is not finished also. I have debugged that.
– Al-Amin
Nov 23 '18 at 6:31
1
What do you mean 'it didn't work'? How are you checking that it wasn't cancelled? Does yourWorkerget a callback toonStopped()?
– ianhanniballake
Nov 23 '18 at 8:20
Try cancelling work by tag
cancelAllWorkByTag and provide same tag to your worker. But remember that it works if your work is unfinished.– Jeel Vankhede
Nov 23 '18 at 5:42
Try cancelling work by tag
cancelAllWorkByTag and provide same tag to your worker. But remember that it works if your work is unfinished.– Jeel Vankhede
Nov 23 '18 at 5:42
Thanks for your comment. Will it cancel my all works that I have enqueued?
– Al-Amin
Nov 23 '18 at 5:44
Thanks for your comment. Will it cancel my all works that I have enqueued?
– Al-Amin
Nov 23 '18 at 5:44
Yes !, it would. May be your issue is not with UUID but it's already finished (Just guessing).
– Jeel Vankhede
Nov 23 '18 at 5:45
Yes !, it would. May be your issue is not with UUID but it's already finished (Just guessing).
– Jeel Vankhede
Nov 23 '18 at 5:45
No luck. cancelAllWorkByTag not working. My work is not finished also. I have debugged that.
– Al-Amin
Nov 23 '18 at 6:31
No luck. cancelAllWorkByTag not working. My work is not finished also. I have debugged that.
– Al-Amin
Nov 23 '18 at 6:31
1
1
What do you mean 'it didn't work'? How are you checking that it wasn't cancelled? Does your
Worker get a callback to onStopped()?– ianhanniballake
Nov 23 '18 at 8:20
What do you mean 'it didn't work'? How are you checking that it wasn't cancelled? Does your
Worker get a callback to onStopped()?– ianhanniballake
Nov 23 '18 at 8:20
|
show 2 more comments
1 Answer
1
active
oldest
votes
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441122%2fhow-to-cancel-work-from-workmanager-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 23 '18 at 8:33
ianhanniballake
103k14214227
103k14214227
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53441122%2fhow-to-cancel-work-from-workmanager-android%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try cancelling work by tag
cancelAllWorkByTagand provide same tag to your worker. But remember that it works if your work is unfinished.– Jeel Vankhede
Nov 23 '18 at 5:42
Thanks for your comment. Will it cancel my all works that I have enqueued?
– Al-Amin
Nov 23 '18 at 5:44
Yes !, it would. May be your issue is not with UUID but it's already finished (Just guessing).
– Jeel Vankhede
Nov 23 '18 at 5:45
No luck. cancelAllWorkByTag not working. My work is not finished also. I have debugged that.
– Al-Amin
Nov 23 '18 at 6:31
1
What do you mean 'it didn't work'? How are you checking that it wasn't cancelled? Does your
Workerget a callback toonStopped()?– ianhanniballake
Nov 23 '18 at 8:20