Android 通知显示与前台使用寿命周期的关系
在我的应用程序中,我向前台提供了一个服务,并向用户显示一个通知。通过Android 通知显示与前台使用寿命周期的关系,android,service,notifications,foreground-service,Android,Service,Notifications,Foreground Service,在我的应用程序中,我向前台提供了一个服务,并向用户显示一个通知。通过notification.setAutoCancel(false)我设置通知图标始终显示而不自动取消 实际上,我需要这个服务长时间运行,我想让用户意识到这一点(通过看到通知图标,他们知道服务仍在运行) 所以我想知道我的假设是否正确,只要显示通知图标,就意味着服务仍在运行,而一旦通知图标消失,就意味着服务出于某些原因而完成。 以下是简化代码: LongLiveService extends Service{ private
notification.setAutoCancel(false)代码>我设置通知图标始终显示而不自动取消
实际上,我需要这个服务长时间运行,我想让用户意识到这一点(通过看到通知图标,他们知道服务仍在运行)
所以我想知道我的假设是否正确,只要显示通知图标,就意味着服务仍在运行,而一旦通知图标消失,就意味着服务出于某些原因而完成。
以下是简化代码:
LongLiveService extends Service{
private MyThread thread;
onStartCommand() {
thread.start();
createNotification(this);
}
createNotification(Context context) {
// build a notification
notification.setAutoCancel(false);
startForeground.invoke(this, notificationID, notification.build());
}
}
MyThread extends Thread{
run() {
// doing something all the time
}
}
你写道:
一旦通知图标消失,这意味着服务由于某些原因而完成
如果您希望通知在服务运行期间保持在状态栏中,则需要通过标志标志持续事件
,使通知保持不变,因此不能解除:
Notification notification = new Notification(...);
PendingIntent pendingIntent = PendingIntent.getActivity(..., Notification.FLAG_ONGOING_EVENT);
notification.flags = Notification.FLAG_ONGOING_EVENT;
notification.setLatestEventInfo(..., pendingIntent);
mNotificationManager.notify(1, notification);
你写道:
一旦通知图标消失,这意味着服务由于某些原因而完成
如果您希望通知在服务运行期间保持在状态栏中,则需要通过标志标志持续事件
,使通知保持不变,因此不能解除:
Notification notification = new Notification(...);
PendingIntent pendingIntent = PendingIntent.getActivity(..., Notification.FLAG_ONGOING_EVENT);
notification.flags = Notification.FLAG_ONGOING_EVENT;
notification.setLatestEventInfo(..., pendingIntent);
mNotificationManager.notify(1, notification);
感谢回复,我使用pendingent.getActivity(…,pendingent.FLAG_UPDATE_CURRENT)代码>到目前为止,这一切都很好,那么FLAG_UPDATE_CURRENT
和FLAG_consuming_EVENT
之间有什么区别呢?我已经阅读了谷歌文档,它让人困惑不解。感谢回复,我使用pendingent.getActivity(…,pendingent.FLAG_UPDATE_CURRENT)代码>到目前为止,这一切都很好,那么FLAG\u UPDATE\u CURRENT
和FLAG\u continuous\u EVENT
之间有什么区别呢?我已经阅读了谷歌文档,它让人困惑。