2012-10-29 44 views
0

我有一個服務在前臺運行。我正在尋找一種方式來阻止服務在用戶觸摸通知時在前臺運行。我相信,通過調用stopforeground函數可以刪除前臺服務。我提到了關於前臺服務的android開發文檔,但是當與它關聯的通知被移除時,它沒有提及前臺服務的行爲。所以我的問題是 1)可以通過創建一個通知來清除用戶觸摸它時停止前臺服務嗎? 2)如何確保服務不再在前臺運行?當關聯通知被刪除時,前臺服務會發生什麼情況?

回答

0

從文檔:

public final void stopForeground (boolean removeNotification) 

Added in API level 5 
Remove this service from foreground state, allowing it to be killed if more memory is needed. 

Parameters 
removeNotification If true, the notification previously provided to startForeground(int, Notification) will be removed. Otherwise it will remain until a later call removes it (or the service is destroyed). 

基本上,它說,如果一個服務被破壞則其通知將自動被同時刪除。但是,如果一個服務只是停止,並提供的參數是false,那麼通知將保持到稍後的調用將其刪除或服務被銷燬。

因此,在通知的回撥中,您可以包含類似stopForeground(true)的呼叫,以便服務停止並且通知也會被刪除。

相關問題