首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ACTION_MEDIA_BUTTON Extras在三星更新后返回null

ACTION_MEDIA_BUTTON Extras在三星更新后返回null
EN

Stack Overflow用户
提问于 2021-11-20 15:16:51
回答 1查看 288关注 0票数 0

在三星更新后,任何接收到的ACTION_MEDIA_BUTTON意图附加信息现在都为空。

Receiver.java:

代码语言:javascript
运行
复制
    @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:

代码语言:javascript
运行
复制
        <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>

这已经用了很长一段时间了。我需要对代码做什么调整?

有什么疑难解答建议吗?

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

EN

回答 1

Stack Overflow用户

发布于 2022-02-24 18:37:52

我在三星手机上也有同样的问题。

以下是工作代码:

代码语言:javascript
运行
复制
    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);

通知管理人员:

代码语言:javascript
运行
复制
    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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70047218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档