文中的源代码版本为api23java
JobServiceContext
与JobService
交互的过程当中会JobServiceContext
会进行超时检查,下面咱们来看看超时检查是怎么作的。 咱们须要解决的主要问题是:api
JobServiceContext
会怎么处理JobServiceContext
中使用一个名为scheduleOpTimeOut
的方法来执行超时检查,那么哪些地方会调用该方法呢? 经过全局搜索发现有如下调用点:异步
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;
}
}
复制代码
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);
}
}
复制代码
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;
}
}
复制代码
JobService
时private void sendStopMessageH() {
removeOpTimeOut();
//...
try {
mVerb = VERB_STOPPING;
scheduleOpTimeOut();
service.stopJob(mParams);
} catch (RemoteException e) {
//...
}
}
复制代码
能够发现,JobService
在执行每一步操做的时候都会有超时检查。this
这个问题就须要咱们来看一下scheduleOpTimeOut
方法了spa
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秒 从代码中咱们能够发现超时时间有两个,不一样的操做有不一样的超时时间 mVerb
为VERB_EXECUTING
的超时消息,只有在JobService
执行异步任务时才会触发,所以咱们能够将超时简单的分为两种:code
JobService
异步任务执行超时(10mins)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 */);
}
}
复制代码
主要处理方法有closeAndCleanupJobH
和sendStopMessageH
事件
sendStopMessageH
该方法会马上执行stopJob
方法,通知JobService
结束任务closeAndCleanupJobH
该方法会直接解绑JobService
,咱们以前讲过了此处再也不赘述。惟一一点是,若是是跨进程执行stopJob
方法超时的话,那么needsReschedule
参数就为true
。JobScheduler
在执行服务绑定、跨进程调用JobService.onStartJob
、JobService.onStopJob
以及JobService
异步执行任务期间都会进行超时检查JobService
异步执行超时时间为10mins,其余全部操做都是8sJobService
异步执行超时后JobServiceContext
会跨进程调用JobService.onStopJob
。其余操做超时,会调用closeAndCleanupJobH
直接解绑服务,若是是跨进程调用JobService.onStopJob
时超时,则还会从新执行Job
.