Ale*_*ino 6 java android android-broadcastreceiver android-8.0-oreo
我有新的Android版本的问题,即8.0(奥利奥).我必须注册广播,我使用以下代码执行此操作:
// android.intent.action.MEDIA_BUTTON
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
r = new MediaButtonIntentReceiver();
// this line sets receiver priority
filter.setPriority(999);
registerReceiver(r, filter);
Run Code Online (Sandbox Code Playgroud)
这适用于较旧的Android版本,但在Android 8上这不起作用,因为它必须注册显式广播,但我不知道如何.如何注册显式广播来检测媒体按钮?
这是在Manifest.xml中:
<receiver android:name=".MediaButtonIntentReceiver">
<intent-filter android:priority="999">
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
如果要接收媒体按钮,则必须播放音频并使用mediasession。
ig
MediaSession ms = new MediaSession(getApplicationContext(), getPackageName());
ms.setActive(true);
ms.setCallback(new MediaSession.Callback() {
@Override
public boolean onMediaButtonEvent(Intent mediaButtonIntent) {
Log.e("hmhm", "hmhm media button");
return super.onMediaButtonEvent(mediaButtonIntent);
}
});
// you can button by receiver after terminating your app
ms.setMediaButtonReceiver(mbr);
// play dummy audio
AudioTrack at = new AudioTrack(AudioManager.STREAM_MUSIC, 48000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT,
AudioTrack.getMinBufferSize(48000, AudioFormat.CHANNEL_OUT_STEREO, AudioFormat.ENCODING_PCM_16BIT), AudioTrack.MODE_STREAM);
at.play();
// a little sleep
at.stop();
at.release();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1532 次 |
| 最近记录: |