語言

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或更新版本的裝置 logcat 會記錄內部例外android.os.Build.VERSION_CODES#S。 在較舊的 Android 版本中,則會記錄一個內部例外 RemoteServiceException ,並附上相應的訊息。

與一般 #startService(Intent)方法不同,此方法可隨時使用,無論服務承載的應用程式是否處於前景狀態。

<div 類別=「注意」>

<strong>Note:</strong> 從 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)

本頁部分內容為基於 Open Source Project 所創建與分享的作品,並依授權條款所描述的使用進行修改。

適用於