diff --git a/src/ch/blinkenlights/android/vanilla/MediaButtonReceiver.java b/src/ch/blinkenlights/android/vanilla/MediaButtonReceiver.java index 41c2ff71..a4ef5a2f 100644 --- a/src/ch/blinkenlights/android/vanilla/MediaButtonReceiver.java +++ b/src/ch/blinkenlights/android/vanilla/MediaButtonReceiver.java @@ -186,15 +186,11 @@ public class MediaButtonReceiver extends BroadcastReceiver { if (act == null) return; - Intent intent = new Intent(context, PlaybackService.class).setAction(act); + Intent intent = new Intent(context, PlaybackService.class) + .setAction(act) + .putExtra(PlaybackService.EXTRA_EARLY_NOTIFICATION, true); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - // Since API 26, we can not use startService anymore if the service - // was not launched. startForegroundService() expects the service - // to call startForeground() within 5 sec, which is PROBABLY okay as - // any key event will start playing a song - probably. - // If this fails, we could still try the chromium hack and have - // PlaybackService:onCreate() start a fake notification. - // https://chromium.googlesource.com/chromium/src/+/81033db0364139db8ee059623d8892f2b00a39ba context.startForegroundService(intent); } else { context.startService(intent); diff --git a/src/ch/blinkenlights/android/vanilla/PlaybackService.java b/src/ch/blinkenlights/android/vanilla/PlaybackService.java index edc1fdfc..cfe45a57 100644 --- a/src/ch/blinkenlights/android/vanilla/PlaybackService.java +++ b/src/ch/blinkenlights/android/vanilla/PlaybackService.java @@ -177,6 +177,10 @@ public final class PlaybackService extends Service * Pause music and hide the notifcation. */ public static final String ACTION_CLOSE_NOTIFICATION = "ch.blinkenlights.android.vanilla.CLOSE_NOTIFICATION"; + /** + * Whether we should create a foreground notification as early as possible. + */ + public static final String EXTRA_EARLY_NOTIFICATION = "extra_early_notification"; /** * Visibility modes of the notification. @@ -515,10 +519,15 @@ public final class PlaybackService extends Service } @Override - public int onStartCommand(Intent intent, int flags, int startId) - { + public int onStartCommand(Intent intent, int flags, int startId) { if (intent != null) { - String action = intent.getAction(); + final String action = intent.getAction(); + final boolean earlyNotification = intent.hasExtra(EXTRA_EARLY_NOTIFICATION); + + if (earlyNotification) { + Song song = mCurrentSong != null ? mCurrentSong : new Song(-1); + startForeground(NOTIFICATION_ID, createNotification(song, mState, VISIBILITY_WHEN_PLAYING)); + } if (ACTION_TOGGLE_PLAYBACK.equals(action)) { playPause();