MediaButtonIntentReceiver不是在Android的工作4.0+ [英] MediaButtonIntentReceiver not working in Android 4.0+

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

问题描述

的目标是为拦截从耳机的广播,以及蓝牙最终到从耳机响应不同类型的点击来改变媒体播放器。该解决方案之前,ICS工作正常的所有​​版本。下面是一些code和东西我曾尝试:

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 /软糖问题......这是正在在服务完成的,而不是活动。

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>

在安卓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天全站免登陆