JobScheduler合并任务流
在我们开发的过程中,我们会遇到这样一种场景,如上传定位信息、同步个人信息、同步联系人,这些任务并不需要即时处理,需要在特定的场景调用,如连接wifi、充电时调用。还有一种场景是任务达到一定的数量之后再调用
这时候我们可以使用JobScheduler来实现合并工作流并在合适的时间执行
例子
这里提供一个例子:https://github.com/ddssingsong/JobManager
源码下载:https://download.csdn.net/download/u011077027/11241799
开启一个后台Service不停的调用定位,定位成功之后上传到服务器,这里需要将多个定位结果合并起来并在连接wifi的时候进行上传
开始
public class JobManager {
public static final String TAG = "dds_JobManager";
private static JobManager instance;
private JobScheduler jobScheduler;
private Context context;
private static final int jobId = 0; // 任务ID
public static JobManager getInstance() {
if (null == instance)
instance = new JobManager();
return instance;
}
public void init(Context context) {
this.context = context.getApplicationContext();
jobScheduler = (JobScheduler)
context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
}
/**
* 添加一个任务
*
* @param location 需要发送的内容
*/
public void addJob(String location) {
if (null == jobScheduler) {
return;
}
JobInfo pendingJob = null;
//整合多个job,7.0之上和之下不太一样
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
pendingJob = jobScheduler.getPendingJob(jobId);
} else {
List<JobInfo> allPendingJobs = jobScheduler.getAllPendingJobs();
for (JobInfo info : allPendingJobs) {
if (info.getId() == jobId) {
pendingJob = info;
break;
}
}
}
//找到待执行的job
if (null != pendingJob) {
//多个坐标信息拼到一起 上传
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//数据 与Intent 一样
PersistableBundle extras = pendingJob.getExtras();
//获得上一次设置的location数据
String data = extras.getString("DATA");
//比如 多条坐标数据用@隔开
location = data + "@" + location;
jobScheduler.cancel(jobId);
}
}
PersistableBundle extras = new PersistableBundle();
extras.putString("DATA", location);
//创建一个job
JobInfo jobInfo = new
JobInfo.Builder(jobId,
new ComponentName(context, MyJobService.class))
//只在充电的时候
.setRequiresCharging(true)
//不是蜂窝网络
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
.setExtras(extras).build();
//提交任务
Log.e(TAG, "提交任务");
jobScheduler.schedule(jobInfo);
}
}
public class MyJobService extends JobService {
public static final String TAG = "dds_MyJobService";
@Override
public boolean onStartJob(JobParameters params) {
new MyAsyncTask(this).execute(params);
return true;
}
//当系统接收到一个取消请求时
@Override
public boolean onStopJob(JobParameters params) {
//如果onStartJob返回false,那么onStopJob不会被调用
// 返回 true 则会重新计划这个job
return false;
}
static class MyAsyncTask extends AsyncTask<JobParameters, Void, Void> {
private JobParameters jobParameters;
private WeakReference<MyJobService> myJobServiceWeakReference;
public MyAsyncTask(MyJobService myJobService) {
myJobServiceWeakReference = new WeakReference<>(myJobService);
}
@Override
protected Void doInBackground(JobParameters[] objects) {
jobParameters = objects[0];
Log.i(TAG, jobParameters.getJobId() + " 任务开始执行......");
PersistableBundle extras = jobParameters.getExtras();
String location = extras.getString("DATA");
Log.i(TAG, jobParameters.getJobId() + " 上传:" + location);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
*/
@Override
protected void onPostExecute(Void s) {
//true表示需要重复执行
//false反之
myJobServiceWeakReference.get().jobFinished(jobParameters, false);
Log.i(TAG, jobParameters.getJobId() + "任务执行完成......");
}
}