深入理解JobScheduler与JobService的使用

2018-08-17  本文已影响0人  汪和呆喵

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

JobScheduler

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

JobService

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

设置周期性任务

    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;
      }

总结几个要点:

        /**
         * @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;
    }
}

设置退避规则

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

如何查看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

上一篇下一篇

猜你喜欢

热点阅读