Android appcompat v7 支持库通知紧凑视图重复按钮问题

Android appcompat v7 support library Notification compact view repeating buttons issue

这是我用来 retrieve/create/update 服务 class 中的应用程序通知的方法,称为 PlayerService:

    import android.support.v7.app.NotificationCompat;
    import android.app.Notification;
    import android.app.NotificationManager;

    // ...
        private Notification getCompatNotification(String contentText) {
                m_notificationBuilder
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle("PlayerService")
                        .setContentText(contentText)
                        .setContentIntent(null)
                        .setWhen(0)
                        .setShowWhen(false)
                        .addAction(android.R.drawable.ic_media_previous, "", null)
                        .addAction((isPlaying() ? android.R.drawable.ic_media_pause : android.R.drawable.ic_media_play), "", null)
                        .addAction(android.R.drawable.ic_media_next, "", null)
                        .setStyle(new NotificationCompat.MediaStyle()                                    
                                .setShowActionsInCompactView(0, 1, 2)
                                .setShowCancelButton(true)
                                .setCancelButtonIntent(null))
                        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                        .setPriority(NotificationCompat.PRIORITY_MAX);

                Notification notification = m_notificationBuilder.build();

                return notification;
            }

现在,当媒体播放器 activity 打开但未开始播放时,通知会显示带有 3 个操作按钮(上一个、play/pause、下一个)的大视图,但是当播放启动后,通知视图变为紧凑型,并首先显示这 3 个按钮,然后再次显示第一个和第二个按钮。请看图片。
测试设备有 KitKat 4.4.4。

不播放



播放开始


要更新通知:

private void updateNotification(String contentText){
                nm.notify(NOTIFICATION_ID, getCompatNotification(contentText));
}

onCreate()中:

@Override
    public void onCreate() {
        super.onCreate();
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        m_notificationBuilder = new NotificationCompat.Builder(this);
}

onStartCommand()中:

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
                startForeground(NOTIFICATION_ID, getCompatNotification("Test Content"));
        return START_STICKY; // run until explicitly stopped.
    }


无法弄清楚是什么问题。非常感谢任何帮助。

希望这会有所帮助:将 android:stateListAnimator="@null" 添加到您的按钮

您正在使用相同的 NotificationCompat.Builder 实例来创建通知,每当您添加 Action 时,它都会添加到之前的 List 并在系统 Api 中创建不需要的行为。如果要使用相同的 Builder 实例,则必须先在 Builder 上调用 clearActions(),然后再添加任何内容。