顾名思义:它显然是一个广播接收器类(BroadbcastReceiver),那么它就具备了BroadbcastReceiver类的使用方式,
但是,因为它需要通过AudioManager对象注册,所以它有着自己的独特之处(否则我也不会单独拿出来分析,- -),后面我们会慢慢的讲解。
点击MEDIA_BUTTON发送的Intent Action 为:
ACTION_MEDIA_BUTTON ="android.intent.action.MEDIA_BUTTON"
Intent 附加值为(Extra)点击MEDIA_BUTTON的按键码 :
//获得KeyEvent对象
KeyEvent keyEvent = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
//获得Action
String intentAction = intent.getAction() ;
AudioManager对象注册MEDIA_BUTTON广播的方法原型为:
public voidregisterMediaButtonEventReceiver(ComponentNameeventReceiver)
Register a component to be the sole receiverof MEDIA_BUTTON intents
Parameters:
eventReceiver : identifier of a BroadcastReceiver that will receive the media button intent. This broadcast receiver
must bedeclared in the application manifest.
从注释可知以下两点:
下面我们就简单的写一个MediaButtonReceiver类,并且在AndroidManifest.xml定义
1、 自定义的MediaButtonReceiver 广播类
2、在AndroidManifest.xml声明我们定义的广 类。
在模拟器上,我们可以手动构造MEDA_BUTTON的广播,并且将它发送出去(后面会介绍)。
如果有真机测试的话,按下MEDIA_BUTTON是可以接受到MEDIA_BUTTON广播的,如果没有接受到,请关闭所有应用程序,在观察效果。