Keep the background service running when the app leaves the screen
flutter_foreground_task's stopWithTask option (9.2 to 11.0) does not wait for the task to be removed: it registers activity lifecycle callbacks and stops the service on the first onActivityPaused with no resumed activity. So the "MeshCore background" service stopped every time the app went to the background or the screen turned off. Android then cached and froze the process, the Bluetooth link dropped and the low-memory killer ended the app a few hours later. Usage stats on a Pixel show FOREGROUND_SERVICE_STOP in the same second as every ACTIVITY_STOPPED. Drop the option. android:stopWithTask="true" in the manifest still stops the service when the app is swiped away, which is what the option was meant to do.
This commit is contained in:
1 parent
34ea0cb7ca
commit
7bb9def628
2 files changed
+37
-10
No files matched your search
@@ -6,6 +6,27 @@ import '../l10n/locale_tag.dart';
|
||||
import '../utils/platform_info.dart';
|
||||
|
||||
class BackgroundService {
|
||||
/// Options for the service that keeps the radio link alive in the
|
||||
/// background.
|
||||
///
|
||||
/// The radio link lives in the app process; once the user swipes the app
|
||||
/// away it is gone, so a lingering "connected" notification (or an
|
||||
/// automatic restart) would only be misleading. Swiping away stops the
|
||||
/// service through `android:stopWithTask="true"` in AndroidManifest.xml.
|
||||
///
|
||||
/// `stopWithTask` must stay unset here: in flutter_foreground_task (9.2 to
|
||||
/// 11.0 at least) the option does not watch the task but stops the service
|
||||
/// as soon as no activity is resumed, which is every time the app goes to
|
||||
/// the background or the screen turns off. Android then caches and freezes
|
||||
/// the process, the Bluetooth link drops and the low-memory killer ends it.
|
||||
@visibleForTesting
|
||||
static final ForegroundTaskOptions taskOptions = ForegroundTaskOptions(
|
||||
eventAction: ForegroundTaskEventAction.repeat(5000),
|
||||
autoRunOnBoot: false,
|
||||
allowWifiLock: false,
|
||||
allowAutoRestart: false,
|
||||
);
|
||||
|
||||
bool _initialized = false;
|
||||
String? Function()? _languageOverrideProvider;
|
||||
|
||||
@@ -30,16 +51,7 @@ class BackgroundService {
|
||||
showNotification: false,
|
||||
playSound: false,
|
||||
),
|
||||
foregroundTaskOptions: ForegroundTaskOptions(
|
||||
eventAction: ForegroundTaskEventAction.repeat(5000),
|
||||
autoRunOnBoot: false,
|
||||
allowWifiLock: false,
|
||||
// The radio link lives in the app process; once the user swipes the
|
||||
// app away it is gone, so a lingering "connected" notification (or
|
||||
// an automatic restart) would only be misleading.
|
||||
allowAutoRestart: false,
|
||||
stopWithTask: true,
|
||||
),
|
||||
foregroundTaskOptions: taskOptions,
|
||||
);
|
||||
_initialized = true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:meshcore_open/services/background_service.dart';
|
||||
|
||||
void main() {
|
||||
test('the foreground service does not stop when the app goes to the '
|
||||
'background', () {
|
||||
// flutter_foreground_task's stopWithTask option stops the service on the
|
||||
// first activity pause, not on task removal. Swipe-away is handled by
|
||||
// android:stopWithTask in the manifest instead.
|
||||
final options = BackgroundService.taskOptions;
|
||||
expect(options.stopWithTask, isNull);
|
||||
expect(options.toJson().containsKey('stopWithTask'), isFalse);
|
||||
expect(options.allowAutoRestart, isFalse);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user