更新现有通知

3
现在最新的Android SDK中,setLatestEventInfo方法已经被弃用了,那么更新现有通知内容的方式是什么呢?我怀疑他们希望我们每次想要更新通知内容时都创建一个新的通知。

通知指南似乎也没有更新以使用建议的Notification.Builder类。
2个回答

2

在 Notification 类中有许多公共字段,您可以直接更改它们,只需保留对它的引用即可。但是,它的其他特性显然不是设计为可更改的(如果通知发生了重大变化,可能会令用户感到困惑)。

您需要使用已弃用的方法或重新构建 Notification 并显示一个新的。

http://developer.android.com/reference/android/app/Notification.html


0

是的,您可以更新现有通知的内容。但是您必须以相似的方式重新构建它,并使用相同的通知ID。还要删除所有相关标志并将优先级设置为默认。

相同的通知ID确保不会创建新的通知。像振动和灯光之类的标志可能不适用,因为通知已经启动。将优先级设置为默认,以便通知在通知托盘中的位置不会改变。如果将优先级设置为高,则它将移动到托盘的顶部。

这是我的实现方式。*我只添加了相关代码

public void showNotification(String action){
    RemoteViews views = new RemoteViews(getPackageName(),
            R.layout.status_bar);
    Notification status;
    if(action.equals("play")){
        views.setImageViewResource(R.id.status_bar_play, R.drawable.play);
    status = notificationBuilder
            .setSmallIcon(R.drawable.ic_launcher)
            .setPriority(Notification.PRIORITY_HIGH)
            .setContentIntent(pendingIntent)
            .setContent(views)
            .setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)
            .build();
    } else {
        views.setImageViewResource(R.id.status_bar_play, R.drawable.pause);
        status = notificationBuilder
            .setSmallIcon(R.drawable.ic_launcher)
            .setPriority(Notification.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .setContent(views)
            .build();
    }
    NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(Constants.NOTIFICATION_ID, status);
}

通过这种方式可以修改现有通知的内容。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接