You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android 13+ MediaStyle通知调用setPlaybackState后自定义按钮消失问题

Android MediaStyle通知自定义按钮与播放状态更新冲突问题

我在Android应用中使用MediaStyle通知,并添加了自定义操作按钮,代码如下:

NotificationCompat.Builder myNotifBuilder;
// ...
androidx.media.app.NotificationCompat.MediaStyle ms = 
         new androidx.media.app.NotificationCompat.MediaStyle();                   
ms.setMediaSession(mSession.getSessionToken());
myNotifBuilder.setStyle(ms)
    .addAction(new NotificationCompat.Action(R.drawable.clipboard_speak,
            "Paste", pIntent5))
    .addAction(new NotificationCompat.Action(R.drawable.fb_on,
            "Floating button", pIntent7));
// ...
myNotifBuilder.build();

生成的通知原本包含这些自定义按钮,但只要调用mSession.setPlaybackState(state),自定义按钮就会消失,被ACTION_SKIP_TO_PREVIOUS按钮、全屏进度条和ACTION_SKIP_TO_NEXT按钮替代,调用代码如下:

PlaybackStateCompat state = new PlaybackStateCompat.Builder()
        .setState(isTalking() ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED,
                progress * 1000, 1f)
        .setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE
                        | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
        );
try {
    mSession.setPlaybackState(state);
} catch (Exception ex) {
    ex.printStackTrace();
}

我知道可以通过PlaybackStateCompat的state.addCustomAction()添加按钮,但无法像NotificationCompat.Builder的addAction()那样直接设置PendingIntent。这两个自定义按钮需要实现点击后隐藏通知栏并打开主界面的效果,如果在自定义动作的回调中启动界面,通知栏不会消失,而这是必须实现的要求。

另一种方案是不调用mSession.setPlaybackState(state),完全依赖通知构建器添加的按钮,但这样会出现新问题:在Android 13中无法显示进度条,Android 14中虽有进度条但无法更新,陷入两难。

更新:已将该问题提交至Android问题跟踪器。


内容的提问来源于stack exchange,提问作者gregko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 10:42:44