BroadcastReceiver的为ACTION_MEDIA_BUTTON不工作 [英] BroadcastReceiver for ACTION_MEDIA_BUTTON not working

查看:876
本文介绍了BroadcastReceiver的为ACTION_MEDIA_BUTTON不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了Android操作系统的Andr​​oid应用程序的版本4.0.3(ICS)。问题是,我没有收到输出从我Log.d()中的BroadcastReceiver的的onReceive()方法,这意味着我的应用程序不正确地处理广播。

I am writing an Android application for version 4.0.3 (ICS) of the Android OS. The issue is that I am not getting the output from my Log.d() in the onReceive() method of the BroadcastReceiver which means my application is not properly handling the broadcast.

我看了一下如何在被点击了ACTION_MEDIA_BUTTON运行code无数的问题。我甚至复制粘贴+ code时,我的没有工作,只是为了看看它是否会工作。

I have read countless questions about how to run code upon a ACTION_MEDIA_BUTTON being clicked. I have even copy + pasted code when mine did not work, just to see if it would work.

我要处理的ACTION_MEDIA_BUTTON是在的耳机,让用户皮卡/结束通话,播放/暂停音乐键。 而是我的应用程序处理这个按钮,当我点击它,股票音乐播放器在我的Nexus S的Andr​​oid开始播放一首歌曲

The ACTION_MEDIA_BUTTON I want to handle is the single button on earphones that allow a user to pickup / end calls, play / pause music. Instead of my application handling this button, when I click it, the stock music player on my Nexus S Android starts playing a song.

我没有把我的code在另一个类,也许这就是为什么它不工作?

I have not placed my code in another class, maybe this is why it's not working?

下面是对的onCreate()方法(这个特定的code我抄掉之后,code我写的没有工作网站)找到了code:

Here's the code found on the onCreate() method (this specific code I copied off a website after the code I wrote didn't work):

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);

我真正需要测试的关键是code_HEADSETHOOK但它不会伤害有其他的code那里进行测试,我会解决它,一旦我能得到的一切工作正常。

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>

我本来以为这可能是一个权限问题,因为我没有指定任何权限,这可是我没有收到任何错误消息。

I originally thought this may be a permissions issue since I didn't specify any permissions for this however I didn't receive any error message.

就像我刚才说的,我已经尝试了这么多变化。一个例子是,在这个链接<一个使用的code href="http://stackoverflow.com/questions/6603170/broadcastreceiver-onreceive-problem-action-media-button-android">broadcastreceiver的onReceive问题ACTION_MEDIA_BUTTON的Andr​​oid ,提供CommonsWare的更正。然而,再次,我修改了它,所以它不是一个独立的类。

Like I said earlier, I have tried many variations of this. One example was the use of the code at this link broadcastreceiver onReceive problem ACTION_MEDIA_BUTTON Android with CommonsWare's corrections. Again, however, I modified it so it wasn't in a seperate class.

感谢您在您的帮助。

推荐答案

我测试了在三星Galaxy S5采用Android 4.4.2。那么,什么是重要的,什么未在其他帖子中提到

I tested this on a Samsung Galaxy S5 with Android 4.4.2. So what is important and what is not mentioned in other posts:

  • 注册在接收器中的的Andr​​oidManifest.xml 的里面的应用程序的标记,但外界从每个的活动的标签。
  • 您的接收器的的BroadcastReceiver 的需要是公共的和静态
  • 在一个活动需要注册一个的 MediaButtonEventReceiver 的监听按钮presses
  • 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

好了这里的一些例如code

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

下面的接收

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
    }
}

和这里的清单片段。如果需要添加优先级的意图过滤器,但没有必要对我来说:

And here the manifest snippet. If needed add priority for the intent-filter, but was not needed for me:

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

有关的参考资料:

  • http://developer.android.com/training/managing-音频/音量playback.html
  • <一个href="http://stackoverflow.com/questions/13257982/mediabuttonintentreceiver-not-working-in-android-4-0">MediaButtonIntentReceiver不工作的Andr​​oid 4.0+
  • <一个href="http://stackoverflow.com/questions/10305261/broadcastreceiver-cant-instantiate-class-no-empty-constructor/10305338#10305338">BroadcastReceiver:无法实例类;没有空的构造
  • http://developer.android.com/training/managing-audio/volume-playback.html
  • MediaButtonIntentReceiver not working in Android 4.0+
  • BroadcastReceiver: can't instantiate class; no empty constructor

这篇关于BroadcastReceiver的为ACTION_MEDIA_BUTTON不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆