From f8d4a4949fb03618eaa462095d4418779b09a793 Mon Sep 17 00:00:00 2001 From: Amirhassan Amirmahani <39486543+hasanm08@users.noreply.github.com> Date: Sun, 23 Jul 2023 11:49:51 +0330 Subject: [PATCH] Service already running, using existing service #326 --- .../flutter_background_service/WatchdogReceiver.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/flutter_background_service_android/android/src/main/java/id/flutter/flutter_background_service/WatchdogReceiver.java b/packages/flutter_background_service_android/android/src/main/java/id/flutter/flutter_background_service/WatchdogReceiver.java index b0b64d9d..b0d14a68 100644 --- a/packages/flutter_background_service_android/android/src/main/java/id/flutter/flutter_background_service/WatchdogReceiver.java +++ b/packages/flutter_background_service_android/android/src/main/java/id/flutter/flutter_background_service/WatchdogReceiver.java @@ -55,7 +55,14 @@ public static void remove(Context context) { public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(ACTION_RESPAWN)){ final Config config = new Config(context); - if (!config.isManuallyStopped()) { + var isRunning = false + val manager = context.getSystemService(Context.ACTIVITY_SERVICE) as am + for (service in manager.getRunningServices(Int.MAX_VALUE)) { + if (BackgroundService::class.java.name == service.service.className) { + isRunning = true + } + } + if (!config.isManuallyStopped() && !isRunning) { if (config.isForeground()) { ContextCompat.startForegroundService(context, new Intent(context, BackgroundService.class)); } else {