[英]Can any application broadcast Intent with action name “android.intent.action.MEDIA_BUTTON”
My application is registered for intent ( "android.intent.action.MEDIA_BUTTON"
) to handle wired headset key pressed using below code 我的应用程序已注册用于意图(
"android.intent.action.MEDIA_BUTTON"
),以处理使用以下代码按下的有线耳机按键
<receiver
android:name=".HeadsetEventReceiver"
android:enabled="true" >
<intent-filter android:priority="2147483647" >
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
Receiver HeadsetEventReceiver
does some operation on receiving this Intent. 接收器
HeadsetEventReceiver
在接收此Intent时执行一些操作。
Now I have a sample application which broadcast an Intent with action as "android.intent.action.MEDIA_BUTTON"
with valid KeyEvent object using below code. 现在,我有一个示例应用程序,使用以下代码使用有效的KeyEvent对象广播一个动作为
"android.intent.action.MEDIA_BUTTON"
的Intent。
Intent newIntent = new Intent("android.intent.action.MEDIA_BUTTON") ;
KeyEvent event = new KeyEvent(System.currentTimeMillis(), System.currentTimeMillis(), KeyEvent.ACTION_DOWN, 79, 1, 1, 1, 1, 1, InputDevice.SOURCE_TOUCHPAD);
newIntent.putExtra(Intent.EXTRA_KEY_EVENT, event);
getApplicationContext().sendBroadcast(newIntent);
My application is receiving this Intent and processing this data as if it is actually broadcast due to headset key press. 我的应用程序正在接收此Intent,并像处理由于耳机按键而实际广播的那样处理该数据。
My query is when any application try to send broadcast with native Intents(eg: "android.intent.action.BOOT_COMPLETED"
) securityException is thrown. 我的查询是任何应用程序尝试发送带有本机Intent(例如
"android.intent.action.BOOT_COMPLETED"
)的广播时,都会抛出securityException。 Why does it not apply for the above Intent( "android.intent.action.MEDIA_BUTTON"
). 为什么不适用于上述Intent(
"android.intent.action.MEDIA_BUTTON"
)。
Since 4.0 or greater the Android document says that you have to use registerMediaButtonEventReceiver
for registering the intent action. 从4.0或更高版本开始 , Android文档说您必须使用
registerMediaButtonEventReceiver
来注册意图操作。 Without registering to registerMediaButtonEventReceiver
you won't get broadcast message. 如果不注册
registerMediaButtonEventReceiver
您将不会收到广播消息。
Below is the code which is documented here 以下是此处记录的代码
AudioManager am = mContext.getSystemService(Context.AUDIO_SERVICE);
...
// Start listening for button presses
am.registerMediaButtonEventReceiver(RemoteControlReceiver);
...
// Stop listening for button presses
am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
So if you are using registerMediaButtonEventReceiver
then you would get the broadcast else you won't. 因此,如果您使用
registerMediaButtonEventReceiver
,则将获得广播,否则将不会。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.