[英]Fail to catch android.intent.action.MEDIA_BUTTON
My app that uses TextToSpeech to read some text is 90% finishes but has been stuck at this part for a few days.我使用 TextToSpeech 阅读一些文本的应用程序完成了 90%,但已经在这部分停留了几天。 All I want is have the play/pause button of my bluetooth headset (Xiaomi's Mi Sports Bluetooth Earphones) to play/pause the TextToSpeech.
我想要的只是让我的蓝牙耳机(小米的 Mi 运动蓝牙耳机)的播放/暂停按钮来播放/暂停 TextToSpeech。 I think it is the android.intent.action.MEDIA_BUTTON I need to catch, so I added these:
我认为这是我需要捕捉的 android.intent.action.MEDIA_BUTTON ,所以我添加了这些:
In AndroidManifest.xml:在 AndroidManifest.xml 中:
<receiver android:name=".ButtonReceiver">
<intent-filter
android:priority="10000">
<action android:name="android.intent.action.MEDIA_BUTTON"/>
</intent-filter>
</receiver>
Then the class ButtonReceiver.java然后是 class ButtonReceiver.java
public class ButtonReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "debug media button test", Toast.LENGTH_LONG).show();
// will finish the code once I catch this intent
}
}
The debug text does not show up.调试文本不显示。 Though if I changed
<action android:name="android.intent.action.MEDIA_BUTTON"/>
to <action android:name="android.media.VOLUME_CHANGED_ACTION"/>
, it does show the text when I press the volume up or down of the headset but this is not exactly what I want.虽然如果我将
<action android:name="android.intent.action.MEDIA_BUTTON"/>
更改为<action android:name="android.media.VOLUME_CHANGED_ACTION"/>
,它会在我按下音量或放下耳机,但这并不是我想要的。 I only want the app to respond to the play/pause button.我只希望应用程序响应播放/暂停按钮。
Then I read that I need to use registerMediaButtonEventReceiver
, so I tried adding this in my MainActivity.java:然后我读到我需要使用
registerMediaButtonEventReceiver
,所以我尝试在我的 MainActivity.java 中添加它:
private AudioManager audioManager;
private ComponentName componentName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
audioManager = (AudioManager) this.getSystemService(AUDIO_SERVICE);
componentName = new ComponentName(this, ButtonReceiver.class);
audioManager.registerMediaButtonEventReceiver(componentName);
}
Still not working.还是行不通。 Also, it says the
registerMediaButtonEventReceiver
is deprecated, so I am wondering if this is the reason that it does not work.此外,它说
registerMediaButtonEventReceiver
被弃用,所以我想知道这是否是它不起作用的原因。
Further reading from the official document page , it tells this:从官方文档页面进一步阅读,它告诉我们:
If you are running Android 5.0 (API level 21) or later, call FLAG_HANDLES_MEDIA_BUTTONS MediaBrowserCompat.ConnectionCallback.onConnected.
如果您正在运行 Android 5.0(API 级别 21)或更高版本,请调用 FLAG_HANDLES_MEDIA_BUTTONS MediaBrowserCompat.ConnectionCallback.onConnected。 This will automatically call your media controller's dispatchMediaButtonEvent(), which translates the key code to a media session callback.
这将自动调用您的媒体控制器的 dispatchMediaButtonEvent(),它将键代码转换为媒体 session 回调。
I find this stupid, as I do not need this extra module to play media.我觉得这很愚蠢,因为我不需要这个额外的模块来播放媒体。 All I need is the detection of the headset button press.
我只需要检测耳机按钮按下。 Anyway, I tried to implement the MediaSession but gave up soon as it quickly ended up with too many codes that were useless for my app since my app is NOT an audio player app!
无论如何,我尝试实现 MediaSession 但很快就放弃了,因为我的应用程序不是音频播放器应用程序,所以很快就结束了太多对我的应用程序无用的代码!
Any suggestion?有什么建议吗? What should I do?
我应该怎么办?
I found the solution myself finally, Instead of making a separate BroadcastReceiver class.我自己终于找到了解决方案,而不是单独制作 BroadcastReceiver class。 I added a public static class in my MainActivity:java:
我在我的 MainActivity:java 中添加了一个公共 static class:
public static class ButtonReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
Toast.makeText(context, "debug media button test", Toast.LENGTH_SHORT).show();
...
}
}
Then added this line in onCreate:然后在 onCreate 中添加这一行:
((AudioManager)getSystemService(AUDIO_SERVICE))
.registerMediaButtonEventReceiver(new ComponentName(this, ButtonReceiver.class));
And finally in AndroidManifest.xml:最后在 AndroidManifest.xml 中:
<receiver android:name=".MainActivity$ButtonReceiver"
android:enabled="true">
<intent-filter
android:priority="10000">
<action android:name="android.intent.action.MEDIA_BUTTON"/>
</intent-filter>
</receiver>
It works!有用! The app can now catch the MEDIA_BUTTON intent with this setting!
该应用程序现在可以使用此设置捕获 MEDIA_BUTTON 意图!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.