From 6fedeed253d2648d876b9a2e3e5a30967934a81a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berke=20Emin=20Kabag=C3=B6z?= Date: Sat, 5 Aug 2023 21:08:21 +0300 Subject: [PATCH] Fix: Issue created by #336 --- .../BackgroundService.java | 5 ----- .../WatchdogReceiver.java | 17 ++++++++++------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/packages/flutter_background_service_android/android/src/main/java/id/flutter/flutter_background_service/BackgroundService.java b/packages/flutter_background_service_android/android/src/main/java/id/flutter/flutter_background_service/BackgroundService.java index 3a8f8ecc..681a1b8e 100644 --- a/packages/flutter_background_service_android/android/src/main/java/id/flutter/flutter_background_service/BackgroundService.java +++ b/packages/flutter_background_service_android/android/src/main/java/id/flutter/flutter_background_service/BackgroundService.java @@ -181,11 +181,6 @@ public int onStartCommand(Intent intent, int flags, int startId) { @SuppressLint("WakelockTimeout") private void runService() { try { - if (isRunning.get() || (backgroundEngine != null && !backgroundEngine.getDartExecutor().isExecutingDart())) { - Log.v(TAG, "Service already running, using existing service"); - return; - } - Log.v(TAG, "Starting flutter engine for background service"); getLock(getApplicationContext()).acquire(); 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 b0d14a68..fa06fd9b 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 @@ -6,6 +6,7 @@ import android.app.AlarmManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; +import android.app.ActivityManager; import android.content.Context; import android.content.Intent; import android.os.Build; @@ -53,16 +54,18 @@ public static void remove(Context context) { @Override public void onReceive(Context context, Intent intent) { - if (intent.getAction().equals(ACTION_RESPAWN)){ + if (intent.getAction().equals(ACTION_RESPAWN)) { final Config config = new Config(context); - 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 + boolean isRunning = false; + + ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); + for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) { + if (BackgroundService.class.getName().equals(service.service.getClassName())) { + isRunning = true; } } - if (!config.isManuallyStopped() && !isRunning) { + + if (!config.isManuallyStopped() && !isRunning) { if (config.isForeground()) { ContextCompat.startForegroundService(context, new Intent(context, BackgroundService.class)); } else {