2014-03-07 67 views

回答

6

註冊知行動意圖的廣播接收機和處理廣播顯示或做一些與軌道信息,source

public class CurrentMusicTrackInfoActivity extends Activity { 

    public static final String SERVICECMD = "com.android.music.musicservicecommand"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     IntentFilter iF = new IntentFilter(); 
     iF.addAction("com.android.music.metachanged"); 
     iF.addAction("com.android.music.playstatechanged"); 
     iF.addAction("com.android.music.playbackcomplete"); 
     iF.addAction("com.android.music.queuechanged"); 
     iF.addAction("com.htc.music.metachanged"); 
     iF.addAction("fm.last.android.metachanged"); 
     iF.addAction("com.sec.android.app.music.metachanged"); 
     iF.addAction("com.nullsoft.winamp.metachanged"); 
     iF.addAction("com.amazon.mp3.metachanged");  
     iF.addAction("com.miui.player.metachanged");   
     iF.addAction("com.real.IMP.metachanged"); 
     iF.addAction("com.sonyericsson.music.metachanged"); 
     iF.addAction("com.rdio.android.metachanged"); 
     iF.addAction("com.samsung.sec.android.MusicPlayer.metachanged"); 
     iF.addAction("com.andrew.apollo.metachanged"); 

     registerReceiver(mReceiver, iF); 
    } 

    private BroadcastReceiver mReceiver = new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      String action = intent.getAction(); 
      String cmd = intent.getStringExtra("command"); 
      Log.v("tag ", action + "/" + cmd); 
      String artist = intent.getStringExtra("artist"); 
      String album = intent.getStringExtra("album"); 
      String track = intent.getStringExtra("track"); 
      Log.v("tag", artist + ":" + album + ":" + track); 
      Toast.makeText(CurrentMusicTrackInfoActivity.this, track, Toast.LENGTH_SHORT).show(); 
     } 
    }; 

} 
+0

謝謝!似乎這是我尋找的! –

+1

這個解決方案對我來說已經夠用了。但還有一個額外的問題。如果音樂開始播放,然後我開始這個應用程序,是否有可能獲得一些信息(可能是一些粘性意圖)有關當前播放歌曲的數據? –

+0

我們如何獲得當前正在播放的歌曲的專輯封面? –

相關問題