美文网首页
深入理解JobScheduler与JobService的使用

深入理解JobScheduler与JobService的使用

作者: 汪和呆喵 | 来源:发表于2018-08-17 17:01 被阅读0次

    JobScheduler和JobService是安卓在api 21中增加的接口,用于在某些指定条件下执行后台任务。

    JobScheduler

    JobScheduler是用于计划基于应用进程的多种类型任务的api接口。

    • 对象获取方法:[Context.getSystemService(Context.JOB_SCHEDULER_SERVICE)]
    • 使用JobInfo.Builder.JobInfo.Builder(int, android.content.ComponentName)构造JobInfo对象,并作为参数传给JobSechduler的schedule(JobInfo)方法。
    • 当JobInfo中声明的执行条件满足时,系统会在应用的JobService中启动执行这个任务。
      当任务执行时,系统会为你的应用持有WakeLock,所以应用不需要做多余的确保设备唤醒的工作。

    JobService

    public abstract class JobService
    extends Service
    JobService继承自Service,是用于处理JobScheduler中规划的异步请求的特殊Service

    • 使用JobService必须先在AndroidManifest.xml中声明service和权限
      <service android:name="MyJobService" android:permission="android.permission.BIND_JOB_SERVICE"/ >
    • 应用需要实现onStartJob(JobParameters)接口,在其中执行任务逻辑。
    • 这个Service会在一个运行在主线程的Handler中执行规划的任务,所以应用需要在另外的thread/handler/AsyncTask中执行业务逻辑,如果不这么做的话可能会引起主线程的阻塞。
    • onStopJob(android.app.job.JobParameters)接口是当计划的执行条件“不再”满足时被执行的(例如网络中断)。

    设置周期性任务

        private static final int JOB_INFO_ID = 10001;
        private static final long JOB_PERIODIC = 5 * 1000L;
        private void onJobStartClick() {
            JobScheduler jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
            ComponentName componentName = new ComponentName(this, MyJobService.class);
            JobInfo jobinfo = new JobInfo.Builder(JOB_INFO_ID, componentName)
                    .setPeriodic(JOB_PERIODIC)
                    .build();
        }
    

    周期periodic设置为5秒->运行,会发现JobService没有被启动,为什么?

    setPeriodic的最小执行间隔

    源码里来找答案:android/frameworks/base/core/java/android/app/job/JobInfo.java

          /* Minimum interval for a periodic job, in milliseconds. */
          private static final long MIN_PERIOD_MILLIS = 15 * 60 * 1000L;   // 15 minutes
          /* Minimum flex for a periodic job, in milliseconds. */
          private static final long MIN_FLEX_MILLIS = 5 * 60 * 1000L; // 5 minutes
    
          /**
           * Specify that this job should recur with the provided interval, not more than once per
           * period. You have no control over when within this interval this job will be executed,
           * only the guarantee that it will be executed at most once within this interval.
           * Setting this function on the builder with {@link #setMinimumLatency(long)} or
           * {@link #setOverrideDeadline(long)} will result in an error.
           * @param intervalMillis Millisecond interval for which this job will repeat.
           */
          public Builder setPeriodic(long intervalMillis) {
              return setPeriodic(intervalMillis, intervalMillis);
          }
          /**
           * Specify that this job should recur with the provided interval and flex. The job can
           * execute at any time in a window of flex length at the end of the period.
           * @param intervalMillis Millisecond interval for which this job will repeat. A minimum
           *                       value of {@link #getMinPeriodMillis()} is enforced.
           * @param flexMillis Millisecond flex for this job. Flex is clamped to be at least
           *                   {@link #getMinFlexMillis()} or 5 percent of the period, whichever is
           *                   higher.
           */
          public Builder setPeriodic(long intervalMillis, long flexMillis) {
              mIsPeriodic = true;
              mIntervalMillis = intervalMillis;
              mFlexMillis = flexMillis;
              mHasEarlyConstraint = mHasLateConstraint = true;
              return this;
          }
    
          /**
           * Query the minimum interval allowed for periodic scheduled jobs.  Attempting
           * to declare a smaller period that this when scheduling a job will result in a
           * job that is still periodic, but will run with this effective period.
           *
           * @return The minimum available interval for scheduling periodic jobs, in milliseconds.
           */
          public static final long getMinPeriodMillis() {
              return MIN_PERIOD_MILLIS;
          }
          /**
           * Set to the interval between occurrences of this job. This value is <b>not</b> set if the
           * job does not recur periodically.
           */
          public long getIntervalMillis() {
              final long minInterval = getMinPeriodMillis();
              return intervalMillis >= minInterval ? intervalMillis : minInterval;
          }
    

    总结几个要点:

    • 可以看到系统默认设置了一个最小间隔时间15分钟,在获取执行间隔时,会先比较最小间隔时间和设置的间隔时间,取其中大的那个。所以setPeriodic设置时间小于15分钟是不会生效的。
    • flexMillis参数是用来设置周期任务执行的活动时间的,这意味着JobScheduler规划的任务不是在精确的时间执行的。并且这个时间也是有最小值的,系统默认5分钟。
    • setMinimumLatency和setOverrideDeadline不能同setPeriodic一起使用,会引起报错,另外还有一些其他规则,看源码:
            /**
             * @return The job object to hand to the JobScheduler. This object is immutable.
             */
            public JobInfo build() {
                // Allow jobs with no constraints - What am I, a database?
                if (!mHasEarlyConstraint && !mHasLateConstraint && mConstraintFlags == 0 &&
                        mNetworkType == NETWORK_TYPE_NONE &&
                        mTriggerContentUris == null) {
                    throw new IllegalArgumentException("You're trying to build a job with no " +
                            "constraints, this is not allowed.");
                }
                // Check that a deadline was not set on a periodic job.
                if (mIsPeriodic) {
                    if (mMaxExecutionDelayMillis != 0L) {
                        throw new IllegalArgumentException("Can't call setOverrideDeadline() on a " +
                                "periodic job.");
                    }
                    if (mMinLatencyMillis != 0L) {
                        throw new IllegalArgumentException("Can't call setMinimumLatency() on a " +
                                "periodic job");
                    }
                    if (mTriggerContentUris != null) {
                        throw new IllegalArgumentException("Can't call addTriggerContentUri() on a " +
                                "periodic job");
                    }
                }
                if (mIsPersisted) {
                    if (mTriggerContentUris != null) {
                        throw new IllegalArgumentException("Can't call addTriggerContentUri() on a " +
                                "persisted job");
                    }
                    if (!mTransientExtras.isEmpty()) {
                        throw new IllegalArgumentException("Can't call setTransientExtras() on a " +
                                "persisted job");
                    }
                    if (mClipData != null) {
                        throw new IllegalArgumentException("Can't call setClipData() on a " +
                                "persisted job");
                    }
                }
                if (mBackoffPolicySet && (mConstraintFlags & CONSTRAINT_FLAG_DEVICE_IDLE) != 0) {
                    throw new IllegalArgumentException("An idle mode job will not respect any" +
                            " back-off policy, so calling setBackoffCriteria with" +
                            " setRequiresDeviceIdle is an error.");
                }
                JobInfo job = new JobInfo(this);
                ....
                return job;
            }
    

    如何查看自己的JobService的运行?

    adb给我们提供了dumpsys工具:
    adb shell dumpsys jobscheduler

      JOB #u0a122/10001: 945a633 com.fantasy.android.demo/.android.job.MyJobService
        u0a122 tag=*job*/com.fantasy.android.demo/.android.job.MyJobService
        Source: uid=u0a122 user=0 pkg=com.fantasy.android.demo
        JobInfo:
          Service: com.fantasy.android.demo/.android.job.MyJobService
          PERIODIC: interval=+1h0m0s0ms flex=+1h0m0s0ms
          Requires: charging=false batteryNotLow=false deviceIdle=false
          Backoff: policy=1 initial=+30s0ms
          Has early constraint
          Has late constraint
        Required constraints: TIMING_DELAY DEADLINE
        Satisfied constraints: APP_NOT_IDLE DEVICE_NOT_DOZING
        Unsatisfied constraints: TIMING_DELAY DEADLINE
        Tracking: TIME
        Enqueue time: -5m38s906ms
        Run time: earliest=+54m21s65ms, latest=+1h54m21s65ms
        Last successful run: 2018-01-02 13:07:16
        Ready: false (job=false user=true !pending=true !active=true !backingup=true comp=true)
    

    JobService的使用:

    继承JobService实现他的两个接口

    public class MyJobService extends JobService{
        private static final String TAG = "MyJobService";
        @Override
        public boolean onStartJob(JobParameters jobParameters) {
            Log.d(TAG, "onStartJob-->");
            return false;
        }
    
        @Override
        public boolean onStopJob(JobParameters jobParameters) {
            Log.d(TAG, "onStopJob-->");
            return false;
        }
    }
    
    • Android准备好执行任务时,服务就会启动,此时会在主线程上收到onStartJob()方法调用。
      该方法返回false结果表示该任务已经全部做完,此时系统会解绑该JobService,最终会调用JobService的onDestroy()方法,效果就如同自己调用了jobFinished()方法一样。
      返回 true 结果则表示任务已经启动成功,但还没有全部做完,此时可以在任务完成后,应用自行调用jobFinished方法。

    • onStopJob(JobParameters)方法是在中断任务时调用,例如用户需要服务在有充电时才运行,如果在调用JobFinished()之前(任务完成之前)充电器拔掉,onStopJob(...) 方法就会被调用,也就是说,一切任务就立即停止了。
      调用 onStopJob(...) 方法就是表明,服务马上就要被停掉,这里返回 true 表示:“任务应该计划在下次继续。”返回 false 表示:“不管怎样,事情就到此结束吧,不要计划下次了。”

    • 关于jobFinished()
      public final void jobFinished (JobParameters params, boolean wantsReschedule)
      Job的任务执行完毕后,APP端自己调用,用以通知JobScheduler已经完成了任务。
      注意:该方法执行完后不会回调onStopJob(),但是会回调onDestroy()
      当wantsReschedule参数设置为true时,表示任务需要另外规划时间进行执行。
      而这个执行的时间受限与JobInfo的退避规则。

    设置退避规则

    setBackoffCriteria
    public [JobInfo.Builder] setBackoffCriteria (long initialBackoffMillis, int backoffPolicy)

    • initialBackoffMillis是第一次尝试重试的等待间隔,单位为毫秒,预设的参数有:
      DEFAULT_INITIAL_BACKOFF_MILLIS
      30000
      MAX_BACKOFF_DELAY_MILLIS
      18000000
    • backoffPolicy是对应的退避策略,预设的参数有:
      BACKOFF_POLICY_EXPONENTIAL
      二进制退避,等待间隔呈指数增长
      retry_time(current_time, num_failures) = current_time + initial_backoff_millis * 2 ^ (num_failures - 1), num_failures >= 1
      BACKOFF_POLICY_LINEAR
      线性退避,等待间隔呈线性增长
      retry_time(current_time, num_failures) = current_time + initial_backoff_millis * num_failures, num_failures >= 1

    如何查看JobScheduler为应用持有的WakeLock

    adb shell dumpsys power com.fantasy.android.demo | grep Wake

    参考文章:
    https://blog.csdn.net/allisonchen/article/details/79240163
    https://blog.csdn.net/allisonchen/article/details/79218713
    https://www.cnblogs.com/chase1/p/7221916.html

    相关文章

      网友评论

          本文标题:深入理解JobScheduler与JobService的使用

          本文链接:https://www.haomeiwen.com/subject/oprzbftx.html