From 60d213dc9c0f3e81ee4df84b12b1d3c85a667c23 Mon Sep 17 00:00:00 2001 From: Meliksah Cakir Date: Wed, 25 Feb 2026 12:11:47 +0100 Subject: [PATCH] fix: DidNotStartInTimeException & DidNotStopInTimeException --- .../service/ForegroundServiceManager.kt | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/android/src/main/kotlin/com/pravera/flutter_foreground_task/service/ForegroundServiceManager.kt b/android/src/main/kotlin/com/pravera/flutter_foreground_task/service/ForegroundServiceManager.kt index 18ad0df..70cb0a2 100644 --- a/android/src/main/kotlin/com/pravera/flutter_foreground_task/service/ForegroundServiceManager.kt +++ b/android/src/main/kotlin/com/pravera/flutter_foreground_task/service/ForegroundServiceManager.kt @@ -60,7 +60,10 @@ class ForegroundServiceManager { ForegroundTaskOptions.updateData(context, argsMap) ForegroundTaskData.updateData(context, argsMap) NotificationContent.updateData(context, argsMap) - ContextCompat.startForegroundService(context, nIntent) + // Use startService instead of startForegroundService because the service + // is already running in the foreground and we only need to deliver the + // update command. This avoids the startForeground() contract requirement. + context.startService(nIntent) } /** Stop the foreground service. */ @@ -76,7 +79,10 @@ class ForegroundServiceManager { ForegroundTaskOptions.clearData(context) ForegroundTaskData.clearData(context) NotificationContent.clearData(context) - ContextCompat.startForegroundService(context, nIntent) + // Use startService instead of startForegroundService because the service + // is already running in the foreground and we only need to deliver the + // stop command. This avoids the startForeground() contract requirement. + context.startService(nIntent) } /** Send data to TaskHandler. */