为什么ACTION_MEDIA_BUTTON无法处理的事件? [英] Why does ACTION_MEDIA_BUTTON fail to process events?

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

问题描述

下面就如何使用的硬件播放控制按键的培训部分来控制音频播放,我创建了一个监听器类:

 公共类RemoteControlReceiver扩展的BroadcastReceiver {
    @覆盖
    公共无效的onReceive(背景为arg0,意图意图){
        如果(Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())){
            KeyEvent的事件=(KeyEvent)方法intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
            Log.e(TAG,ACTION_MEDIA_BUTTON);

            INT键code = event.getKey code();
            开关(钥匙code)
            {
                案例KeyEvent.KEY code_MEDIA_NEXT:
                    Log.e(TAG,KEY code_MEDIA_NEXT);
                    打破;
                案例KeyEvent.KEY code_MEDIA_PLAY_PAUSE:
                    Log.e(TAG,KEY code_MEDIA_PLAY_PAUSE);
                    打破;
                案例KeyEvent.KEY code_MEDIA_ preVIOUS:
                    Log.e(TAG,KEY code_MEDIA_ preVIOUS);
                    打破;
                默认:
            }

            如果(/*KeyEvent.KEY$c$c_MEDIA_PLAY*/ 126 == event.getKey code()){// KEY code_MEDIA_PLAY未定义API< 11
                Log.e(TAG,KEY code_MEDIA_PLAY);
            }
        }
    }
}
 

在活动的onCreate()注册的:

 私人AudioManager mAudioManager;
私人组件名mRemoteControlReceiver;
...
...
mAudioManager =(AudioManager)getSystemService(Context.AUDIO_SERVICE);
mRemoteControlReceiver =新的组件名(本,RemoteControlReceiver.class);
mAudioManager.registerMediaButtonEventReceiver(mRemoteControlReceiver);
 

和它注册的清单:

 <接收机器人:RemoteControlReceiverNAME =>
    <意向滤光器>
        <作用机器人:名称=android.intent.action.MEDIA_BUTTON/>
    &所述; /意图滤光器>
< /接收器>
 

但当pressing任何这些按钮不会导致任何出现在LogCat中日志消息的! (边打我的媒体与否,无论是否被pressed)

我的codeS似乎拦截其他媒体播放器这些事件,但为什么我不能看到我的任何 Log.e()的消息?我在想什么?

顺便说一句,当我preSS任何一个按钮,出现以下消息而不是:

  W / KeyCharacterMap(19801):无法打开keycharmap文件
W / KeyCharacterMap(19801):错误加载keycharmap文件'/system/usr/keychars/h2w_headset.kcm.bin。 hw.keyboards.131075.devname ='H2W耳机
W / KeyCharacterMap(19801):使用默认的键盘对应:/system/usr/keychars/qwerty.kcm.bin
 

解决方案

你碰巧使用图书馆项目来实现这一点?

如果是这样, <哪些/ em>的清单你把&LT;接收器&GT; 吗?图书馆的或应用程序的?

如果你把在库的,它不会工作。你必须把这个应用程序的清单:

 &LT;接收机器人:RemoteControlReceiverNAME =&GT;
    &LT;意向滤光器&gt;
        &lt;作用机器人:名称=android.intent.action.MEDIA_BUTTON/&GT;
    &所述; /意图滤光器&gt;
&LT; /接收器&GT;
 

Following the training section on how to use hardware playback control keys to control audio playback, I create a listener class:

public class RemoteControlReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context arg0, Intent intent) {
        if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
            KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
            Log.e(TAG, "ACTION_MEDIA_BUTTON"); 

            int keycode = event.getKeyCode();
            switch (keycode)
            {
                case KeyEvent.KEYCODE_MEDIA_NEXT:
                    Log.e(TAG, "KEYCODE_MEDIA_NEXT"); 
                    break;
                case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                    Log.e(TAG, "KEYCODE_MEDIA_PLAY_PAUSE"); 
                    break;
                case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
                    Log.e(TAG, "KEYCODE_MEDIA_PREVIOUS"); 
                    break;
                default:
            }

            if (/*KeyEvent.KEYCODE_MEDIA_PLAY*/ 126 == event.getKeyCode()) { // KEYCODE_MEDIA_PLAY undefined for API < 11
                Log.e(TAG, "KEYCODE_MEDIA_PLAY"); 
            }           
        }
    }
}

Registered it in activity's onCreate():

private AudioManager mAudioManager;
private ComponentName mRemoteControlReceiver;
...
...
mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
mRemoteControlReceiver = new ComponentName(this, RemoteControlReceiver.class);
mAudioManager.registerMediaButtonEventReceiver(mRemoteControlReceiver);

And registered it in the manifest:

<receiver android:name=".RemoteControlReceiver">
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_BUTTON" />
    </intent-filter>
</receiver>

But when pressing any of these buttons does not result in any of the log message appearing on LogCat! (regardless whether being pressed while playing my media or not)

My codes seem to intercept these events from other media players, but why can't I see any of my Log.e() messages? What am I missing?

BTW, when I press any of those buttons, the following messages appear instead:

W/KeyCharacterMap(19801): Can't open keycharmap file
W/KeyCharacterMap(19801): Error loading keycharmap file '/system/usr/keychars/h2w_headset.kcm.bin'. hw.keyboards.131075.devname='h2w headset'
W/KeyCharacterMap(19801): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

解决方案

Do you happen to use Library Project to implement this?

If so, which manifest did you put the <receiver> in? The library's or the application's?

If you put in the library's, it won't work. You must place this in the application's manifest:

<receiver android:name=".RemoteControlReceiver">
    <intent-filter>
        <action android:name="android.intent.action.MEDIA_BUTTON" />
    </intent-filter>
</receiver>

这篇关于为什么ACTION_MEDIA_BUTTON无法处理的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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