【问题标题】:MediaButtonIntentReceiver not working in Android 4.0+MediaButtonIntentReceiver 在 Android 4.0+ 中不起作用
【发布时间】:2012-11-06 19:28:35
【问题描述】:

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

....
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+ 需要这样的东西,但它不起作用:

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

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

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

【问题讨论】:

标签: android android-intent media-player android-4.0-ice-cream-sandwich


【解决方案1】:

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

<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 的情况下注册了接收器。

【讨论】:

  • 我使用内部类的唯一理由是能够访问外部类的方法,但是如果将内部类设为静态,我将无法访问这些方法。 Catch 22。建议?
  • @an00b 我们使用 Greenrobot 的 EventBus 作为组件之间通信的简单方式 (greenrobot.org/eventbus/documentation)。例如。静态 BroadcastReceiver 发布活动订阅的事件。非常适合解耦
【解决方案2】:

试试果冻,这对我有用:

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

意图:

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

使用 Widgetsoid 的媒体按钮(模拟 BT 媒体按钮)从服务中测试。一切正常。

【讨论】:

  • 当我按住媒体按钮时,Google 应用程序仍然会接管它。我什至在意图中添加了最大优先级。我在 onCreate 函数中添加了您的代码片段。我还需要定义其他地方吗,比如关于意图的清单?就像我说的,这在 4.0 之前的 Android 上运行良好
  • 你在真机上测试过吗?还是模拟器?我的代码对你不起作用?让我在 android 4.2 的 Galaxy Nexus 上工作。通常意图是在清单上或像我给你的代码中,但没有其他地方。
  • 在 4.1.1 的 Galaxy S3 上测试。我已经尝试了以上所有方法。您的代码确实适用于 2.3.6
  • 也许您有另一个具有高优先级(比您的优先级更高)的应用程序获得事件,所以您永远不会拥有它。因为这段代码在 android 4 上为我工作。我只看到这种可能性......
  • 我在 3 台设备上试用过,都是类似的应用程序。我只是不认为寄存器工作。看看这个:stackoverflow.com/questions/10537184/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-12
  • 1970-01-01
  • 1970-01-01
  • 2017-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多