发送歌曲标题到spotify开始从Android应用程序播放

Pet*_*ter 5 android

有没有办法从我的应用程序发送一个歌曲标题到spotify应用程序,以便它将通过spotify开始播放歌曲?

我尝试使用我在另一个代码中找到的波纹管代码,但没有任何反应.

Intent intent = new Intent(Intent.ACTION_MAIN);
                intent.setAction(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
                intent.setComponent(new ComponentName("com.spotify.mobile.android.ui", "com.spotify.mobile.android.ui.Launcher"));
                intent.putExtra(SearchManager.QUERY, "michael jackson smooth criminal");
Run Code Online (Sandbox Code Playgroud)

我知道shazam能够做到这一点.

poi*_*oae 8

您只是创建一个Intent,但是您没有启动Intent.

设置意图后添加此行

startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

所以完整的代码看起来就像那样

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setAction(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
intent.setComponent(new ComponentName("com.spotify.mobile.android.ui", "com.spotify.mobile.android.ui.Launcher"));
intent.putExtra(SearchManager.QUERY, "michael jackson smooth criminal");
try {
  startActivity(intent);
}catch (ActivityNotFoundException e) {
  Toast.makeText(context, "You must first install Spotify", Toast.LENGTH_LONG).show();  
  Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse("market://details?id=com.spotify.mobile.android.ui"));
  startActivity(i);
}
Run Code Online (Sandbox Code Playgroud)