Android:处理耳机按钮事件和向MainActivity发送信息

don*_*nea 7 android headset

这是我努力制作一个工作代码来处理耳机按钮事件的最佳方式.我阅读了Android开发人员指南,但这显然是错误的,因为他们要求开始监听注册类名.

am.registerMediaButtonEventReceiver(RemoteControlReceiver); // Wrong
Run Code Online (Sandbox Code Playgroud)

所以我查看其他示例以更正代码.例如,在这个问题中已经发布了许多秘密建议,我还尝试了其他代码,例如这个,以及MediaSession的另一个解决方案,并清理不需要的我写了这段代码:

我实现了RemoteControlReceiver类.显然,不需要静态内部类,事实上,请看这条评论:

public class RemoteControlReceiver extends BroadcastReceiver {

        public RemoteControlReceiver() {
            super();
        }

        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "EVENT!!", Toast.LENGTH_SHORT).show();
            if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
                KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
                if (KeyEvent.KEYCODE_MEDIA_PLAY == event.getKeyCode()) {
                    Toast.makeText(context, "EVENT!!", Toast.LENGTH_SHORT).show();

                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后我在MainActivity onCreate(){ ...中注册了意图

    AudioManager am = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
    ComponentName mReceiverComponent = new ComponentName(this, RemoteControlReceiver.class);
    am.registerMediaButtonEventReceiver(mReceiverComponent);
Run Code Online (Sandbox Code Playgroud)

registerMediaButtonEventReceiver被弃用了......

在清单中我记录了过滤器,在活动标记之后:

<activity>
...
</activity>

<receiver android:name=".RemoteControlReceiver" android:enabled="true">
    <intent-filter android:priority="2147483647">
        <action android:name="android.intent.action.MEDIA_BUTTON" />
    </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)

注意:使用静态内部类将是例如" .MainActivity $ RemoteControlReceiver ".

我正在努力

compileSdkVersion 24
buildToolsVersion "24.0.0"
...
minSdkVersion 21
targetSdkVersion 24
Run Code Online (Sandbox Code Playgroud)

在这里我的问题:

  • 为什么不推荐使用registerMediaButtonEventReceiver?现在似乎所有这些范例都是错误的,但我没有找到关于如何在Android Developer Portal上处理这些类问题的信息.
  • 我可以通过哪种方式与MainActivity进行互动?我的目的是在执行某些耳机操作时对MainActivity执行操作.

ian*_*ake 5

API 21更改了整个媒体应用程序API,现在完全以MediaSession为中心。您可以直接在MediaSession.Callback中接收回调,而无需注册BroadcastReceiver(在API 18之前需要)或PendingIntent(通过registerMediaButtonEventReceiver(PendingIntent))。

您可以MediaSession通过以下代码设置一个:

MediaSession.Callback callback = new MediaSession.Callback() {
  @Override
  public void onPlay() {
    // Handle the play button
  }
};
MediaSession mediaSession = new MediaSession(context,
  TAG); // Debugging tag, any string
mediaSession.setFlags(
  MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
  MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSession.setCallback(callback);

// Set up what actions you support and the state of your player
mediaSession.setState(
  new PlaybackState.Builder()
    .setActions(PlaybackState.ACTION_PLAY |
                PlaybackState.ACTION_PAUSE |
                PlaybackState.ACTION_PLAY_PAUSE);
    .setState(PlaybackState.STATE_PLAYING,
      0, // playback position in milliseconds
      1.0); // playback speed

// Call this when you start playback after receiving audio focus
mediaSession.setActive(true);
Run Code Online (Sandbox Code Playgroud)

如果您只想在活动可见时处理媒体按钮,则可以仅MediaSession由活动本身来处理(这将使您Callback成为活动中的变量)。

媒体最佳实践播放的谈话从I / O 2016遍历所有的细节,并建立一个伟大的媒体应用所需的其他API,但是注意,它使用MediaSessionCompat和其他支持库类如在详细媒体播放和支持图书馆博客文章