在启动时Spotify Android Intent Play

Tho*_*mas 1 android spotify android-intent

我试图让Spotify在从意图启动时恢复播放但没有太多运气.我想我很接近我可以让Spotify推出,如果我指定一个艺术家的搜索,它会自动播放,但我真的只想让它恢复我上次播放的内容,我还没有开始工作.这个网站似乎有可能,但到目前为止,Spotify只是启动并进入搜索屏幕. http://developer.android.com/guide/components/intents-common.html#PlaySearch

到目前为止,这是我的代码:

        final Intent intent1 = new Intent(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
        intent1.setComponent(new ComponentName("com.spotify.music", "com.spotify.music.MainActivity"));
        intent1.putExtra(MediaStore.EXTRA_MEDIA_FOCUS, "vnd.android.cursor.item/*");
        intent1.putExtra(SearchManager.QUERY, "");
        intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (intent1.resolveActivity(getPackageManager()) != null) {
            startActivity(intent1);
        }
Run Code Online (Sandbox Code Playgroud)

Tho*_*mas 7

我花了一段时间来弄清楚这一点,所以我想我会发布我用过的解决方案.我遍历了订阅Intent.ACTION_MEDIA_BUTTON的所有软件包,那时我发现了我需要的组件名称才能使其工作:

private void playPlayMusic() {
    Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
    i.setComponent(new ComponentName("com.spotify.music", "com.spotify.music.internal.receiver.MediaButtonReceiver"));
    i.putExtra(Intent.EXTRA_KEY_EVENT,new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MEDIA_PLAY));
    sendOrderedBroadcast(i, null);

    i = new Intent(Intent.ACTION_MEDIA_BUTTON);
    i.setComponent(new ComponentName("com.spotify.music", "com.spotify.music.internal.receiver.MediaButtonReceiver"));
    i.putExtra(Intent.EXTRA_KEY_EVENT,new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_MEDIA_PLAY));
    sendOrderedBroadcast(i, null);
}
Run Code Online (Sandbox Code Playgroud)