Android Priority Job Queue (Job Manager):后台线程任务结果传回前台(三)
在附录文章4,5的基础上改造MainActivity.java和MyJob.java,改造后的代码,将使MyJob在后台完成线程任务后返回数据给前台。
实现的技术路线:
(1) 首先需要在MyJob里面定义一个结果值,假设这个值就是打算盛放后台线程计算结果的容器bean等等,然后当线程在后台计算完成后赋值。同时写一个public公共的get方法,这个get操作暴露给其他代码使用。
package zhangphil.app; import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log; import com.birbit.android.jobqueue.Job;
import com.birbit.android.jobqueue.Params;
import com.birbit.android.jobqueue.RetryConstraint; /**
* Created by Phil on 2016/10/10.
*/
public class MyJob extends Job{ private String tag; //此处的结果将传回给发起线程的上层代码
//比如经过一个耗时的长期后台线程完成后,一个结果获得,然后传回给需求方
private String result; public MyJob(String tag) {
super(new Params(500).requireNetwork().persist().groupBy(tag));
this.tag = tag;
Log.d(tag, "初始化");
} public String getResult(){
return result;
} @Override
public void onAdded() {
Log.d(tag, "添加任务");
} //在这里面放置耗时的后台线程化任务
@Override
public void onRun() throws Throwable {
Log.d(tag, "开始运行..."); SystemClock.sleep(10000); //此处抛出异常后,jobqueue将立即自动进入shouldReRunOnThrowable
//throw new Exception(); result="任务完成!"; Log.d(tag,"完成");
} @Override
protected RetryConstraint shouldReRunOnThrowable(@NonNull Throwable throwable, int runCount, int maxRunCount) {
// An error occurred in onRun.
// Return value determines whether this job should retry or cancel. You can further
// specify a backoff strategy or change the job's priority. You can also apply the
// delay to the whole group to preserve jobs' running order. Log.d(tag, "runCount: " + runCount + " maxRunCount: " + maxRunCount); //这样的返回将导致onRun方法的再次执行,重启线程,重启将导致代码再次进入onRun。同时,runCount自加1
return RetryConstraint.RETRY;
} //如果重试超过限定次数,将onCancel.
//如果用户主动放弃删掉这个任务,也一样进入onCancel
@Override
protected void onCancel(int cancelReason, @Nullable Throwable throwable) {
Log.d(tag, "放弃:" + cancelReason);
} @Override
protected int getRetryLimit() {
//仅仅重启5次次,超过5次则放弃任务。
return 5;
}
}
(2) JobManager添加后台线程任务时候,重写回调方法,在回调里面等待结果。
package zhangphil.app; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log; import com.birbit.android.jobqueue.Job;
import com.birbit.android.jobqueue.JobManager;
import com.birbit.android.jobqueue.callback.JobManagerCallback; public class MainActivity extends AppCompatActivity { private JobManager jobManager;
private String tag=this.getClass().getName(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main); jobManager=MyApplication.getInstance().getJobManager(); MyJob job=new MyJob("任务1");
jobManager.addJobInBackground(job); jobManager.addCallback(new JobManagerCallback() {
@Override
public void onJobAdded(@NonNull Job job) { } @Override
public void onJobRun(@NonNull Job job, int resultCode) { } @Override
public void onJobCancelled(@NonNull Job job, boolean byCancelRequest, @Nullable Throwable throwable) { } @Override
public void onDone(@NonNull Job job) {
MyJob myJob= (MyJob) job;
Log.d(tag,job.getId()+" onDone : "+myJob.getResult());
} @Override
public void onAfterJobRun(@NonNull Job job, int resultCode) { }
});
}
}
代码运行结果:
附录:
【1】《Java线程池:ExecutorService,Executors》链接地址:http://blog.csdn.net/zhangphil/article/details/43898637
【2】《Java线程池及Future、Callable获得线程返回结果【Java线程池系列2】》链接地址:http://blog.csdn.net/zhangphil/article/details/49701219
【3】《Java线程池之FutureTask【Java线程池系列3】》链接地址:http://blog.csdn.net/zhangphil/article/details/49702751
【4】《Android Priority Job Queue (Job Manager)(一)》链接地址:http://blog.csdn.net/zhangphil/article/details/52777196
【5】《Android Priority Job Queue (Job Manager):线程任务的容错重启机制(二)》链接地址:http://blog.csdn.net/zhangphil/article/details/52778525