android ACTION_MEDIA_BUTTON Extras现在在Samsung更新后返回null

watbbzwu  于 2023-04-10  发布在  Android
关注(0)|答案(2)|浏览(145)

Samsung更新后,任何收到的ACTION_MEDIA_BUTTON Intent extra现在都为空。
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>

这已经工作了很长时间。我需要对代码做什么调整?
有任何故障排除建议吗?
或者我的应用是否需要在手机设置中重新授予某种类型的权限?

jei2mxaa

jei2mxaa1#

我在三星手机上遇到了同样的问题。
下面是工作代码:

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);
cngwdvgl

cngwdvgl2#

我遇到了同样的问题,它就在那里:

Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setClass(this, MyMediaButtonReceiver.class);
int flags = 0;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
    flags = PendingIntent.FLAG_MUTABLE;
}

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, flags);

mediaSessionCompat.setMediaButtonReceiver(pendingIntent);

当我把flag改为MUTABLE时,它就开始工作了。

相关问题