我确实有一项服务是从前台开始的:
val notification = NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_notify)
.setContentTitle(title)
.setTicker(message)
.setStyle(NotificationCompat.BigTextStyle().bigText(message))
.setContentText(message)
.setContentIntent(pendingIntent)
.build()
startForeground(Notifications.Id.RUNNING, notification)
请注意,我是,而不是使用setOngoing(true)
的。
我在StackOveflow上找到了一些例子和答案,有些人正在使用setOngoing(true)
,有些人没有使用。
还有,Android文档上说
前台服务是用户主动意识到的服务,而不是系统在内存不足时杀死的候选服务。前台服务必须为状态栏提供通知,状态栏放在正在进行的标题下。--这意味着,除非服务停止或从前台中删除,否则不能拒绝通知。
而且,在文档中,没有设置setOngoing(true)
:
Intent notificationIntent = new Intent(this, ExampleActivity.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification =
new Notification.Builder(this, CHANNEL_DEFAULT_IMPORTANCE)
.setContentTitle(getText(R.string.notification_title))
.setContentText(getText(R.string.notification_message))
.setSmallIcon(R.drawable.icon)
.setContentIntent(pendingIntent)
.setTicker(getText(R.string.ticker_text))
.build();
startForeground(ONGOING_NOTIFICATION_ID, notification);
问题
省略setOngoing(true)
的影响是什么?
https://stackoverflow.com/questions/48186800
复制相似问题