Android 后台任务

Android 后台任务

  • Service

    Android N(Android 7.0)和 Android O(Android 8.0)系统对Background ServiceBroadcast做了一定的限制,其中的一些限制包括:

    • 不可以在应用处于后台状态时call startService启动Service,否则抛出IllegalStateException
    • 当应用退到后台之后,系统会在一分钟左右的时间继续运行之前所创建启动的后台服务,之后便会被系统标记为空闲状态,并停止该后台服务。

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

    对于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,但是来自Google Play Service对于国内来说是不可能

    1. JobIntentService,相比workmanager不能配置像在特定环境下运行job

      在8.0或以后使用JobScheduler.enqueue实现,在8.0前使用startService IntentService实现

      相当于对JobScheduler的封装

  • WorkManager

    所以引入了WorkManager


    image.png

WorkManager使用Processor的决策树

image.png

https://juejin.cn/post/6844903609461653518

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

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

你可能感兴趣的:(Android 后台任务)