2016-09-26 25 views
1

以下是我努力製作工作代碼以處理耳機按鈕事件的最佳方式。我讀了Android developer guide,但它顯然是錯誤的,因爲他們要求開始監聽註冊類名。Android:處理耳機按鈕事件並將信息發送至MainActivity

am.registerMediaButtonEventReceiver(RemoteControlReceiver); // Wrong 

因此,我查看其他示例以更正代碼。例如,許多祕密的建議已發表在this question,我也嘗試過其他的代碼,如this,並與MediaSession另一種解決方案,並清洗不需要的,我寫這個代碼:

我實現了類RemoteControlReceiver。顯然,沒有必要對一個靜態內部類,其實,看到this comment

public class RemoteControlReceiver extends BroadcastReceiver { 

     public RemoteControlReceiver() { 
      super(); 
     } 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      Toast.makeText(context, "EVENT!!", Toast.LENGTH_SHORT).show(); 
      if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) { 
       KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT); 
       if (KeyEvent.KEYCODE_MEDIA_PLAY == event.getKeyCode()) { 
        Toast.makeText(context, "EVENT!!", Toast.LENGTH_SHORT).show(); 

       } 
      } 
     } 
    } 

然後我註冊了MainActivity的onCreate(){裏面的意圖......

AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE); 
    ComponentName mReceiverComponent = new ComponentName(this, RemoteControlReceiver.class); 
    am.registerMediaButtonEventReceiver(mReceiverComponent); 

的registerMediaButtonEventReceiver被廢棄的方式...

在清單內我記錄過濾器,在活動標記後:

<activity> 
... 
</activity> 

<receiver android:name=".RemoteControlReceiver" android:enabled="true"> 
    <intent-filter android:priority="2147483647"> 
     <action android:name="android.intent.action.MEDIA_BUTTON" /> 
    </intent-filter> 
</receiver> 

注意:使用靜態內部類將是,例如,「.MainActivity $ RemoteControlReceiver」。

我的工作

compileSdkVersion 24 
buildToolsVersion "24.0.0" 
... 
minSdkVersion 21 
targetSdkVersion 24 

這裏我的問題:

  • 爲什麼registerMediaButtonEventReceiver已經過時?看來現在所有這些範例都是錯誤的,但我沒有發現如何在Android Developer Portal上處理這些類問題。
  • 我可以通過哪種方式與MainActivity交互?我的目的是在執行一些耳機操作時對MainActivity執行操作。
+0

你的清單聲明'MainActivity $ MediaButtonReceiver',但你的類被稱爲'RemoteControlReceiver'。這是什麼? – ianhanniballake

+0

謝謝@ianhanniballake硝酸那。這只是一個複製粘貼錯字,但我在所有的實驗中都實現了很多接收器。現在我糾正了它,我確認設置如上所述。 – donnadulcinea

回答

1

API 21更改了整個媒體應用程序API,現在整個集中在MediaSession左右。您可以直接在MediaSession.Callback中收到回調,而無需註冊BroadcastReceiver(如API 18之前所需)或PendingIntent(通過registerMediaButtonEventReceiver(PendingIntent))。

您可以通過下面的代碼建立一個MediaSession

MediaSession.Callback callback = new MediaSession.Callback() { 
    @Override 
    public void onPlay() { 
    // Handle the play button 
    } 
}; 
MediaSession mediaSession = new MediaSession(context, 
    TAG); // Debugging tag, any string 
mediaSession.setFlags(
    MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | 
    MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS); 
mediaSession.setCallback(callback); 

// Set up what actions you support and the state of your player 
mediaSession.setState(
    new PlaybackState.Builder() 
    .setActions(PlaybackState.ACTION_PLAY | 
       PlaybackState.ACTION_PAUSE | 
       PlaybackState.ACTION_PLAY_PAUSE); 
    .setState(PlaybackState.STATE_PLAYING, 
     0, // playback position in milliseconds 
     1.0); // playback speed 

// Call this when you start playback after receiving audio focus 
mediaSession.setActive(true); 

如果你只是想處理的多媒體按鍵,而你的活動是可見的,你可以有你的MediaSession由活動本身處理(這將允許您的Callback只是您的活動中的一個變量)。

Best practices in media playback talk from I/O 2016遍歷所有的細節,並建立一個偉大的媒體應用所需的其他API,但是注意,它使用MediaSessionCompat和作爲Media playback and the Support Library blog post詳述的其他支持庫類。

+0

這就是@ianhanniballake,我想找到有關新的最佳實踐的信息。所有附加的參考文獻也是有用的。謝謝! – donnadulcinea