Android示例 JobService分析

类预览:
JobInfo
Container of data passed to the JobScheduler fully encapsulating the parameters required to schedule work against the calling application.

JobInfo.Builder
Builder class for constructing JobInfo objects.
用于构造JobInfo对象

JobParameters
Contains the parameters used to configure/identify your job.
包含用于配置/识别你的工作参数。

JobScheduler
This is an API for scheduling various types of jobs against the framework that will be executed in your application's own process.
将在你自己的应用进程中安排各种类型的Job的API

JobService
Entry point for the callback from the JobScheduler
JobScheduler回调的切入点
实现JobService的时候需实现它的抽象方法onStartJob,onStopJob
在onStartJob方法中的任何逻辑都需要在一个单独的线程中执行,因为本方法在应用的主线程中执行

1.启动实现了JobService的服务
//类似Intent的setClass的效果

mServiceComponent = new ComponentName(this, TestJobService.class);
// Start service and provide it a way to communicate with us.Intent startServiceIntent = new Intent(this, TestJobService.class);
//Create a new Messenger pointing to the given Handler.
//创建一个新的Messenger指向给定的Handler
// Any Message objects sent through this Messenger will appear in the Handler
// as if Handler.sendMessage(Message) had been called directly.
//如果任何Message通过Handler.sendMessage(Message)直接调用Messenger发送Message都将出现在这个Handler里面
startServiceIntent.putExtra("messenger", new Messenger(mHandler));
startService(startServiceIntent);

2.获取传递的Messenger,将本Service发送给与Messenger绑定的Handler

Messenger callback = intent.getParcelableExtra("messenger");
Message m = Message.obtain();
m.what = MainActivity.MSG_SERVICE_OBJ;
m.obj = this;
try {    
    callback.send(m);
} catch (RemoteException e) {    
    Log.e(TAG, "Error passing service object back to activity.");
}

3.在Handler中相互持有引用,用于实现回调

case MSG_SERVICE_OBJ:                
//建立连接                
mTestService = (TestJobService) msg.obj;   
mTestService.setUiCallback(MainActivity.this);  

4.生成JobInfo并设置约束条件
//生成JobInfo对象

JobInfo.Builder builder = new JobInfo.Builder(kJobId++, 
mServiceComponent);
String delay = mDelayEditText.getText().toString();
if (delay != null && !TextUtils.isEmpty(delay)) {    
//设置最小的延迟时间    
builder.setMinimumLatency(Long.valueOf(delay) * 1000);
}
String deadline = mDeadlineEditText.getText().toString();
if (deadline != null && !TextUtils.isEmpty(deadline)) {   
builder.setOverrideDeadline(Long.valueOf(deadline) * 1000);
}
boolean requiresUnmetered = mWiFiConnectivityRadioButton.isChecked();
boolean requiresAnyConnectivity = 
mAnyConnectivityRadioButton.isChecked();
if (requiresUnmetered) {    
//不计流量
 builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED);
} else if (requiresAnyConnectivity) { 
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
}
builder.setRequiresDeviceIdle(mRequiresIdleCheckbox.isChecked());
//是否要求充电状态下
builder.setRequiresCharging(mRequiresChargingCheckBox.isChecked());
mTestService.scheduleJob(builder.build());

//安排具体的任务
JobScheduler tm =
(JobScheduler)getSystemService(Context.JOB_SCHEDULER_SERVICE);
tm.schedule(t);//t即为JobInfo对象

//取消安排的任务
JobScheduler tm = 
(JobScheduler)getSystemService(Context.JOB_SCHEDULER_SERVICE;
tm.cancelAll();

//通知JobManager完成了执行的回调,该方法可以运行在任何线程,因为最终会在应用的主线程中调用
jobFinished(params, false);

你可能感兴趣的:(Android示例 JobService分析)