我只是好奇了解在Android中NotificationManager.notify
和startForeground
之间有什么区别。
我只是好奇了解在Android中NotificationManager.notify
和startForeground
之间有什么区别。
NotificationManager.notify
,您可以发布任意数量的通知更新,包括通过Noticiation.Builder.setProgress
调整进度条。这样,您只向用户显示一个通知,并且它是由startForeground
所需的通知。startForeground()
以更新通知的情况,但我认为使用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
的例子和解释。startForeground
的内容。
startForeground
的用法可以在这里找到。
startForeground
方法,否则我们只需使用NotificationManagerCompat.from(serviceContext).notify(myId, builder.build());
技术进行更新。 - Top-MasterVpnService
在断开连接之前提供自己的通知,但是如果您需要该服务保持活动状态以执行其他任务,即使VPN已断开连接,您需要使用startForeground
方法(如果服务在后台运行而没有任何打开的活动),但只需第一次更新后使用NotificationManager
。 - Top-Master