JobScheduler之超时检查

本文深入探讨了JobScheduler在Android系统中如何进行超时检查,包括服务绑定、跨进程调用、异步任务执行等场景下的超时时间设置及处理机制。揭示了不同操作的超时时间差异,以及超时后的服务解绑和任务终止流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文中的源代码版本为api23

JobScheduler之超时检查

JobServiceContextJobService交互的过程中会JobServiceContext会进行超时检查,下面我们来看看超时检查是怎么做的。 我们需要解决的主要问题是:

  1. 哪些操作会执行超时检查
  2. 超时时间是多少
  3. 一旦发生超时JobServiceContext会怎么处理

哪些操作会执行超时检查

JobServiceContext中使用一个名为scheduleOpTimeOut的方法来执行超时检查,那么哪些地方会调用该方法呢? 通过全局搜索发现有以下调用点:

  1. 执行服务的bind操作时
boolean executeRunnableJob(JobStatus job) {
    synchronized (mLock) {
        //...
        scheduleOpTimeOut();
        final Intent intent = new Intent().setComponent(job.getServiceComponent());
        boolean binding = mContext.bindServiceAsUser(intent, this,
                Context.BIND_AUTO_CREATE | Context.BIND_NOT_FOREGROUND,
                new UserHandle(job.getUserId()));
        //...
        return true;
    }
}
复制代码
  1. 触发JobService.onStartJob方法时
private void handleServiceBoundH() {
    //...
    try {
        mVerb = VERB_STARTING;
        scheduleOpTimeOut();
        service.startJob(mParams);
    } catch (RemoteException e) {
        Slog.e(TAG, "Error sending onStart message to '" +
                mRunningJob.getServiceComponent().getShortClassName() + "' ", e);
    }
}
复制代码
  1. 等待JobService异步执行任务的期间
private void handleStartedH(boolean workOngoing) {
    switch (mVerb) {
        case VERB_STARTING:
            mVerb = VERB_EXECUTING;
            if (!workOngoing) {
                // Job is finished already so fast-forward to handleFinished.
                handleFinishedH(false);
                return;
            }
            //...
            //workOngoing为true表示JobService
            //需要异步执行任务,完成任务后需要调用
            //jobFinished方法通知JSC
            scheduleOpTimeOut();
            break;
        default:
            //...
            return;
    }
}
复制代码
  1. 停止JobService
private void sendStopMessageH() {
    removeOpTimeOut();
    //...
    try {
        mVerb = VERB_STOPPING;
        scheduleOpTimeOut();
        service.stopJob(mParams);
    } catch (RemoteException e) {
        //...
    }
}
复制代码

可以发现,JobService在执行每一步操作的时候都会有超时检查。

超时时间是多少

这个问题就需要我们来看一下scheduleOpTimeOut方法了

private void scheduleOpTimeOut() {
    removeOpTimeOut();
    
    //有两个事件
    final long timeoutMillis = (mVerb == VERB_EXECUTING) ?
            EXECUTING_TIMESLICE_MILLIS : OP_TIMEOUT_MILLIS;
    //log...
    Message m = mCallbackHandler.obtainMessage(MSG_TIMEOUT);
    mCallbackHandler.sendMessageDelayed(m, timeoutMillis);
    mTimeoutElapsed = SystemClock.elapsedRealtime() + timeoutMillis;
}
复制代码

EXECUTING_TIMESLICE_MILLIS为10分钟,OP_TIMEOUT_MILLIS为8秒 从代码中我们可以发现超时时间有两个,不同的操作有不同的超时时间 mVerbVERB_EXECUTING的超时消息,只有在JobService执行异步任务时才会触发,因此我们可以将超时简单的分为两种:

  1. JobService异步任务执行超时(10mins)
  2. 跨进程调用超时(8s)

一旦发生超时JobServiceContext会怎么处理

JobServiceContext使用handleOpTimeoutH方法来处理超时

private void handleOpTimeoutH() {
    switch (mVerb) {
        case VERB_BINDING:
            //log...
            closeAndCleanupJobH(false /* needsReschedule */);
            break;
        case VERB_STARTING:
            //log...
            closeAndCleanupJobH(false /* needsReschedule */);
            break;
        case VERB_STOPPING:
            //log...
            closeAndCleanupJobH(true /* needsReschedule */);
            break;
        case VERB_EXECUTING:
            //log...
            sendStopMessageH();
            break;
        default:
            //log...
            closeAndCleanupJobH(false /* needsReschedule */);
    }
}
复制代码

主要处理方法有closeAndCleanupJobHsendStopMessageH

  1. sendStopMessageH 该方法会立刻执行stopJob方法,通知JobService结束任务
  2. closeAndCleanupJobH 该方法会直接解绑JobService,我们之前讲过了此处不再赘述。唯一一点是,如果是跨进程执行stopJob方法超时的话,那么needsReschedule参数就为true

总结

  1. JobScheduler在执行服务绑定、跨进程调用JobService.onStartJobJobService.onStopJob以及JobService异步执行任务期间都会进行超时检查
  2. JobService异步执行超时时间为10mins,其他所有操作都是8s
  3. JobService异步执行超时后JobServiceContext会跨进程调用JobService.onStopJob。其他操作超时,会调用closeAndCleanupJobH直接解绑服务,如果是跨进程调用JobService.onStopJob时超时,则还会重新执行Job.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值