Skip to content

Commit

Permalink
add a onResult method for task. onResult is invoked in the ui thread …
Browse files Browse the repository at this point in the history
…after the task finish
  • Loading branch information
twolight committed Oct 14, 2015
1 parent 5ad1ab1 commit bceba32
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
16 changes: 16 additions & 0 deletions demo/src/main/java/cn/zhaiyifan/demo/DemoApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ protected void start() {
public boolean runOnProcess(String processName) {
return processName.equals("cn.zhaiyifan.init");
}

@Override
protected void onResult() {
}
};

Task task2 = new Task("task2", false, 5) {
Expand All @@ -43,6 +47,9 @@ protected void start() {
e.printStackTrace();
}
}
@Override
protected void onResult() {
}
};

Task task3 = new Task("task3", true) {
Expand All @@ -55,6 +62,9 @@ protected void start() {
e.printStackTrace();
}
}
@Override
protected void onResult() {
}
};

Task task4 = new Task("task4", false) {
Expand All @@ -67,6 +77,9 @@ protected void start() {
e.printStackTrace();
}
}
@Override
protected void onResult() {
}
};

Task task5 = new Task("task5", false) {
Expand All @@ -78,6 +91,9 @@ protected void start() {
e.printStackTrace();
}
}
@Override
protected void onResult() {
}
};
task5.setParentTask(task4);

Expand Down
35 changes: 34 additions & 1 deletion init/src/main/java/cn/zhaiyifan/init/Task.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package cn.zhaiyifan.init;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import java.util.concurrent.CountDownLatch;

/**
Expand All @@ -15,7 +18,8 @@ public abstract class Task implements Runnable {
private boolean mIsBlocked = true;
private long mDelay = 0;
private int mStatus = Status.STATUS_PENDING_START;

private static InternalHandler sHandler;
private static final int MESSAGE_POST_RUSULT = 0x1;
// for asynchronous task chain
private Task mParentTask;
private Task mChildTask;
Expand Down Expand Up @@ -92,6 +96,7 @@ public void run() {
long startTime = System.currentTimeMillis();

start();
getHandler().obtainMessage(MESSAGE_POST_RUSULT, this).sendToTarget();

long endTime = System.currentTimeMillis();
LogImpl.i(TAG, getName() + " runs " + (endTime - startTime));
Expand All @@ -107,6 +112,10 @@ public void run() {
mStatus = Status.STATUS_DONE;
}

/**
* This method is invoked by ui thread when the task finish.
*/
protected abstract void onResult();
/**
* Run task.
*/
Expand Down Expand Up @@ -176,4 +185,28 @@ public boolean runOnProcess(String processName) {
public String getName() {
return mTaskName;
}

/**
* Provide a public handler for all the task;
* @return a handler object
*/
private static Handler getHandler() {
synchronized (Task.class) {
if (sHandler == null) {
sHandler = new InternalHandler();
}
return sHandler;
}
}

private static class InternalHandler extends Handler {
public InternalHandler() {
super(Looper.getMainLooper());
}
@Override
public void handleMessage(Message msg) {
Task task = (Task)msg.obj;
task.onResult();
}
}
}

0 comments on commit bceba32

Please sign in to comment.