From a364b5b8854117b42f57c014d6b616b5199ccb46 Mon Sep 17 00:00:00 2001 From: Adrian Ulrich Date: Wed, 6 Sep 2017 21:55:14 +0200 Subject: [PATCH] Use ShortcutPseudoActivity for headset events. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This works on Android 8 without calling startForegroundService() - ¯\_(ツ)_/¯ --- .../android/vanilla/MediaButtonReceiver.java | 5 ++--- .../vanilla/ShortcutPseudoActivity.java | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/ch/blinkenlights/android/vanilla/MediaButtonReceiver.java b/src/ch/blinkenlights/android/vanilla/MediaButtonReceiver.java index a061c59a..5240fdea 100644 --- a/src/ch/blinkenlights/android/vanilla/MediaButtonReceiver.java +++ b/src/ch/blinkenlights/android/vanilla/MediaButtonReceiver.java @@ -185,9 +185,8 @@ public class MediaButtonReceiver extends BroadcastReceiver { if (act == null) return; - Intent intent = new Intent(context, PlaybackService.class); - intent.setAction(act); - context.startService(intent); + Intent intent = ShortcutPseudoActivity.getIntent(context, act); + context.startActivity(intent); } diff --git a/src/ch/blinkenlights/android/vanilla/ShortcutPseudoActivity.java b/src/ch/blinkenlights/android/vanilla/ShortcutPseudoActivity.java index 6720f63c..3843a220 100644 --- a/src/ch/blinkenlights/android/vanilla/ShortcutPseudoActivity.java +++ b/src/ch/blinkenlights/android/vanilla/ShortcutPseudoActivity.java @@ -25,18 +25,37 @@ import android.os.Bundle; public class ShortcutPseudoActivity extends Activity { + final static int INTENT_FLAGS = Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_TASK_ON_HOME; + + /** + * Returns an intent pointing to this activity. + * + * @param context the context to use. + * @param action the action to set. + * @return an intent for this activity. + */ + public static Intent getIntent(Context context, String action) { + Intent intent = new Intent(context, ShortcutPseudoActivity.class) + .setFlags(INTENT_FLAGS) + .setAction(action); + return intent; + } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); final String action = getIntent().getAction(); switch (action) { + case PlaybackService.ACTION_PLAY: + case PlaybackService.ACTION_PAUSE: case PlaybackService.ACTION_TOGGLE_PLAYBACK: case PlaybackService.ACTION_TOGGLE_PLAYBACK_DELAYED: case PlaybackService.ACTION_RANDOM_MIX_AUTOPLAY: case PlaybackService.ACTION_NEXT_SONG: case PlaybackService.ACTION_NEXT_SONG_DELAYED: + case PlaybackService.ACTION_NEXT_SONG_AUTOPLAY: case PlaybackService.ACTION_PREVIOUS_SONG: + case PlaybackService.ACTION_PREVIOUS_SONG_AUTOPLAY: case PlaybackService.ACTION_CYCLE_SHUFFLE: case PlaybackService.ACTION_CYCLE_REPEAT: Intent intent = new Intent(this, PlaybackService.class);