how*_*ttl 40 notifications android android-3.0-honeycomb
我有一个无线连接到其他设备的服务.启用该服务后,我会持续发出通知,说明它已启用.
启用该服务后,用户将连接到另一台设备.此时,我想更新我正在进行的通知,以说明已连接的设备的名称.通过startForeground(ONGOING_NOTIFICATION, notification)使用更新的信息再次呼叫,这很容易做到; 但每次调用时,它会在条形图上闪烁通知.我真正想要的是通知在后台静默更新而不会在通知栏上闪烁,因此用户在打开通知区域之前不会知道区别.
有没有在没有调用的情况下更新通知startForeground()?
此行为仅发生在Honeycomb中.姜饼设备(我假设Froyo等)表现出理想的方式.
Chr*_*dus 60
我也经历过这个问题,在先前的评论和一些挖掘的帮助下,我找到了解决方案.
如果您不希望在更新时闪烁通知,或者不断地占用设备的状态栏,您必须:
如果你每次都使用一个新的构建器,那么我猜测Android必须重新重建视图,导致它暂时消失.
一些好代码的例子:
class NotificationExample extends Activity {
private NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
private mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//Different Id's will show up as different notifications
private int mNotificationId = 1;
//Some things we only have to set the first time.
private boolean firstTime = true;
private updateNotification(String message, int progress) {
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());
}
}
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,您可以使用消息和进度(0-100)调用updateNotification(String,int),它将更新通知而不会让用户烦恼.
den*_*nko 23
您应该更新现有通知https://developer.android.com/training/notify-user/build-notification.html#Updating
这对我有用,因为正在进行的活动(而不是服务)通知会"无声地"更新.
NotificationManager notifManager; // notifManager IS GLOBAL
note = new NotificationCompat.Builder(this)
.setContentTitle(YOUR_TITLE)
.setSmallIcon(R.drawable.yourImageHere);
note.setOnlyAlertOnce(true);
note.setOngoing(true);
note.setWhen( System.currentTimeMillis() );
note.setContentText(YOUR_MESSAGE);
Notification notification = note.build();
notifManager.notify(THE_ID_TO_UPDATE, notification );
Run Code Online (Sandbox Code Playgroud)