Describe the bug
Calls to enableBackgroundExecution may fail and return false. However, even if that happens, requests to isBackgroundExecutionEnabled will still return true even though the enabling failed.
In consequence, calls to disableBackgroundExecution will fail and crash the app, even though we checked for isBackgroundExecutionEnabled first:
if (!FlutterBackground.isBackgroundExecutionEnabled) {
// Not canceling background processing: Not active
return;
}
FlutterBackground.disableBackgroundExecution();
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1771)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6518)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
To Reproduce
I'm a bit fuzzy here. I have a phone where enabling of background execution fails reliably and in turn causes the described problem. I don't know why it fails, and only on this device (Spectralink Versity 9640, OS: Android 8.1.0), and how to reproduce on other devices.
Expected behavior
Only return true for isBackgroundExecutionEnabled if enabling actually succeeded. Even better: Don't crash on disableBackgroundExecution regardless of enabling state.
Smartphone:
Spectralink Versity 9640
Android 8.1.0
Logs
```
android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1771)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6518)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
```
```
[✓] Flutter (Channel stable, 3.13.3, on macOS 13.5.2 22G91 darwin-x64, locale en-US)
• Flutter version 3.13.3 on channel stable at /Users/matthiasschicker/dev/flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 2524052335 (7 days ago), 2023-09-06 14:32:31 -0700
• Engine revision b8d35810e9
• Dart version 3.1.1
• DevTools version 2.25.0
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
• Android SDK at /Users/matthiasschicker/Library/Android/sdk
• Platform android-34, build-tools 33.0.1
• Java binary at: /Users/matthiasschicker/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 14E300c
• CocoaPods version 1.11.3
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2022.3)
• Android Studio at /Users/matthiasschicker/Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
[✓] VS Code (version 1.82.0)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.72.0
[✓] Connected device (3 available)
• Versity 9640 (mobile) • pwlg08bn3ec00dj • android-arm64 • Android 8.1.0 (API 27)
• macOS (desktop) • macos • darwin-x64 • macOS 13.5.2 22G91 darwin-x64
• Chrome (web) • chrome • web-javascript • Google Chrome 116.0.5845.187
[✓] Network resources
• All expected network resources are available.
• No issues found!
```
Describe the bug Calls to
enableBackgroundExecution
may fail and returnfalse
. However, even if that happens, requests toisBackgroundExecutionEnabled
will still returntrue
even though the enabling failed.In consequence, calls to
disableBackgroundExecution
will fail and crash the app, even though we checked forisBackgroundExecutionEnabled
first:To Reproduce I'm a bit fuzzy here. I have a phone where enabling of background execution fails reliably and in turn causes the described problem. I don't know why it fails, and only on this device (Spectralink Versity 9640, OS: Android 8.1.0), and how to reproduce on other devices.
Expected behavior Only return
true
forisBackgroundExecutionEnabled
if enabling actually succeeded. Even better: Don't crash ondisableBackgroundExecution
regardless of enabling state.Smartphone:
Logs
``` android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground() at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1771) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6518) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) ``` ``` [✓] Flutter (Channel stable, 3.13.3, on macOS 13.5.2 22G91 darwin-x64, locale en-US) • Flutter version 3.13.3 on channel stable at /Users/matthiasschicker/dev/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 2524052335 (7 days ago), 2023-09-06 14:32:31 -0700 • Engine revision b8d35810e9 • Dart version 3.1.1 • DevTools version 2.25.0 [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1) • Android SDK at /Users/matthiasschicker/Library/Android/sdk • Platform android-34, build-tools 33.0.1 • Java binary at: /Users/matthiasschicker/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231) • All Android licenses accepted. [✓] Xcode - develop for iOS and macOS (Xcode 14.3.1) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 14E300c • CocoaPods version 1.11.3 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2022.3) • Android Studio at /Users/matthiasschicker/Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231) [✓] VS Code (version 1.82.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.72.0 [✓] Connected device (3 available) • Versity 9640 (mobile) • pwlg08bn3ec00dj • android-arm64 • Android 8.1.0 (API 27) • macOS (desktop) • macos • darwin-x64 • macOS 13.5.2 22G91 darwin-x64 • Chrome (web) • chrome • web-javascript • Google Chrome 116.0.5845.187 [✓] Network resources • All expected network resources are available. • No issues found! ```