简体   繁体   English

MediaButtonIntentReceiver无法在Android 4.0+中运行

[英]MediaButtonIntentReceiver not working in Android 4.0+

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. 此解决方案适用于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();
    }
}

To try to make this work, it sounds like 4.0+ requires something like this which didnt work: 为了尝试使这项工作,听起来像4.0+需要这样的东西不起作用:

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

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. 它肯定是4.0 + / ICS / JellyBean问题......这是在服务中完成的,而不是活动。

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>

In Android 3.0+ you must use registerMediaButtonEventReceiver to register the receiver. 在Android 3.0+中,您必须使用registerMediaButtonEventReceiver来注册接收器。 This uses the AndroidManifest for the IntentFilter. 这使用AndroidManifest作为IntentFilter。 The reason it works in 2.x is because you were registering it with this.registerReceiver() which registered the receiver without the AndroidManifest. 它在2.x中工作的原因是因为您使用this.registerReceiver()注册它,该注册接收器没有AndroidManifest。

Just try on jelly on this is working for me : 试试果冻这对我有用:

             broadcastReceiver = new BroadcastReceiver() {//global BroadcastReceiver
            @Override
            public void onReceive(Context context, Intent intent) {

                String action = intent.getAction();
                if(action.equals("android.intent.action.MEDIA_BUTTON")){
                    Log.e("test", "ok");
                }
            };

Intent : 意图:

        IntentFilter intentfilterTime = null;
        intentfilterTime = new IntentFilter();  
        intentfilterTime.addAction("android.intent.action.MEDIA_BUTTON");
        registerReceiver(broadcastReceiver, intentfilterTime);

Test from service with media button of Widgetsoid (who emulate BT media buttons). 使用Widgetsoid的媒体按钮(模拟BT媒体按钮)进行服务测试。 All is working. 一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM