HandlerThread
本质是个Thread,就是内部创建了Looper对象。一般使用getLooper方法返回Looper对象,来创建一个Handler对象。一般在UI线程使用handler对象来sendMessage,而处理逻辑handleMessage反而会在子线程中。如果在数据处理完成后,更新UI线程,则必须用一个在UI线程新建的Handler类来发送数据(而不是使用HandlerThread的Looper对象设置的Handler)
public class HandlerThread extends Thread {
int mPriority;
int mTid = -1;
Looper mLooper;
public HandlerThread(String name) {
super(name);
mPriority = Process.THREAD_PRIORITY_DEFAULT;
}
public HandlerThread(String name, int priority) {
super(name);
mPriority = priority;
}
protected void onLooperPrepared() {
}
@Override
public void run() {
mTid = Process.myTid();
Looper.prepare();
synchronized (this) {
mLooper = Looper.myLooper();
notifyAll();
}
Process.setThreadPriority(mPriority);
onLooperPrepared();
Looper.loop();
mTid = -1;
}
public Looper getLooper() {
if (!isAlive()) {
return null;
}
// If the thread has been started, wait until the looper has been created.
synchronized (this) {
while (isAlive() && mLooper == null) {
try {
wait();
} catch (InterruptedException e) {
}
}
}
return mLooper;
}
public boolean quit() {
Looper looper = getLooper();
if (looper != null) {
looper.quit();
return true;
}
return false;
}
public boolean quitSafely() {
Looper looper = getLooper();
if (looper != null) {
looper.quitSafely();
return true;
}
return false;
}
/**
* Returns the identifier of this thread. See Process.myTid().
*/
public int getThreadId() {
return mTid;
}
}
IntentSevice
本质是个Service,可以执行耗时任务,而不用自己开启线程。已经事先对几个方法进行了处理。onBind方法返回null,onStartCommand处理完整,只需要实现抽象方法onHandleIntent,并在里面进行耗时操作。并且每次只执行一次任务,service被调起后执行完任务就自动关闭自己。
IntentService内部是使用HandlerThread实现的,用HandlerThread的Looper对象创建了一个ServiceHandler,通过其发送 onStartCommand中发来的intent,这样就可以转到子线程中的handleMessage中进行处理。
public abstract class IntentService extends Service {
private volatile Looper mServiceLooper;
private volatile ServiceHandler mServiceHandler;
private String mName;
private boolean mRedelivery;
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
onHandleIntent((Intent)msg.obj);
stopSelf(msg.arg1);
}
}
public IntentService(String name) {
super();
mName = name;
}
public void setIntentRedelivery(boolean enabled) {
mRedelivery = enabled;
}
@Override
public void onCreate() {
super.onCreate();
HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
thread.start();
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
@Override
public void onStart(Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
mServiceHandler.sendMessage(msg);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
onStart(intent, startId);
return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
}
@Override
public void onDestroy() {
mServiceLooper.quit();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@WorkerThread
protected abstract void onHandleIntent(Intent intent);
}
好吧,不知道怎么转载,直接贴链接过来了
鸿洋大神讲得都特别好
Android HandlerThread 完全解析
Android Handler 异步消息处理机制的妙用 创建强大的图片加载类
Android IntentService完全解析 当Service遇到Handler