语言

Context.StartForegroundService(Intent) 方法

定义

类似于 #startService(Intent),但具有隐式承诺,服务在开始运行后将调用 android.app.Service#startForeground(int, android.app.Notification) startForeground(int, android.app.Notification) 。

[Android.Runtime.Register("startForegroundService", "(Landroid/content/Intent;)Landroid/content/ComponentName;", "GetStartForegroundService_Landroid_content_Intent_Handler", ApiSince=26)]
public abstract Android.Content.ComponentName? StartForegroundService(Android.Content.Intent? service);
[<Android.Runtime.Register("startForegroundService", "(Landroid/content/Intent;)Landroid/content/ComponentName;", "GetStartForegroundService_Landroid_content_Intent_Handler", ApiSince=26)>]
abstract member StartForegroundService : Android.Content.Intent -> Android.Content.ComponentName

参数

service
Intent

标识要启动的服务。 意向必须完全显式(提供组件名称)。 意向额外内容中可能包含其他值,以提供参数以及此特定启动调用。

返回

如果服务正在启动或正在运行, ComponentName 则返回已启动的实际服务;否则,如果该服务不存在 null,则返回。

特性

注解

类似于 #startService(Intent),但具有隐式承诺,服务在开始运行后将调用 android.app.Service#startForeground(int, android.app.Notification) startForeground(int, android.app.Notification) 。 为该服务提供与 ANR 间隔相当的时间来执行此操作,否则系统会自动崩溃进程,在这种情况下,在运行 SDK 版本ForegroundServiceDidNotStartInTimeException或更高版本的设备上记录内部异常android.os.Build.VERSION_CODES#S。 在较旧的 Android 版本中,会改为记录内部异常 RemoteServiceException ,并显示相应的消息。

与普通 #startService(Intent)方法不同,无论托管服务的应用是否处于前台状态,都可以随时使用此方法。

<div class=“warning”>

注意: 从 SDK 版本 android.os.Build.VERSION_CODES#S开始,不允许面向 SDK 版本 android.os.Build.VERSION_CODES#S 或更高版本的应用从后台启动前台服务。 请参阅

行为更改:面向 Android 12 的应用

有关详细信息。 </div>

Java文档android.content.Context.startForegroundService(android.content.Intent)。

本页的某些部分是根据 创建和共享的工作进行的修改,并根据 许可证中所述的条款使用。

适用于