简体   繁体   English

任何应用程序都可以广播具有动作名称“ android.intent.action.MEDIA_BUTTON”的Intent

[英]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.

相关问题 抓不到 android.intent.action.MEDIA_BUTTON - Fail to catch android.intent.action.MEDIA_BUTTON 如果按一下Headset按钮,android.intent.action.MEDIA_BUTTON事件将触发两次 - android.intent.action.MEDIA_BUTTON event fires twice if i press Headset button once 应用程序未接收到动作意图广播 - Action intent broadcast not recieved by application 当我删除 <action android:name=“android.intent.action.MAIN” /> 从清单中,应用程序从第二个活动开始 - When I remove the <action android:name=“android.intent.action.MAIN” /> from the manifest the the application starts from the 2nd activity 使用action_view意图后Android应用程序崩溃 - Android application crash after using action_view intent 应用程序因 Permission Denial 崩溃:不允许发送广播 android.intent.action.SCREEN_ON - App crashes with Permission Denial: not allowed to send broadcast android.intent.action.SCREEN_ON 无法将主要活动中的按钮单击操作意图连接到Service类中的待处理意图,以使用.getAction()获得通知中的动作 - Can not connect button click action Intent in Main activity to a Pending Intent in Service class to get action in notification using .getAction() 找不到用于处理意图的活动{act = android.intent.action.View} - No activity found to handle intent { act=android.intent.action.View } 没有找到处理意图的活动{act=android,intent.action.VIEW} - No activity found to handle intent{act=android,intent.action.VIEW} 找不到处理 Intent 的活动:android.intent.action.EDIT - No Activity found to handle Intent : android.intent.action.EDIT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM