语言

Service.StartForeground 方法

定义

重载

名称 说明
StartForeground(Int32, Notification)

如果服务已启动(正在运行 Context#startService(Intent)),则还会使此服务在前台运行,从而在处于此状态时向用户显示正在进行的通知。

StartForeground(Int32, Notification, ForegroundService)

具有其他 foregroundServiceType 参数的 #startForeground(int, Notification) 重载版本。

StartForeground(Int32, Notification)

如果服务已启动(正在运行 Context#startService(Intent)),则还会使此服务在前台运行,从而在处于此状态时向用户显示正在进行的通知。

[Android.Runtime.Register("startForeground", "(ILandroid/app/Notification;)V", "")]
public void StartForeground(int id, Android.App.Notification? notification);
[<Android.Runtime.Register("startForeground", "(ILandroid/app/Notification;)V", "")>]
member this.StartForeground : int * Android.App.Notification -> unit

参数

id
Int32

此通知的 NotificationManager#notify(int, Notification) NotificationManager.notify(int, Notification)标识符不得为 0。

notification
Notification

要显示的通知。

特性

注解

如果服务已启动(通过 Context.startService(Intent)运行),则还会使此服务在前台运行,从而提供在处于此状态时向用户显示的持续通知。 默认情况下,启动的服务是后台服务,这意味着不会为其进程提供前台 CPU 计划(除非该进程中的其他内容是前台),并且,如果系统需要终止它们以回收更多内存(例如在 Web 浏览器中显示大型页面),则它们可以被终止,而不会造成太大的伤害。 如果使用 startForeground(int,Notification),如果终止服务会对用户造成干扰,例如服务正在执行后台音乐播放,则用户会注意到其音乐是否停止播放。 请注意,调用此方法不会使服务本身处于启动状态,即使名称听起来像是它。 必须始终调用 startService(Intent)来告知系统它应使服务保持运行状态,然后使用此方法告诉它使其更加困难地运行。

面向 API Build.VERSION_CODES的应用。P 或更高版本必须请求权限Manifest.permission.FOREGROUND_SERVICE才能使用此 API。

使用 SDK 版本Build.VERSION_CODES生成的应用。Q 或更高版本可以使用清单文件的服务元素中的属性 R.attr.foregroundServiceType 指定前台服务类型。 属性 R.attr.foregroundServiceType 的值可以是多个标志 ORed。

注意:从 SDK 版本Build.VERSION_CODES开始。S,面向 SDK 版本Build.VERSION_CODES的应用。不允许 S 或更高版本从后台启动前台服务。 有关更多详细信息,请参阅行为更改:面向 Android 12 的应用。 注意:从 SDK 版本Build.VERSION_CODES开始。UPSIDE_DOWN_CAKE,面向 SDK 版本Build.VERSION_CODES的应用。不允许UPSIDE_DOWN_CAKE或更高版本启动前台服务,而无需在清单属性 R.attr.foregroundServiceType 中指定有效的前台服务类型。 有关更多详细信息,请参阅行为更改:面向 Android 14 的应用。

引发:ForegroundServiceStartNotAllowedException 如果应用目标 API Build.VERSION_CODES。S 或更高版本,由于后台限制,服务被限制为前台服务。

引发:InvalidForegroundServiceTypeException 如果应用目标 API Build.VERSION_CODES。UPSIDE_DOWN_CAKE或更高版本,清单属性 R.attr.foregroundServiceType 设置为无效类型(即ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE)。

引发:MissingForegroundServiceTypeException 如果应用目标 API Build.VERSION_CODES。UPSIDE_DOWN_CAKE或更高版本,并且未设置清单属性 R.attr.foregroundServiceType。

引发:如果应用目标 API Build.VERSION_CODES,则引发 SecurityException。UPSIDE_DOWN_CAKE或更高版本,并且无权在清单属性 R.attr.foregroundServiceType 中使用指定类型启动前台服务。

Android 参考。android.app.Service.startForeground

本页的某些部分是根据 Android 开放源代码项目创建和共享的工作进行的修改,并根据 Creative Commons 2.5 属性许可证中所述的术语使用。

另请参阅

适用于

StartForeground(Int32, Notification, ForegroundService)

具有其他 foregroundServiceType 参数的 #startForeground(int, Notification) 重载版本。

[Android.Runtime.Register("startForeground", "(ILandroid/app/Notification;I)V", "", ApiSince=29)]
public void StartForeground(int id, Android.App.Notification notification, Android.Content.PM.ForegroundService foregroundServiceType);
[<Android.Runtime.Register("startForeground", "(ILandroid/app/Notification;I)V", "", ApiSince=29)>]
member this.StartForeground : int * Android.App.Notification * Android.Content.PM.ForegroundService -> unit

参数

id
Int32

此通知的 NotificationManager#notify(int, Notification) NotificationManager.notify(int, Notification)标识符不得为 0。

notification
Notification

要显示的通知。

foregroundServiceType
ForegroundService

必须是清单属性android.R.attr#foregroundServiceType标志的子集标志;不能。ServiceInfo#FOREGROUND_SERVICE_TYPE_NONE

特性

注解

带有其他 foregroundServiceType 参数的 startForeground(int,Notification)重载版本。 使用 SDK 版本Build.VERSION_CODES生成的应用。Q 或更高版本可以使用清单文件的服务元素中的属性 R.attr.foregroundServiceType 指定前台服务类型。 属性 R.attr.foregroundServiceType 的值可以是多个标志 ORed。

foregroundServiceType 参数必须是清单属性 R.attr.foregroundServiceType 中指定的子集标志,否则将引发 IllegalArgumentException。 将 foregroundServiceType 参数指定为ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST以使用清单属性 foregroundServiceType 中指定的所有标志。

注意:从 SDK 版本Build.VERSION_CODES开始。S,面向 SDK 版本Build.VERSION_CODES的应用。不允许 S 或更高版本从后台启动前台服务。 有关更多详细信息,请参阅行为更改:面向 Android 12 的应用。 注意:从 SDK 版本Build.VERSION_CODES开始。UPSIDE_DOWN_CAKE,面向 SDK 版本Build.VERSION_CODES的应用。不允许 UPSIDE_DOWN_CAKE在清单属性 R.attr.foregroundServiceType 中指定有效的前台服务类型的情况下启动前台服务,此处的参数 foregroundServiceType 不能是ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE。 有关更多详细信息,请参阅行为更改:面向 Android 14 的应用。

引发:ForegroundServiceStartNotAllowedException 如果应用目标 API Build.VERSION_CODES。S 或更高版本,由于后台限制,服务被限制为前台服务。

引发:InvalidForegroundServiceTypeException 如果应用目标 API Build.VERSION_CODES。UPSIDE_DOWN_CAKE或更高版本,清单属性 R.attr.foregroundServiceType 或 param foregroundServiceType 设置为无效类型(i.e.ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE)。

引发:MissingForegroundServiceTypeException 如果应用目标 API Build.VERSION_CODES。UPSIDE_DOWN_CAKE或更高版本,未设置清单属性 R.attr.foregroundServiceType,参数 foregroundServiceType 设置为ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST。

引发:如果 param foregroundServiceType 不是清单属性 R.attr.foregroundServiceType 的子集,则引发:IllegalArgumentException。

引发:如果应用目标 API Build.VERSION_CODES,则引发 SecurityException。UPSIDE_DOWN_CAKE或更高版本,并且无权在 foregroundServiceType 中使用指定类型启动前台服务。 R.attr.foregroundServiceType。

Android 参考。android.app.Service.startForeground

本页的某些部分是根据 Android 开放源代码项目创建和共享的工作进行的修改,并根据 Creative Commons 2.5 属性许可证中所述的术语使用。

适用于