前奏
我正在尝试在通知上添加计时器。计时器是一项服务。这一行每秒都被调用(继续Thread是一个“运行”布尔值,timeString是精心设计的String,显示时间):
NotificationChrono.updateNotification(getApplicationContext(), continueThread,
NOTIF_ID, timeString, "Chronometer", notificationManager);
这是NotificationChrono类:
public class NotificationChrono {
static public void updateNotification(Context context, boolean running,
int id, String title, String text,
NotificationManager notificationManager) {
Intent stopIntent = new Intent("com.corsalini.david.barcalc.STOP");
PendingIntent stopPendingIntent = PendingIntent.getBroadcast(context,
0, stopIntent, 0);
Intent startIntent = new Intent(
"com.corsalini.david.barcalc.STARTPAUSE");
PendingIntent startPendingIntent = PendingIntent.getBroadcast(context,
0, startIntent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context)
.setContentText(context.getString(R.string.notif_text))
.setContentTitle(title)
.setSmallIcon(R.drawable.ic_action_alarm_2)
.setAutoCancel(false)
.setOngoing(running)
.setOnlyAlertOnce(true)
.setContentIntent(
PendingIntent.getActivity(context, 10, new Intent(
context, FrontActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP), 0))
.addAction(
running ? R.drawable.ic_action_pause
: R.drawable.ic_action_play,
running ? context.getString(R.string.pause) : context
.getString(R.string.start), startPendingIntent)
.addAction(R.drawable.ic_action_stop,
context.getString(R.string.stop), stopPendingIntent);
notificationManager.notify(id, builder.build());
}
}
问题
每隔一秒删除并重新创建通知,视觉上意味着通知每秒消失并重新出现在通知列表中。
我想要的只是更新TITLE文本,而不是每秒都重新创建通知。有可能吗?
答案 0 :(得分:95)
确保每次都使用相同的NotificationCompat.Builder
构建器来创建Notification
!
虽然第一次必须设置所有内容,但第二次使用Builder
时,您只需设置要更新的值。之后,就像你一样调用notificationManager.notify(id, builder.build())
。如果您使用相同的ID
,则通知会更新(重要!)。
示例:
//First time
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentText(context.getString(R.string.notif_text))
.setContentTitle(title)
.setSmallIcon(R.drawable.ic_action_alarm_2)
.setAutoCancel(false)
.setOngoing(running)
.setOnlyAlertOnce(true)
.setContentIntent(
PendingIntent.getActivity(context, 10,
new Intent(context, FrontActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP),
0)
)
.addAction(running ? R.drawable.ic_action_pause
: R.drawable.ic_action_play,
running ? context.getString(R.string.pause)
: context.getString(R.string.start),
startPendingIntent)
.addAction(R.drawable.ic_action_stop, context.getString(R.string.stop),
stopPendingIntent);
notificationManager.notify(id, builder.build());
//Second time
builder.setContentTitle(title);
notificationManager.notify(id, builder.build());
但您也可以使用NotificationCompat
类的setUsesChronometer
方法。这会使用给定的时间戳自动显示计时器(能够使用setWhen设置)。
答案 1 :(得分:3)
感谢PieterAelse的想法!
在我的情况下,我必须在第二次及以下时间使用此内容(构建新通知):
builder.setContentText(getConvertedTime(millisUntilFinished));
Notification notification = builder.getNotification();
notification.flags = Notification.FLAG_ONGOING_EVENT;
nm.notify(R.string.app_name,notification);
希望它有所帮助。
答案 2 :(得分:3)
您需要将通知标记为“仅一次发送警报”
Builder#setOnlyAlertOnce(true)