flutter_workmanager icon indicating copy to clipboard operation
flutter_workmanager copied to clipboard

🐞[Android, Task was cancelled in periodic run]

Open trueromanus opened this issue 6 years ago β€’ 20 comments

  • [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);
  });
}

image

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)

trueromanus avatar Jun 12 '20 09:06 trueromanus

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 avatar Jun 14 '20 12:06 gebsl

@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.

trueromanus avatar Jun 14 '20 12:06 trueromanus

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?

gebsl avatar Jun 14 '20 12:06 gebsl

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?

gebsl avatar Jun 14 '20 12:06 gebsl

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?

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 refreshLocationAndNetworkStatus but the screenshot you added states task name is simplePeriodicTask. 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.

trueromanus avatar Jun 14 '20 19:06 trueromanus

I meet same error..

zhangxinagjunHJH avatar Jun 17 '20 01:06 zhangxinagjunHJH

i meet same error too. ):

yakupbaser avatar Jul 03 '20 09:07 yakupbaser

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 ;)

gebsl avatar Jul 03 '20 10:07 gebsl

I am having similar issue too

manuelemeka avatar Aug 01 '20 16:08 manuelemeka

Have you checked the output of adb shell dumpsys jobscheduler?

gebsl avatar Aug 01 '20 16:08 gebsl

Have you checked the output of adb shell dumpsys jobscheduler? yeah, nothing helpful

manuelemeka avatar Aug 01 '20 16:08 manuelemeka

Hi guys, what about the status of this issue? What is the chance this issue will be fixed?

trueromanus avatar Aug 13 '20 09:08 trueromanus

I don't think this is a problem of flutter_workmanager. Example app works fine. I have never experienced this issue.

gebsl avatar Aug 13 '20 10:08 gebsl

Does anyone know how to resolve this problem? I notice that on one device app work properly but on another, I get this error :/

MarcinKrz1 avatar Dec 10 '20 21:12 MarcinKrz1

Getting same error. any fix??

S7071 avatar Jan 14 '21 06:01 S7071

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?

willroyMyles avatar Mar 29 '21 01:03 willroyMyles

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()

PaoloP98 avatar Mar 29 '21 04:03 PaoloP98

Please open a new issue if you encounter the problem still.

ened avatar Jan 31 '22 07:01 ened

Please what is the solution to this?

codemicah avatar Apr 16 '23 11:04 codemicah

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).

janhelwich avatar Jul 30 '23 14:07 janhelwich