深入理解JobScheduler与JobService的使用

  • @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 not 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()方法调用。
    自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

有任何问题,欢迎广大网友一起来交流,分享高阶Android学习视频资料和面试资料包~

偷偷说一句:群里高手如云,欢迎大家加群和大佬们一起交流讨论啊!

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!

手如云,欢迎大家加群和大佬们一起交流讨论啊!

[外链图片转存中…(img-8MeYybzI-1712469260555)]

《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值