π[Android, Task was cancelled in periodic run]
- [x] I have read the README
- [x] I have done the setup for Android
- [ ] I have ran the sample app and it does not work there
Version
| Technology | Version |
|---|---|
| Workmanager version | 0.2.2 |
Describe the error
I run the code on Android (periodic), but it works for the first time only, all further runs failed with an error:
My code:
void backgroundTrackerInitialize() {
Workmanager.cancelAll();
Workmanager.initialize(callbackDispatcher, isInDebugMode: true);
Workmanager.registerPeriodicTask(
"1",
"refreshLocationAndNetworkStatus",
frequency: Duration(minutes: 15)
);
}
void callbackDispatcher() {
Workmanager.executeTask((task, parameters) async {
print('task:getposition');
print('task:finished');
return Future.value(true);
});
}

Error output:
I/WM-WorkerWrapper( 7011): java.util.concurrent.CancellationException: Task was cancelled.
I/WM-WorkerWrapper( 7011): at androidx.work.impl.utils.futures.AbstractFuture.cancellationExceptionWithCause(AbstractFuture.java:1184)
I/WM-WorkerWrapper( 7011): at androidx.work.impl.utils.futures.AbstractFuture.getDoneValue(AbstractFuture.java:514)
I/WM-WorkerWrapper( 7011): at androidx.work.impl.utils.futures.AbstractFuture.get(AbstractFuture.java:475)
I/WM-WorkerWrapper( 7011): at androidx.work.impl.WorkerWrapper$2.run(WorkerWrapper.java:284)
I/WM-WorkerWrapper( 7011): at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
I/WM-WorkerWrapper( 7011): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
I/WM-WorkerWrapper( 7011): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
I/WM-WorkerWrapper( 7011): at java.lang.Thread.run(Thread.java:923)
In example project all work fine, but if I copy/paste code from example project to my project I get same error. Any ideas?
Output of flutter doctor -v
[β] Flutter (Channel stable, v1.17.3, on Microsoft Windows [Version 10.0.18363.900], locale en-US)
β’ Flutter version 1.17.3 at C:\ide\flutter
β’ Framework revision b041144f83 (7 days ago), 2020-06-04 09:26:11 -0700
β’ Engine revision ee76268252
β’ Dart version 2.8.4
[β] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
β’ Android SDK at C:\Users\truer\AppData\Local\Android\Sdk
β’ Platform android-29, build-tools 29.0.3
β’ ANDROID_HOME = C:\Users\truer\AppData\Local\Android\Sdk
β’ Java binary at: C:\ide\androidstudio\jre\bin\java
β’ Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
β’ All Android licenses accepted.
[β] Android Studio (version 3.6)
β’ Android Studio at C:\ide\androidstudio
β’ Flutter plugin version 44.0.2
β’ Dart plugin version 192.7761
β’ Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
[β] Connected device (1 available)
β’ sdk gphone x86 arm β’ emulator-5554 β’ android-x86 β’ Android 10 (API 29) (emulator)
Why do you return Future.value(true)? You already have the async keyword in your executeTask handler, so just returning true should be enough. Alternatively, just remove the async keyword. This could do the trick.
@gebsl I checked with async and without. The error is still there. I think something is wrong with the configuration of the Android project, but I donβt understand where exactly.
Strange.
Have you checked this guide?
Is callbackDispatcher a real top level function (i had problems while callbackDispatcher was a public static function within a class)?
Does it print your debug messages in console?
What I also noticed: The name of your periodic task is refreshLocationAndNetworkStatus but the screenshot you added states task name is simplePeriodicTask. Is there additional code registering this task?
Have you checked this guide? Is
callbackDispatchera real top level function (i had problems whilecallbackDispatcherwas a public static function within a class)? Does it print your debug messages in console?
I checked the guide and my project has tag metadata from it. In console only error Task was cancelled.
What I also noticed: The name of your periodic task is
refreshLocationAndNetworkStatusbut the screenshot you added states task name issimplePeriodicTask. Is there additional code registering this task?
I copy this file to my project for tests. The screenshot was taken already when I check the code from the example, but in my code error was same but with name refreshLocationAndNetworkStatus.
I meet same error..
i meet same error too. ):
Have you checked the status of your workmanager tasks using adb shell dumpsys jobscheduler?
I've found that it provides some useful information from time to time ;)
I am having similar issue too
Have you checked the output of adb shell dumpsys jobscheduler?
Have you checked the output of
adb shell dumpsys jobscheduler? yeah, nothing helpful
Hi guys, what about the status of this issue? What is the chance this issue will be fixed?
I don't think this is a problem of flutter_workmanager. Example app works fine. I have never experienced this issue.
Does anyone know how to resolve this problem? I notice that on one device app work properly but on another, I get this error :/
Getting same error. any fix??
I had this problem on my sansung A21s, turns out when i have on power mode (optimize battery usage) it limits background activity. turning it off allowed my app to continue wokring in the background.
is there any way around this?
I had this problem on my sansung A21s, turns out when i have on power mode (optimize battery usage) it limits background activity. turning it off allowed my app to continue wokring in the background.
is there any way around this?
Maybe you can try to request the permission to ignore battery optimization.
I use permission_handler: Permission.ignoreBatteryOptimizations.request()
Please open a new issue if you encounter the problem still.
Please what is the solution to this?
For those having this error: for me it was the return type of the code in executeTask. If use async I need to return only true (not encapsulated in a Future).