Don't init the foreground service unless it is needed (#2437)

This commit is contained in:
Imran Remtulla
2025-08-01 11:56:53 -04:00
parent 71bf23a110
commit 69904265c9

View File

@@ -185,7 +185,6 @@ class _ObtainiumState extends State<Obtainium> {
initPlatformState(); initPlatformState();
WidgetsBinding.instance.addPostFrameCallback((_) { WidgetsBinding.instance.addPostFrameCallback((_) {
requestNonOptionalPermissions(); requestNonOptionalPermissions();
initForegroundService();
}); });
} }
@@ -201,28 +200,32 @@ class _ObtainiumState extends State<Obtainium> {
} }
void initForegroundService() { void initForegroundService() {
FlutterForegroundTask.init( // ignore: invalid_use_of_visible_for_testing_member
androidNotificationOptions: AndroidNotificationOptions( if (!FlutterForegroundTask.isInitialized) {
channelId: 'bg_update', FlutterForegroundTask.init(
channelName: tr('foregroundService'), androidNotificationOptions: AndroidNotificationOptions(
channelDescription: tr('foregroundService'), channelId: 'bg_update',
onlyAlertOnce: true, channelName: tr('foregroundService'),
), channelDescription: tr('foregroundService'),
iosNotificationOptions: const IOSNotificationOptions( onlyAlertOnce: true,
showNotification: false, ),
playSound: false, iosNotificationOptions: const IOSNotificationOptions(
), showNotification: false,
foregroundTaskOptions: ForegroundTaskOptions( playSound: false,
eventAction: ForegroundTaskEventAction.repeat(900000), ),
autoRunOnBoot: true, foregroundTaskOptions: ForegroundTaskOptions(
autoRunOnMyPackageReplaced: true, eventAction: ForegroundTaskEventAction.repeat(900000),
allowWakeLock: true, autoRunOnBoot: true,
allowWifiLock: true, autoRunOnMyPackageReplaced: true,
), allowWakeLock: true,
); allowWifiLock: true,
),
);
}
} }
Future<ServiceRequestResult?> startForegroundService(bool restart) async { Future<ServiceRequestResult?> startForegroundService(bool restart) async {
initForegroundService();
if (await FlutterForegroundTask.isRunningService) { if (await FlutterForegroundTask.isRunningService) {
if (restart) { if (restart) {
return FlutterForegroundTask.restartService(); return FlutterForegroundTask.restartService();