标签归档:JobScheduler

JobScheduler 测试

JobScheduler 执行时间并不确定,其在满足预先设置的条件时会由系统来调用。

执行周期

通过 setPeriodic 方法可以设置任务执行的周期,在 Android N 及以后的版本中周期需大于15分钟,小于15分钟的按15分钟执行。

查看手机中的任务

adb shell dumpsys jobscheduler

该命令可以列出系统中的各种任务,包含:

  • Settings
  • Registered XX Jobs
  • Connectivity
  • Alarms
  • Idle
  • Battery
  • AppIdle
  • Content
  • Job history
  • Pending queue

等。其中 Registered XX Jobs 列出了系统中 JobScheduler。
例如:

 JOB #u0a7/-100: 88a17c2 com.android.providers.downloads/.DownloadIdleService
    u0a7 tag=*job*/com.android.providers.downloads/.DownloadIdleService
    Source: uid=u0a7 user=0 pkg=com.android.providers.downloads
    JobInfo:
      Service: com.android.providers.downloads/.DownloadIdleService
      PERIODIC: interval=+12h0m0s0ms flex=+12h0m0s0ms
      Requires: charging=true batteryNotLow=false deviceIdle=true
      Backoff: policy=1 initial=+30s0ms
      Has early constraint
      Has late constraint
    Required constraints: CHARGING TIMING_DELAY DEADLINE IDLE
    Satisfied constraints: BATTERY_NOT_LOW TIMING_DELAY APP_NOT_IDLE DEVICE_NOT_DOZING
    Unsatisfied constraints: CHARGING DEADLINE IDLE
    Tracking: BATTERY IDLE
    Enqueue time: -4h43m43s596ms
    Run time: earliest=-4h43m43s596ms, latest=+7h16m16s404ms
    Ready: false (job=false user=true !pending=true !active=true !backingup=true comp=true)

可以查看任务设置的条件,具体信息等。

Job history:
       -25m52s057ms START: u0a85 com.yulore.framework/com.yulore.superyellowpage.service.YuloreJobService
       -25m36s869ms  STOP: u0a85 com.yulore.framework/com.yulore.superyellowpage.service.YuloreJobService
       -15m51s422ms START: u0a46 DownloadManager:com.android.providers.downloads
       -15m50s704ms  STOP: u0a46 DownloadManager:com.android.providers.downloads
        -1m17s539ms START: u0a85 com.yulore.framework/com.yulore.superyellowpage.service.YuloreJobService
        -1m15s576ms  STOP: u0a85 com.yulore.framework/com.yulore.superyellowpage.service.YuloreJobService

Job History 可以查看 JobScheduler 的任务调度情况。

强制执行

adb shell cmd jobscheduler run -f package_name jobId

参考链接

What tools are available to test JobScheduler?