From 934affe0191fdfa1662953b3b13f569e07ffbeba Mon Sep 17 00:00:00 2001 From: Kalpesh Patel <1988.kalpesh@gmail.com> Date: Sun, 16 Feb 2020 04:25:16 +0530 Subject: [PATCH] Stopping Foreground Service directly instead of starting the service and then stopping in onStartCommand. (#316) --- .../react/MusicControlEventEmitter.java | 12 ++++--- .../react/MusicControlNotification.java | 32 ++++--------------- 2 files changed, 15 insertions(+), 29 deletions(-) diff --git a/android/src/main/java/com/tanguyantoine/react/MusicControlEventEmitter.java b/android/src/main/java/com/tanguyantoine/react/MusicControlEventEmitter.java index e4ecbeea..aa5372dc 100644 --- a/android/src/main/java/com/tanguyantoine/react/MusicControlEventEmitter.java +++ b/android/src/main/java/com/tanguyantoine/react/MusicControlEventEmitter.java @@ -2,13 +2,16 @@ import android.content.Intent; import android.os.Build; -import androidx.core.content.ContextCompat; import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.WritableMap; import com.facebook.react.modules.core.DeviceEventManagerModule; +import androidx.core.app.NotificationManagerCompat; + +import static com.tanguyantoine.react.MusicControlModule.NOTIFICATION_ID; + public class MusicControlEventEmitter { private static void sendEvent(ReactApplicationContext context, String type, Object value) { WritableMap data = Arguments.createMap(); @@ -79,10 +82,11 @@ public void onVolumeChange(int volume) { } private void stopForegroundService() { + NotificationManagerCompat.from(context).cancel(NOTIFICATION_ID); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - Intent intent = new Intent(context, MusicControlNotification.NotificationService.class); - intent.setAction("StopService"); - ContextCompat.startForegroundService(context, intent); + Intent myIntent = + new Intent(context, MusicControlNotification.NotificationService.class); + context.stopService(myIntent); } } } diff --git a/android/src/main/java/com/tanguyantoine/react/MusicControlNotification.java b/android/src/main/java/com/tanguyantoine/react/MusicControlNotification.java index 94692fdb..edff7cb0 100644 --- a/android/src/main/java/com/tanguyantoine/react/MusicControlNotification.java +++ b/android/src/main/java/com/tanguyantoine/react/MusicControlNotification.java @@ -1,26 +1,22 @@ package com.tanguyantoine.react; import android.app.Notification; -import android.app.NotificationChannel; -import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; -import android.content.Context; import android.content.Intent; import android.content.res.Resources; import android.os.Build; import android.os.IBinder; -import androidx.core.app.NotificationCompat; -import androidx.core.app.NotificationManagerCompat; import android.support.v4.media.session.PlaybackStateCompat; import android.view.KeyEvent; + import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.ReadableMap; import java.util.Map; -import static androidx.core.app.NotificationCompat.PRIORITY_MIN; -import static com.tanguyantoine.react.MusicControlModule.CHANNEL_ID; +import androidx.core.app.NotificationCompat; +import androidx.core.app.NotificationManagerCompat; + import static com.tanguyantoine.react.MusicControlModule.NOTIFICATION_ID; public class MusicControlNotification { @@ -82,7 +78,7 @@ public synchronized void updateActions(long mask, Map options) /** * NOTE: Synchronized since the NotificationService called prepare without a re-entrant lock. - * Other call sites (e.g. show/hide in module) are already synchronized. + * Other call sites (e.g. show/hide in module) are already synchronized. */ public synchronized Notification prepareNotification(NotificationCompat.Builder builder, boolean isPlaying) { // Add the buttons @@ -187,35 +183,22 @@ public IBinder onBind(Intent intent) { return null; } - private boolean isRunning; - private Notification notification; - @Override public void onCreate() { super.onCreate(); - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - notification = MusicControlModule.INSTANCE.notification.prepareNotification(MusicControlModule.INSTANCE.nb, false); - startForeground(NOTIFICATION_ID, notification); - isRunning = true; - } } @Override public int onStartCommand(Intent intent, int flags, int startId) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - if (intent.getAction() != null && intent.getAction().equals("StopService") && notification != null && isRunning) { - stopForeground(true); - isRunning = false; - stopSelf(); - } + Notification notification = MusicControlModule.INSTANCE.notification.prepareNotification(MusicControlModule.INSTANCE.nb, false); + startForeground(NOTIFICATION_ID, notification); } return START_NOT_STICKY; } @Override public void onTaskRemoved(Intent rootIntent) { - isRunning = false; // Destroy the notification and sessions when the task is removed (closed, killed, etc) if (MusicControlModule.INSTANCE != null) { MusicControlModule.INSTANCE.destroy(); @@ -228,7 +211,6 @@ public void onTaskRemoved(Intent rootIntent) { @Override public void onDestroy() { - isRunning = false; if (MusicControlModule.INSTANCE != null) { MusicControlModule.INSTANCE.destroy();