語言

Service.StartForeground 方法

定義

多載

名稱 Description
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 排程(除非該程序中有其他項目是前台),如果系統需要關閉它們以回收更多記憶體(例如在瀏覽器中顯示大型頁面),也可以輕易終止。 如果關閉服務會干擾使用者,例如服務正在播放背景音樂,使用者會注意到音樂停止播放,你會使用 startForeground(int, Notification)。 請注意,呼叫此方法並不會將服務本身置於啟動狀態,儘管名稱聽起來像是。 你必須先呼叫 startService(Intent),告訴系統應該繼續執行服務,然後用這個方法讓它繼續執行。

針對 API Build.VERSION_CODES 的應用程式。P 或更新版本必須申請權限Manifest.permission.FOREGROUND_SERVICE才能使用此 API。

使用 SDK 版本 Build.VERSION_CODES 建置的應用程式。Q 或更新版本可在 manifest 檔案的服務元素中使用 r.attr.foregroundServiceType 屬性來指定前景服務類型。 屬性 R.attr.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 或以上的用戶不得在未在 manifest 屬性 R.attr.foregroundServiceType 中指定有效的前景服務類型前啟動前台服務。 詳情請參閱「行為變更:針對 Android 14 的應用程式」。

另請參閱:

Throws:ForegroundServiceStartNotAllowedException 如果應用程式目標 API 是 Build.VERSION_CODES。S或更高版本,且因背景限制,該服務無法成為前景服務。

Throws: InvalidForegroundServiceTypeException 如果應用程式目標 API 為 Build.VERSION_CODES。UPSIDE_DOWN_CAKE 或之後,且 manifest 屬性 R.attr.foregroundServiceType 被設定為無效型別(即 ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE)。

拋出:MissingForegroundServiceTypeException 如果應用程式目標 API 是 Build.VERSION_CODES。UPSIDE_DOWN_CAKE 或更晚版本,且 manifest 屬性 R.attr.foregroundServiceType 未被設定。

拋出:SecurityException 如果應用程式目標 API 是 Build.VERSION_CODES。UPSIDE_DOWN_CAKE 或之後,且沒有權限在 manifest 屬性 R.attr.foregroundServiceType 中啟動指定類型的前台服務。

Android 參考資料。android.app.Service.startForeground

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

適用於

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

必須是 manifest 屬性 android.R.attr#foregroundServiceType 標誌的子集標誌;但不一定是 ServiceInfo#FOREGROUND_SERVICE_TYPE_NONE。

屬性

備註

一個超載的 startForeground(int,Notification) 版本,並增加了 foregroundServiceType 參數。 使用 SDK 版本 Build.VERSION_CODES 建置的應用程式。Q 或更新版本可在 manifest 檔案的服務元素中使用 r.attr.foregroundServiceType 屬性來指定前景服務類型。 屬性 R.attr.foregroundServiceType 的值可以是多個旗標合併而成。

foregroundServiceType 參數必須是 manifest 屬性 R.attr.foregroundServiceType 中指定的子集旗標,否則會拋出 IllegalArgumentException。 將 foregroundServiceType 參數指定為 ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST,以使用所有在 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 或更高等級的用戶不得啟動前景服務,除非在 manifest 屬性 R.attr.foregroundServiceType 中指定有效的前景服務類型,且此處的參數 foregroundServiceType 不得為 ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE。 詳情請參閱「行為變更:針對 Android 14 的應用程式」。

另請參閱:

Throws:ForegroundServiceStartNotAllowedException 如果應用程式目標 API 是 Build.VERSION_CODES。S或更高版本,且因背景限制,該服務無法成為前景服務。

Throws: InvalidForegroundServiceTypeException 如果應用程式目標 API 為 Build.VERSION_CODES。UPSIDE_DOWN_CAKE 或更晚版本,且 manifest 屬性 R.attr.foregroundServiceType 或參數 foregroundServiceType 設為 invalid types(i.e.ServiceInfo.FOREGROUND_SERVICE_TYPE_NONE)。

拋出:MissingForegroundServiceTypeException 如果應用程式目標 API 是 Build.VERSION_CODES。UPSIDE_DOWN_CAKE或之後,且 manifest 屬性 R.attr.foregroundServiceType 未設定,param foregroundServiceType 設為 ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST。

Throws: IllegalArgumentException 如果參數 foregroundServiceType 不是 manifest 屬性 R.attr.foregroundServiceType 的子集。

拋出:SecurityException 如果應用程式目標 API 是 Build.VERSION_CODES。UPSIDE_DOWN_CAKE或更晚,且沒有權限在 foregroundServiceType 中以指定類型啟動前景服務。 R.attr.foregroundServiceType.

Android 參考資料。android.app.Service.startForeground

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

適用於