Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何正确更新api 11之后的通知?_Android_Android Notifications_Android 4.2 Jelly Bean - Fatal编程技术网

Android 如何正确更新api 11之后的通知?

Android 如何正确更新api 11之后的通知?,android,android-notifications,android-4.2-jelly-bean,Android,Android Notifications,Android 4.2 Jelly Bean,在出现之前,通知托盘中已经存在的通知是调用setLatestEventInfo(),然后通过NotificationManager.notify()调用将通知发送回,其ID与您进行的第一次notify()调用相匹配 现在不推荐使用以下消息:改用Notification.Builder。但是我找不到任何关于如何使用notification.Builder正确更新通知的文档 是否每次需要更新通知时都要重新创建一个新的通知实例?然后用您以前使用的ID将其传递给NotificationManager.n

在出现之前,通知托盘中已经存在的通知是调用
setLatestEventInfo()
,然后通过
NotificationManager.notify()
调用将通知发送回,其ID与您进行的第一次
notify()
调用相匹配

  • 现在不推荐使用以下消息:
    改用Notification.Builder
    。但是我找不到任何关于如何使用
    notification.Builder
    正确更新通知的文档

  • 是否每次需要更新通知时都要重新创建一个新的
    通知
    实例?然后用您以前使用的ID将其传递给NotificationManager.notify()

  • 这似乎是可行的,但我想看看是否有人有任何官方证据证明这是一种新的“方法”


  • 我问这个问题的真正原因是因为在
    Android 4.1.1 Jelly Bean
    中,每次调用
    notify()
    时,通知都会闪烁。当用这个更新进度条时,看起来很糟糕,很难点击通知。在4.1或以前的版本中,情况并非如此。因此,我希望在提交bug之前,确保正确执行此操作

    您所做的是正确的,您只是缺少了可以设置的标志。我不知道您的特定通知实现,但您可以考虑使用:

    setOngoing(boolean ongoing)
    

    我猜(因为我刚才遇到了同样的问题)您在通知中使用了RemoteView。我成功地更新了通知,但通知没有像这样闪烁:

    RemoteViews views;
    if( this.mNotification == null) {
        views = new RemoteViews(getPackageName(), R.layout.notification);
        this.mNotification = new Notification.Builder(this)
            .setContent(views)
            .setSmallIcon(R.drawable.status_icon)
            .setContentIntent(mNotificationAction)
            .setOngoing(true)
            .setOnlyAlertOnce(true)
            .getNotification();
    } else {
        views = this.mNotification.contentView;
    }
    

    感谢@seanmonstar的回答。

    我通过调用Notification.Builder上的
    setWhen(0)
    解决了这个问题。这个参数的Android默认值似乎不适合在整个通知淡出/淡入的情况下更新通知视图的位

    Notification.Builder builder = new Notification.Builder(c)
                    .setContentTitle("Notification Title")
                    .setSmallIcon(R.drawable.ic_notification_icon)
                    .setProgress(max_progress,current_progress,false)
                    .setWhen(0);
                    notification = builder.getNotification();
    
    mNotificationManager.notify(NOTIFICATION_ID, notification);
    
    更新: 如上所述,使用
    when=0
    不是一种优雅的方式。我形成了如下解决方案:

    if(mNotif == null) {
    //either setting mNotif first time
    //or was lost when app went to background/low memory
        mNotif = createNewNotification();
    }
    else {
        long oldWhen = mNotif.when;
        mNotif = createNewNotification();
        mNotif.when = oldWhen;
    }
    mNotificationManager.notify(NOTIFICATION_ID, mNotif);
    

    此处描述的解决方案效果良好:

    关键是要使用以重用生成器和setOnlyAlertOnce(true):


    您从哪里更新通知?我假设是一个服务..?您还可以添加用于创建/更新通知的代码吗?您应该使用
    setWhen(Calendar.getInstance().getTimeInMillis())
    而不是
    setWhen(0)
    @user1521536为什么我们要这样做?谢谢。你的回答使我找到了正确的解决办法。但你应该为这次活动安排一段有意义的时间。如果更新使用相同的有意义时间,通知将不会闪烁/闪烁。只有当时间戳发生变化时(在没有明确设置
    的情况下,
    才会发生),您的通知才会闪烁/闪烁。请记住,
    when
    字段可能用于排序-现在或将来。因此,将其设置为
    0
    可能会导致各种意外的问题。@Wolframritmeyer我认为我们可以这样做来避免从系统中获得时间(因为它只会显示闪烁效果):
    long oldWhen=mNotif.when;mNotif=createNewNotification();mNotif.when=oldWhen
    @sufian Well
    mNotif
    可能超出范围(例如,当用户刷走活动时)。这取决于你有什么。如果您有一个长期运行的服务,这当然不是问题。尽管如此,每个通知都有一个自然的开始时间戳。例如,当用户开始播放歌曲时。或者gcm消息的时间戳,或者一天的开始,或者进球的时间,或者。。。如果您总是使用这种自然时间戳,一切正常。在设置startForeground()并在同一通知上使用mNotificationManager.notify()后,您是否设法消除了闪烁?
    if(mNotif == null) {
    //either setting mNotif first time
    //or was lost when app went to background/low memory
        mNotif = createNewNotification();
    }
    else {
        long oldWhen = mNotif.when;
        mNotif = createNewNotification();
        mNotif.when = oldWhen;
    }
    mNotificationManager.notify(NOTIFICATION_ID, mNotif);
    
    if (firstTime) {
      mBuilder.setSmallIcon(R.drawable.icon)
      .setContentTitle("My Notification") 
      .setOnlyAlertOnce(true); 
      firstTime = false;
    } 
    mBuilder.setContentText(message)
    .setProgress(100, progress, true);
    
    mNotificationManager.notify(mNotificationId, mBuilder.build());