2017-08-28 27 views
7

如何取消JobIntentService?在文檔中我沒有看到這樣做的方法,除了JobSheduler API和Context#stopService(...)方法外,我不確定它是否正確。如何取消JobIntentService

+0

在Android 8.0之前,JobIntentService是作爲IntentService實現的。雖然你可以使用'stopService()'銷燬一個'IntentService',這可能不會影響用於'onHandleIntent()的後臺線程(我從來沒有嘗試過)。 – CommonsWare

回答

0

JobIntentService只帶來IntentService到Android 8+是更加嚴格的後臺服務。它在舊版Android設備上運行時的舊版本號爲IntentService

IntentService從未被設計爲取消,因此缺少任何API來取消JobIntentService

因此,JobIntenService只應用於不需要取消的工作。請注意,JobIntentService仍然可以用於可能失敗,因此被中止的工作。

工作可以取消應在執行兩種:

  • Activity(或其ViewModel)用於短時工作
  • 一個JobService(使用火力地堡工作分派或Job Scheduler如果minSdk爲21)
  • 自定義前臺服務
1

在Android 8.0之前,JobIntentService實現爲IntentServiceIntentService不會通過某些意向操作停止或採取任何其他請求,直到其onHandleIntent方法完成先前的請求。 stopService()將在任務完成之前不會被調用。並且它會在它自動調用時調用。這是IntentService的一個特徵但是在這裏已經回答了類似的問題,指的是IntentServices,希望它有幫助。

answered question

還檢查了android developers