我有这个简单的接收器:
public class RemoteControlReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
Toast.makeText(context, "Something received", Toast.LENGTH_SHORT).show();
}
}
并且应该在按下音量按钮时显示此Toast消息。另外,我在清单文件中注册了它:
<receiver android:name=".RemoteControlReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
我目前正在使用API26。我在这里阅读过此内容:
https://android-developers.googleblog.com/2010/06/allowing-applications-to-play-nicer.html
所以,我什至可以在这里使用KeyEvent,对吗?但这并没有显示任何内容。...可能的帮助,请?
尝试使用此Java代码: