MediaButtonIntentReceiver 在 Android 4.0+ 中不起作用 [英] MediaButtonIntentReceiver not working in Android 4.0+

查看:54
本文介绍了MediaButtonIntentReceiver 在 Android 4.0+ 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标是拦截来自耳机的广播,以及最终的蓝牙,以响应来自耳机的不同类型的点击以改变媒体播放器.此解决方案适用于 ICS 之前的所有版本.这是我尝试过的一些代码和事情:

The goal is to intercept broadcasts from the headset, as well as bluetooth eventually, to respond to different types of clicks from the headset to alter the mediaplayer. This solution works fine for all versions prior to ICS. Here is some of the code and things I have tried:

....
private BroadcastReceiver mediaButtonReceiver = new MediaButtonIntentReceiver();
....
public void onCreate() {
    ...
    IntentFilter mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
    mediaFilter.setPriority(2147483647); // this is bad...I know
    this.registerReceiver(mediaButtonReceiver, mediaFilter);
    ...
}

public class MediaButtonIntentReceiver extends BroadcastReceiver {

    private KeyEvent event;

    public MediaButtonIntentReceiver() {
        super();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String intentAction = intent.getAction();
        if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
            return;
        }
        event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
        if (event == null) {
            return;
        }
        try {
            int action = event.getAction();

            switch(action) {

                case KeyEvent.ACTION_UP :
                    Log.d("TEST", "BUTTON UP");
                    break;
                case KeyEvent.ACTION_DOWN :
                case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE :
                    Log.d("TEST", "BUTTON DOWN");
                    break;
            }
        } catch (Exception e) {
            Log.d("TEST", "THIS IS NOT GOOD");
        }
        abortBroadcast();
    }
}

为了尝试使这项工作,听起来像 4.0+ 需要这样的东西,但不起作用:

To try to make this work, it sounds like 4.0+ requires something like this which didnt work:

((AudioManager)getSystemService(AUDIO_SERVICE)).registerMediaButtonEventReceiver(new ComponentName(this, MediaButtonIntentReceiver.class));

我什至尝试将其添加到清单中,除了上述内容:

I even tried to add it to the manifest, in addition to the above:

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

仍然没有运气.我在这里错过了什么?这当然是 4.0+/ICS/JellyBean 问题......这是在服务中完成的,而不是活动.

Still no luck. What am I missing here? It's certainly a 4.0+/ICS/JellyBean issue... This is being done in a service, not an activity.

推荐答案

看起来您的广播接收器是您服务的内部类?如果是这样,请将您的广播接收器设为静态并在清单中执行以下操作:

It looks like your broadcast receiver is an inner class to your service? If so, make your broadcast receiver static and in the manifest do this:

<receiver android:name="MyOuterClass$MediaButtonIntentReceiver" android:enabled="true">
    <intent-filter android:priority="2147483647" >
        <action android:name="android.intent.action.MEDIA_BUTTON" />
    </intent-filter>
</receiver>

在 Android 3.0+ 中,您必须使用 registerMediaButtonEventReceiver 来注册接收器.这将 AndroidManifest 用于 IntentFilter.它在 2.x 中工作的原因是因为您使用 this.registerReceiver() 注册它,它在没有 AndroidManifest 的情况下注册了接收器.

In Android 3.0+ you must use registerMediaButtonEventReceiver to register the receiver. This uses the AndroidManifest for the IntentFilter. The reason it works in 2.x is because you were registering it with this.registerReceiver() which registered the receiver without the AndroidManifest.

这篇关于MediaButtonIntentReceiver 在 Android 4.0+ 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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