Merge pull request #670 from jooray/fix/background-service-stops-on-pause
Keep the background service running when the app leaves the screen
This commit is contained in:
2 files changed
+37
-10
No files matched your search
@@ -6,6 +6,27 @@ import '../l10n/locale_tag.dart';
|
|||||||
import '../utils/platform_info.dart';
|
import '../utils/platform_info.dart';
|
||||||
|
|
||||||
class BackgroundService {
|
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;
|
bool _initialized = false;
|
||||||
String? Function()? _languageOverrideProvider;
|
String? Function()? _languageOverrideProvider;
|
||||||
|
|
||||||
@@ -30,16 +51,7 @@ class BackgroundService {
|
|||||||
showNotification: false,
|
showNotification: false,
|
||||||
playSound: false,
|
playSound: false,
|
||||||
),
|
),
|
||||||
foregroundTaskOptions: ForegroundTaskOptions(
|
foregroundTaskOptions: taskOptions,
|
||||||
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,
|
|
||||||
),
|
|
||||||
);
|
);
|
||||||
_initialized = true;
|
_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