Android Priority Job Queue (Job Manager):多重不同Job并发执行并在前台获得返回结果(四)
在Android Priority Job Queue (Job Manager)中,如果两个或者若干多个完全不同的Job子任务并发执行,如何在前台获得返回结果呢?现在给出一个例子说明。
假设有两个完全不同的子任务JobA和JobB。当JobA任务完成后返回一个结果给前台回调,同样当任务JobB任务完成后返回一个结果给前台回调使用。
写两个Job:JobA和JobB。
JobA.java:
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/11/2.
*/
public class JobA extends Job {
private String tag;
private String result;
public String getResult() {
return result;
}
public JobA(String tag) {
super(new Params(500).requireNetwork().persist().groupBy(tag));
this.tag = tag;
Log.d(tag, "构造");
}
@Override
public void onAdded() {
Log.d(tag, "onAdded");
}
@Override
public void onRun() throws Throwable {
Log.d(tag, "开始运行...");
SystemClock.sleep(3000);
result = tag + "的结果:" + System.currentTimeMillis();
Log.d(tag, "完成:" + result);
}
@Override
protected void onCancel(int cancelReason, @Nullable Throwable throwable) {
}
@Override
protected RetryConstraint shouldReRunOnThrowable(@NonNull Throwable throwable, int runCount, int maxRunCount) {
return null;
}
}
JobB.java:
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/11/2.
*/
public class JobB extends Job {
private String tag;
private String result;
public String getResult() {
return result;
}
public JobB(String tag) {
super(new Params(500).requireNetwork().persist().groupBy(tag));
this.tag = tag;
Log.d(tag, "构造");
}
@Override
public void onAdded() {
Log.d(tag, "onAdded");
}
@Override
public void onRun() throws Throwable {
Log.d(tag, "开始运行...");
SystemClock.sleep(3000);
result = tag + "的结果:" + System.currentTimeMillis();
Log.d(tag, "完成:" + result);
}
@Override
protected void onCancel(int cancelReason, @Nullable Throwable throwable) {
}
@Override
protected RetryConstraint shouldReRunOnThrowable(@NonNull Throwable throwable, int runCount, int maxRunCount) {
return null;
}
}
前台回调获得结果:
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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
JobManager jobManager = MyApplication.getInstance().getJobManager();
JobA a = new JobA("任务->A");
JobA b = new JobA("任务->B");
jobManager.addJobInBackground(a);
jobManager.addJobInBackground(b);
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) {
if (job instanceof JobA) {
JobA a = (JobA) job;
Log.d("返回", a.getResult());
}
if (job instanceof JobB) {
JobB b = (JobB) job;
Log.d("返回", b.getResult());
}
}
@Override
public void onAfterJobRun(@NonNull Job job, int resultCode) {
}
});
}
}
需要注意须用instanceof判断回调的实例是具体哪一个。
代码运行结果如图(添加关键词‘任务’过滤):
关于MyApplication的配置和写法和附录文章4,5,6相同。
附录:
【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
【6】《 Android Priority Job Queue (Job Manager):后台线程任务结果传回前台(三)》链接:http://blog.csdn.net/zhangphil/article/details/52787588