Most visited

Recently visited

MediaButtonReceiver

public class MediaButtonReceiver
extends BroadcastReceiver

java.lang.Object
   ↳ android.content.BroadcastReceiver
     ↳ android.support.v4.media.session.MediaButtonReceiver


媒体按钮接收器接收并帮助将硬件媒体播放按钮(例如有线和无线耳机中的按钮)转换为应用程序中的相应回调。

You can add this MediaButtonReceiver to your app by adding it directly to your AndroidManifest.xml:
 <receiver android:name="android.support.v4.media.session.MediaButtonReceiver" >
   <intent-filter>
     <action android:name="android.intent.action.MEDIA_BUTTON" />
   </intent-filter>
 </receiver>
 
This class assumes you have a Service in your app that controls media playback via a MediaSessionCompat - all Intents received by the MediaButtonReceiver will be forwarded to that service.

First priority is given to a Service that includes an intent filter that handles ACTION_MEDIA_BUTTON:
 <service android:name="com.example.android.MediaPlaybackService" >
   <intent-filter>
     <action android:name="android.intent.action.MEDIA_BUTTON" />
   </intent-filter>
 </service>
 
If such a Service is not found, MediaButtonReceiver will attempt to find a media browser service implementation. If neither is available or more than one valid service/media browser service is found, an IllegalStateException will be thrown.

Events can then be handled in onStartCommand(Intent, int, int) by calling handleIntent(MediaSessionCompat, Intent), passing in your current MediaSessionCompat:
 private MediaSessionCompat mMediaSessionCompat = ...;

 public int onStartCommand(Intent intent, int flags, int startId) {
   MediaButtonReceiver.handleIntent(mMediaSessionCompat, intent);
   return super.onStartCommand(intent, flags, startId);
 }
 
This ensures that the correct callbacks to MediaSessionCompat.Callback will be triggered based on the incoming KeyEvent.

Summary

Public constructors

MediaButtonReceiver()

Public methods

static KeyEvent handleIntent(MediaSessionCompat mediaSessionCompat, Intent intent)

提取的任何可用 KeyEventACTION_MEDIA_BUTTON意图,它传递到 MediaSessionCompat使用 dispatchMediaButtonEvent(KeyEvent) ,这反过来将触发回调到 MediaSessionCompat.Callback经由注册 setCallback(MediaSessionCompat.Callback)

void onReceive(Context context, Intent intent)

当BroadcastReceiver正在接收Intent广播时调用此方法。

Inherited methods

From class android.content.BroadcastReceiver
From class java.lang.Object

Public constructors

MediaButtonReceiver

MediaButtonReceiver ()

Public methods

handleIntent

KeyEvent handleIntent (MediaSessionCompat mediaSessionCompat, 
                Intent intent)

提取的任何可用 KeyEventACTION_MEDIA_BUTTON意图,它传递到 MediaSessionCompat使用 dispatchMediaButtonEvent(KeyEvent) ,这反过来将触发回调到 MediaSessionCompat.Callback经由注册 setCallback(MediaSessionCompat.Callback)

The returned KeyEvent is non-null if any KeyEvent is found and can be used if any additional processing is needed beyond what is done in the MediaSessionCompat.Callback. An example of is to prevent redelivery of a KEYCODE_MEDIA_PLAY_PAUSE Intent in the case of the Service being restarted (which, by default, will redeliver the last received Intent).
 KeyEvent keyEvent = MediaButtonReceiver.handleIntent(mediaSession, intent);
 if (keyEvent != null && keyEvent.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
   Intent emptyIntent = new Intent(intent);
   emptyIntent.setAction("");
   startService(emptyIntent);
 }
 

Parameters
mediaSessionCompat MediaSessionCompat: A MediaSessionCompat that has a MediaSessionCompat.Callback set.
intent Intent: The intent to parse.
Returns
KeyEvent The extracted KeyEvent if found, or null.

onReceive

void onReceive (Context context, 
                Intent intent)

当BroadcastReceiver正在接收Intent广播时调用此方法。 在此期间,您可以使用BroadcastReceiver上的其他方法查看/修改当前结果值。 此方法始终在其进程的主线程中调用,除非您明确要求将其安排在使用registerReceiver(BroadcastReceiver, IntentFilter, String, android.os.Handler)的其他线程上。 当它在主线程上运行时,您不应该在其中执行长时间运行的操作(在考虑接收器被阻塞并且候选者被终止之前系统允许超时10秒)。 你不能在你的onReceive()的实现中启动一个弹出对话框。

如果此BroadcastReceiver是通过<receiver>标签启动的,则该对象在从此函数返回后不再有效。 这意味着您不应该执行任何异步返回结果的操作 - 特别是与服务交互时,应该使用startService(Intent)而不是bindService(Intent, ServiceConnection, int) 如果您希望与已在运行的服务进行交互,则可以使用peekService(Context, Intent)

registerReceiver(BroadcastReceiver, IntentFilter)和应用程序清单中使用的意图过滤器保证是排他性的。 它们提示操作系统如何找到合适的收件人。 发件人可能会强制传递给特定收件人,从而绕过过滤器解决方案。 由于这个原因, onReceive()实现应该只响应已知的操作,忽略它们可能收到的任何意外的意图。

Parameters
context Context: The Context in which the receiver is running.
intent Intent: The Intent being received.

Hooray!