notificationManager.notify 和 Android 中的 startForeground 有什么区别?
What is the difference between notificationManager.notify and startForeground in Android?
我只是想知道 Android 中的 NotificationManager.notify
和 startForeground
有什么区别。
以这种方式使用 NotificationManager.notify
you can post as many updates to a notification as you like including adjustments to progress bars via Noticiation.Builder.setProgress
您只向用户显示一个通知,并且它是 startForeground
所需的通知。
当你想更新一个由startForeground()设置的Notification时,只需建立一个新的notification,然后使用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 Notification.Builder(this)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.ic_launcher_b3)
.setContentIntent(contentIntent).getNotification();
}
/**
* this is the method that can be called to update the Notification
*/
private void updateNotification() {
String text = "Some text that will update the notification";
Notification notification = getMyActivityNotification(text);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notif_id, notification);
}
您可以在 NotificationManager.notify
here
上找到更多示例和说明
我还建议您参考此 page 以了解有关 startForeground
的更多信息
可以找到 startForeground
的用法 here
我只是想知道 Android 中的 NotificationManager.notify
和 startForeground
有什么区别。
以这种方式使用 NotificationManager.notify
you can post as many updates to a notification as you like including adjustments to progress bars via Noticiation.Builder.setProgress
您只向用户显示一个通知,并且它是 startForeground
所需的通知。
当你想更新一个由startForeground()设置的Notification时,只需建立一个新的notification,然后使用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 Notification.Builder(this)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.ic_launcher_b3)
.setContentIntent(contentIntent).getNotification();
}
/**
* this is the method that can be called to update the Notification
*/
private void updateNotification() {
String text = "Some text that will update the notification";
Notification notification = getMyActivityNotification(text);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notif_id, notification);
}
您可以在 NotificationManager.notify
here
我还建议您参考此 page 以了解有关 startForeground
可以找到 startForeground
的用法 here