【问题标题】:ACTION_MEDIA_BUTTON Extras now returning null after Samsung updateACTION_MEDIA_BUTTON Extras 现在在三星更新后返回 null
【发布时间】:2021-11-20 15:16:51
【问题描述】:

在三星更新后,任何收到的 ACTION_MEDIA_BUTTON Intent 额外内容现在都为空。

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>

这已经工作了很长时间。我需要对代码进行哪些调整?

有任何故障排除建议吗?

或者我的应用是否需要在手机设置中重新授予某种类型的权限?

【问题讨论】:

    标签: android broadcastreceiver


    【解决方案1】:

    我在三星手机上遇到了同样的问题。

    这是工作代码:

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

    【讨论】:

      猜你喜欢
      • 2014-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多