Skip to content
This repository has been archived by the owner on Oct 6, 2022. It is now read-only.

Commit

Permalink
Stopping Foreground Service directly instead of starting the service …
Browse files Browse the repository at this point in the history
…and then stopping in onStartCommand. (#316)
  • Loading branch information
kalpeshp0310 authored Feb 15, 2020
1 parent 074a69d commit 934affe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -82,7 +78,7 @@ public synchronized void updateActions(long mask, Map<String, Integer> 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
Expand Down Expand Up @@ -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();
Expand All @@ -228,7 +211,6 @@ public void onTaskRemoved(Intent rootIntent) {

@Override
public void onDestroy() {
isRunning = false;

if (MusicControlModule.INSTANCE != null) {
MusicControlModule.INSTANCE.destroy();
Expand Down

0 comments on commit 934affe

Please sign in to comment.