diff --git a/starboard/android/apk/app/src/main/java/dev/cobalt/coat/MediaPlaybackService.java b/starboard/android/apk/app/src/main/java/dev/cobalt/coat/MediaPlaybackService.java index 4cfd607fbf0a..16518daa3059 100644 --- a/starboard/android/apk/app/src/main/java/dev/cobalt/coat/MediaPlaybackService.java +++ b/starboard/android/apk/app/src/main/java/dev/cobalt/coat/MediaPlaybackService.java @@ -35,7 +35,6 @@ public class MediaPlaybackService extends Service { private static final int NOTIFICATION_ID = 193266736; // CL number for uniqueness. private static final String NOTIFICATION_CHANNEL_ID = "dev.cobalt.coat media playback service"; private static final String NOTIFICATION_CHANNEL_NAME = "Media playback service"; - private Context context; @Override public void onCreate() { @@ -46,7 +45,6 @@ public void onCreate() { return; } getStarboardBridge().onServiceStart(this); - context = getApplicationContext(); } @Override @@ -70,7 +68,6 @@ public void onDestroy() { return; } getStarboardBridge().onServiceDestroy(this); - context = null; super.onDestroy(); Log.i(TAG, "Destroying the Media playback service."); } @@ -92,7 +89,7 @@ public void stopService() { private void hideNotification() { Log.i(TAG, "Hiding notification after stopped the service"); NotificationManager notificationManager = - (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.cancel(NOTIFICATION_ID); } @@ -105,7 +102,7 @@ private void createChannel() { @RequiresApi(26) private void createChannelInternalV26() { NotificationManager notificationManager = - (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); NotificationChannel channel = new NotificationChannel( NOTIFICATION_CHANNEL_ID, @@ -115,7 +112,7 @@ private void createChannelInternalV26() { try { notificationManager.createNotificationChannel(channel); } catch (IllegalArgumentException e) { - + // intentional empty. } } @@ -128,13 +125,13 @@ public void deleteChannel() { @RequiresApi(26) private void deleteChannelInternalV26() { NotificationManager notificationManager = - (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.deleteNotificationChannel(NOTIFICATION_CHANNEL_ID); } Notification buildNotification() { NotificationCompat.Builder builder = - new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID) + new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID) .setShowWhen(false) .setPriority(NotificationCompat.PRIORITY_MIN) .setSmallIcon(android.R.drawable.stat_sys_warning) diff --git a/starboard/android/apk/app/src/main/java/dev/cobalt/coat/StarboardBridge.java b/starboard/android/apk/app/src/main/java/dev/cobalt/coat/StarboardBridge.java index 843579c8365f..455f47bab7ec 100644 --- a/starboard/android/apk/app/src/main/java/dev/cobalt/coat/StarboardBridge.java +++ b/starboard/android/apk/app/src/main/java/dev/cobalt/coat/StarboardBridge.java @@ -171,6 +171,10 @@ protected void onServiceDestroy(Service service) { protected void startMediaPlaybackService() { Service service = serviceHolder.get(); if (service == null) { + if (appContext == null) { + Log.w(TAG, "Activiy already destoryed."); + return; + } Log.i(TAG, "Cold start - Instantiating a MediaPlaybackService."); Intent intent = new Intent(appContext, MediaPlaybackService.class); appContext.startService(intent);