我以Android O的源码做了个class图,来整体的认识一下这个机制。
我么打开frameworks/base/core/java/android/app/job/IJobService.aidl
oneway interface IJobService {
/** Begin execution of application's job. */
void startJob(in JobParameters jobParams);
/** Stop execution of application's job. */
void stopJob(in JobParameters jobParams);
}
这里有个startJob()的接口方法。应该就是执行onStartJob()的开始。
public final class JobServiceContext implements ServiceConnection {
...
/** Start the job on the service. */
private void handleServiceBoundLocked() {
...
try {
mVerb = VERB_STARTING; // 这里将Job的状态置为VERB_STARTING
scheduleOpTimeOutLocked();
service.startJob(mParams); ★ 从这里开始和JobService进行交互
} catch (Exception e) {
...
}
}
...
}
public abstract class JobServiceEngine {
...
static final class JobInterface extends IJobService.Stub {
final WeakReference mService;
...
@Override
public void startJob(JobParameters jobParams) throws RemoteException {
JobServiceEngine service = mService.get();
if (service != null) {
Message m = Message.obtain(service.mHandler, MSG_EXECUTE_JOB, jobParams); ★
m.sendToTarget();
}
}
...
}
}
public abstract class JobServiceEngine {
class JobHandler extends Handler {
...
@Override
public void handleMessage(Message msg) {
final JobParameters params = (JobParameters) msg.obj;
switch (msg.what) {
case MSG_EXECUTE_JOB:
try {
boolean workOngoing = JobServiceEngine.this.onStartJob(params); ★1
ackStartMessage(params, workOngoing); ★2
} catch (Exception e) {
Log.e(TAG, "Error while executing job: " + params.getJobId());
throw new RuntimeException(e);
}
break;
...
}
}
}
}
public abstract class JobService extends Service {
...
/** @hide */
public final IBinder onBind(Intent intent) {
if (mEngine == null) {
mEngine = new JobServiceEngine(this) {
@Override
public boolean onStartJob(JobParameters params) {
return JobService.this.onStartJob(params);★
}
...
};
}
return mEngine.getBinder();
}
}
public abstract class JobServiceEngine {
class JobHandler extends Handler {
...
private void ackStartMessage(JobParameters params, boolean workOngoing) {
final IJobCallback callback = params.getCallback();
final int jobId = params.getJobId();
if (callback != null) {
try {
callback.acknowledgeStartMessage(jobId, workOngoing);★
}...
}...
}
}
}
public final class JobServiceContext implements ServiceConnection {
...
final class JobCallback extends IJobCallback.Stub {
@Override
public void acknowledgeStartMessage(int jobId, boolean ongoing) {
doAcknowledgeStartMessage(this, jobId, ongoing);★1
}
...
}
...
void doAcknowledgeStartMessage(JobCallback cb, int jobId, boolean ongoing) {
doCallback(cb, ongoing, "finished start");★2
}
...
void doCallback(JobCallback cb, boolean reschedule, String reason) {
final long ident = Binder.clearCallingIdentity();
try {
synchronized (mLock) {
if (!verifyCallerLocked(cb)) {
return;
}
doCallbackLocked(reschedule, reason);★3 此刻的reschedule为false
}
}...
}
...
void doCallbackLocked(boolean reschedule, String reason) {
...
removeOpTimeOutLocked();//在这里移除回调onStartJob时发送的8s延时msg
if (mVerb == VERB_STARTING) {
handleStartedLocked(reschedule);★4 此刻的reschedule为false
} else if (mVerb == VERB_EXECUTING ||
mVerb == VERB_STOPPING) {
handleFinishedLocked(reschedule, reason);
}...
}
...
private void handleStartedLocked(boolean workOngoing) {
switch (mVerb) {
case VERB_STARTING:
mVerb = VERB_EXECUTING; // 此刻将Job状态置为VERB_EXECUTING
if (!workOngoing) {/*这边就是分水岭,如果workOngoing为false则结束job否则等待job的执行*/
// Job is finished already so fast-forward to handleFinished.
handleFinishedLocked(false, "onStartJob returned false"); ★5 准备结束该Job
return;
}
...
}
}
...
private void handleFinishedLocked(boolean reschedule, String reason) {
switch (mVerb) {
case VERB_EXECUTING:
case VERB_STOPPING:
closeAndCleanupJobLocked(reschedule, reason); ★6
break;
...
}
}
...
private void closeAndCleanupJobLocked(boolean reschedule, String reason) {
...
applyStoppedReasonLocked(reason);
completedJob = mRunningJob;
mJobPackageTracker.noteInactive(completedJob);
...
if (mWakeLock != null) {
mWakeLock.release();//释放WakeLock
}
mContext.unbindService(JobServiceContext.this); ★7 告诉AMS解绑该JobService,最终会调到它的onDestroy()
mWakeLock = null;
mRunningJob = null;
mRunningCallback = null;
mParams = null;
mVerb = VERB_FINISHED;// 将Job状态置为VERB_FINISHED
mCancelled = false;
service = null;
mAvailable = true;
removeOpTimeOutLocked();
mCompletedListener.onJobCompletedLocked(completedJob, reschedule);
}
}