English 中文(简体)
广播行动 不工作
原标题:BroadcastReceiver for ACTION_MEDIA_BUTTON not working

我正在撰写一份安文斯文科文版申请书。 问题是,我没有从我的Log.d()中获取广播接收器的收受方法的产出,这意味着我的申请没有妥善处理广播。

我已读到无数问题,即如何在被点击的“行动”上制定守则。 在我不工作时,我甚至复制了+过去版的代码,只是看它是否会发挥作用。

行动: 我想处理的是耳机上的single,使用户能够收听/终端电话、游戏/乐。 代替我处理这一纽顿语的申请,当我点击时,我的Nexus S Anders音乐家开始玩一个歌曲

我没有把我的法典放在另一个类别中,这可能是为什么它不工作?

这里的法典载于“Create()”方法上(在我撰写的《守则》之后,我在网站上抄录了该具体准则):

IntentFilter mediaButtonFilter = new IntentFilter(
            Intent.ACTION_MEDIA_BUTTON);
    mediaButtonFilter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
    BroadcastReceiver brMediaButton = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            Log.d("Event", "Media button!");
            this.abortBroadcast();

            KeyEvent key = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
            if(key.getAction() == KeyEvent.ACTION_UP) {
                int keycode = key.getKeyCode();
                if(keycode == KeyEvent.KEYCODE_MEDIA_NEXT) {
                    Log.d("TestApp", "Next Pressed");
                } else if(keycode == KeyEvent.KEYCODE_MEDIA_PREVIOUS) {
                    Log.d("TestApp", "Previous pressed");
                } else if(keycode == KeyEvent.KEYCODE_HEADSETHOOK) {
                    Log.d("TestApp", "Head Set Hook pressed");
                }
            }

        }
    };
    registerReceiver(brMediaButton, mediaButtonFilter);

All I really need to test for is the KEYCODE_HEADSETHOOK but it doesn t hurt to have the other code there for testing, I ll fix it up once I can get everything working correctly.

在我的发言中:

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

我最初认为,这可能是一个许可问题,因为我没有具体说明对此的任何许可,但我没有收到任何错误的信息。

同我先前说过的那样,我尝试了这方面的许多变化。 其中一个例子就是在以下链接中使用该守则:。 - 《关于接收问题的行动》——MEDIA_BUTTON Anders/a>和《共同战争》的更正。 然而,我再次修改了它,这样,它就属于一个充裕的阶层。

事先感谢你的帮助。

最佳回答

我在三星银河S5号上测试了这一试验,并测试了4.4.2。 因此,其他职位没有提及哪些重要和哪些内容?

  • Register the receiver in the AndroidManifest.xml inside the application tag but outside from every activity tag.
  • Your receiver Broadcastreceiver need to be public and static
  • One activity need to register an MediaButtonEventReceiver to listen for the button presses

Okay and here some example Code:

mAudioManager =  (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mReceiverComponent = new ComponentName(this,YourBroadcastReceiver.class);
...
mAudioManager.registerMediaButtonEventReceiver(mReceiverComponent);
...
// somewhere else
mAudioManager.unregisterMediaButtonEventReceiver(mReceiverComponent);

www.un.org/Depts/DGACM/index_french.htm

public static class YourBroadcastReceiver extends BroadcastReceiver{

    // Constructor is mandatory
    public MediaBroadcastReceiver ()
    {
        super ();
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        String intentAction = intent.getAction();
        Log.i (TAG_MEDIA, intentAction.toString() + " happended");
        if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
            Log.i (TAG_MEDIA, "no media button information");
            return;
        }
        KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            Log.i (TAG_MEDIA, "no keypress");
            return;
        }
        // other stuff you want to do
    }
}

这里还有<>万能>。 如果需要增加意向书的优先顺序,但我不需要:

<application>
    <receiver android:name="OuterClass$YourBroadcastReceiver">
        <intent-filter>
           <action android:name="android.intent.action.MEDIA_BUTTON" />
         </intent-filter>
    </receiver>
    <activity> ... </activity>
</application>

参考文献:

问题回答
mediaButtonReceiver = new ComponentName(getPackageName(), MediaButtonReceiver.class.getName());

mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

mAudioManager.registerMediaButtonEventReceiver(mediaButtonReceiver);

该法典将约束你的耳光和你的 app子,直到你要求:

mAudioManager.unregisterMediaButtonEventReceiver(mediaButtonReceiver);

您可以避免使用Blacastreceiver for Anders >5.0(API, 21 LOLLIPOP)使用下文所述媒体:





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签