【问题标题】:Android appcompat v7 support library Notification compact view repeating buttons issueAndroid appcompat v7 支持库通知紧凑视图重复按钮问题
【发布时间】:2016-06-01 03:42:31
【问题描述】:

这是我在名为 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;
            }

现在,当媒体播放器活动打开但播放未开始时,通知会显示带有这 3 个操作按钮(上一个、播放/暂停、下一个)的大视图,但当开始播放时,通知视图会变为紧凑型和首先显示这 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 android-notifications android-appcompat


    【解决方案1】:

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

    【讨论】:

    • 您的回答提出了一个很好的观点,但是对于 NotificationCompat.Builder,没有 clearActions() 这样的方法。除此之外,无法理解为什么在任何地方都没有指定?使用相同的构建器对象进行创建/更新操作不是一个好习惯吗?
    • 好的,看起来有人请求添加这些方法(clearActions 和 removeAction)-[问题 207040]:(code.google.com/p/android/issues/detail?id=207040)。现在,我可以使用m_notificationBuilder.mActions.clear();@mariusc,非常感谢!
    【解决方案2】:

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

    【讨论】:

    • 我的按钮是由 notificationBuilder 对象的 addActions() 方法创建的,它不是自定义布局,它是允许使用 MediaStyle 样式构建通知的 v7 库支持。
    猜你喜欢
    • 2015-10-04
    • 2014-03-03
    • 1970-01-01
    • 2017-06-20
    • 2014-12-31
    • 1970-01-01
    • 2014-12-30
    • 2016-11-20
    • 2019-11-17
    相关资源
    最近更新 更多