在三星更新后,任何接收到的ACTION_MEDIA_BUTTON意图附加信息现在都为空。
Receiver.java:
@Override
public void onReceive(Context context, Intent intent) {
if (!Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {return;}
// This is now coming back null
KeyEvent event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
// ... do something
}
AndroidManifest.xml:
<uses-permission android:name="android.permission.BLUETOOTH" />
<!-- stuff -->
<receiver
android:name=".RemoteControlReceiver"
android:enabled="true"
android:exported="true">
<intent-filter android:priority="999">
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
这已经用了很长一段时间了。我需要对代码做什么调整?
有什么疑难解答建议吗?
或者,我的应用程序是否需要在手机设置中重新授予某种类型的权限?
发布于 2022-02-24 18:37:52
我在三星手机上也有同样的问题。
以下是工作代码:
ComponentName componentName = new ComponentName(getContext(), MediaButtonReceiver.class);
final Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(componentName);
PendingIntent mediaButtonReceiverPendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mediaButtonReceiverPendingIntent = PendingIntent.getBroadcast(getContext(), 0, mediaButtonIntent, PendingIntent.FLAG_IMMUTABLE);
} else {
mediaButtonReceiverPendingIntent = PendingIntent.getBroadcast(getContext(), 0, mediaButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
Repository.getInstance().mediaSessionCompat = new MediaSessionCompat(getContext(), "radio_playback_channel");
Repository.getInstance().mediaSessionCompat.setCallback(new MediaSessionCompat.Callback()
{
@Override
public boolean onMediaButtonEvent(Intent mediaButtonEvent)
{
String intentAction = mediaButtonEvent.getAction();
if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
Log.i (TAG, "no media button information");
return super.onMediaButtonEvent(mediaButtonEvent);
}
KeyEvent keyEvent = (KeyEvent)mediaButtonEvent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (keyEvent == null) {
return super.onMediaButtonEvent(mediaButtonEvent);
}
if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
Log.i (TAG, "Pushed media button " + String.valueOf(keyEvent.getKeyCode()));
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PAUSE && Repository.getInstance().playerService.isPlaying()) {
Repository.getInstance().playerService.stop();
Repository.getInstance().playStopBtn.setImageResource(R.drawable.ic_play);
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_MEDIA_PLAY && !Repository.getInstance().playerService.isPlaying()) {
Repository.getInstance().playerService.play(streamUrl);
Repository.getInstance().playStopBtn.setImageResource(R.drawable.ic_stop);
}
}
return super.onMediaButtonEvent(mediaButtonEvent);
}
});
Repository.getInstance().mediaSessionCompat.setActive(true);
Repository.getInstance().mediaSessionCompat.setMediaButtonReceiver(mediaButtonReceiverPendingIntent);
通知管理人员:
Repository.getInstance().player.setHandleAudioBecomingNoisy(true);
Repository.getInstance().player.setWakeMode(C.WAKE_MODE_NETWORK);
PlayerNotificationManager.Builder playerNotificationManagerBuilder = new PlayerNotificationManager.Builder(
this,
100,
"rve_playback_channel",
new DescriptionAdapter(this)
);
playerNotificationManagerBuilder.setChannelImportance(NotificationUtil.IMPORTANCE_DEFAULT);
playerNotificationManagerBuilder.setChannelNameResourceId(R.string.app_name);
playerNotificationManager = playerNotificationManagerBuilder.build();
playerNotificationManager.setUseNextAction(true);
playerNotificationManager.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
playerNotificationManager.setPlayer(Repository.getInstance().player);
// omit the stop action
playerNotificationManager.setUseStopAction(false);
playerNotificationManager.setMediaSessionToken(Repository.getInstance().mediaSessionCompat.getSessionToken());
playerNotificationManager.setUseChronometer(false);
playerNotificationManager.setColorized(true);
playerNotificationManager.setSmallIcon(R.drawable.ic_logo);
playerNotificationManager.setBadgeIconType(NotificationCompat.BADGE_ICON_LARGE);
https://stackoverflow.com/questions/70047218
复制相似问题