在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) {
}
});
}
}