Android 后臺(tái)任務(wù)

Android 后臺(tái)任務(wù)

  • Service

    Android N(Android 7.0)和 Android O(Android 8.0)系統(tǒng)對(duì)Background ServiceBroadcast做了一定的限制,其中的一些限制包括:

    • 不可以在應(yīng)用處于后臺(tái)狀態(tài)時(shí)call startService啟動(dòng)Service,否則拋出IllegalStateException
    • 當(dāng)應(yīng)用退到后臺(tái)之后,系統(tǒng)會(huì)在一分鐘左右的時(shí)間繼續(xù)運(yùn)行之前所創(chuàng)建啟動(dòng)的后臺(tái)服務(wù),之后便會(huì)被系統(tǒng)標(biāo)記為空閑狀態(tài),并停止該后臺(tái)服務(wù)。

    Above limitations are only background service as foreground service is more visible to users.

    對(duì)于Broadcast的限制包括:

    • Android 7.0 中,app can NOT send or receive ACTION_NEW_PICTURE or ACTION_NEW_VIDEO broadcast
    • App targeting Android 7.0 and higher can't receive CONNECTIVITY_ACTION if it's declared in manifest
    • From Android O,all implicit broadcast receivers declared in manifest file will not work
  • JobScheduler

    JobScheduler may elect to postpone that work for a bit, but its job are more likely to be differed or interrupted in low-memory situtations ,in doze mode or when they reach a time limit(~10 minutes)

    在API 21引入,但是在API 21和22的版本存在較大bug,所以>=23使用JobScheduler,為了兼容小于23的版本引JobDispatcher,但是來(lái)自Google Play Service對(duì)于國(guó)內(nèi)來(lái)說(shuō)是不可能

    1. JobIntentService,相比workmanager不能配置像在特定環(huán)境下運(yùn)行job

      在8.0或以后使用JobScheduler.enqueue實(shí)現(xiàn),在8.0前使用startService IntentService實(shí)現(xiàn)

      相當(dāng)于對(duì)JobScheduler的封裝

  • WorkManager

    所以引入了WorkManager


    image.png

WorkManager使用Processor的決策樹(shù)

image.png

https://juejin.cn/post/6844903609461653518

https://zhuanlan.zhihu.com/p/37510156

https://stackoverflow.com/questions/59725556/when-to-use-jobintentservice-vs-workmanager

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容