import android.os.Handler;
import android.os.Looper;
import java.util.concurrent.Executor;
class WorkerThread extends Thread implements Executor {
private Handler handler;
private boolean isAlive = false;
public WorkerThread() {
super("WorkerThread-" + System.currentTimeMillis());
}
public void run() {
Looper.prepare();
this.isAlive = true;
this.handler = new Handler();
synchronized (this) {
super.notifyAll();
}
Looper.loop();
}
public Handler getHandler() {
return this.handler;
}
public void stopThread() {
this.isAlive = false;
this.handler.getLooper().quit();
this.handler.removeCallbacksAndMessages(null);
}
public static WorkerThread createWorkerThread() {
WorkerThread thread = new WorkerThread();
synchronized (thread) {
thread.start();
try {
thread.wait();
} catch (InterruptedException e) {
throw new RuntimeException();
}
return thread;
}
}
public void execute(Runnable command)
{
if (this.isAlive)
this.handler.post(command);
}
}
Thursday, March 5, 2015
Android WorkerThread class
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment