如果要更新startForeground()设置的通知,只需构建新通知,然后使用NotificationManager通知它。
关键是使用相同的通知ID。
我没有测试反复调用startForeground()来更新Notification的场景,但我认为使用NotificationManager.notify会更好。
更新通知不会从前台状态中删除服务(这只能通过调用stopForground来完成);
例:
private static final int NOTIF_ID=1;
@Override
public void onCreate (){
this.startForeground();
}
private void startForeground() {
startForeground(NOTIF_ID, getMyActivityNotification(""));
}
private Notification getMyActivityNotification(String text){
// The PendingIntent to launch our activity if the user selects
// this notification
CharSequence title = getText(R.string.title_activity);
PendingIntent contentIntent = PendingIntent.getActivity(this,
0, new Intent(this, MyActivity.class), 0);
return new Noti