目录
1、HandlerThread
1.1、使用
1.2、源码
2、IntentService
2.1、使用
2.2、实现
2.3、源码
1、HandlerThread
HandlerThread 继承于Thread,其实他就是一个线程,与普通线程不一样的地方是它在内部加载了Looper,方便我们为该线程创建一个Handler。
1.1、使用
1.2、源码
public class HandlerThread extends Thread {private int mTid = -1;private Looper mLooper;private Handler mHandler;public HandlerThread(String name) {super(name);}public HandlerThread(String name, int priority) {super(name);}protected void onLooperPrepared() {}@Overridepublic void run() {mTid = Process.myTid();Looper.prepare();synchronized (this) {mLooper = Looper.myLooper();notifyAll();}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;}@NonNullpublic Handler getThreadHandler() {if (mHandler == null) {mHandler = new Handler(getLooper());}return mHandler;}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;}public int getThreadId() {return mTid;}
}
2、IntentService
IntentService继承于Service,同时它的内部使用了HandlerThread,它相当于一个后台线程,但是,相对于Thread来说,它继承了四大组件中的Service,所以它的优先级会高点,不容易被系统杀死,所以它很适合执行一些高优先级的后台任务。
1、IntentService中的任务是顺序执行,这是因为我们内部使用的Handler,每次sendMessage()后,我们的Looper得按照顺序提取出来然后处理。
2、IntentService执行完任务后会自动关闭自己。
2.1、使用
2.2、实现
它的源码实现很简单, 当我们的IntentService第一次启动,会调用它的onCreate()方法,在该方法中我们会创建一个HandlerThread和它对应的Handler。
当我们每次启动IntentService的时候,都会触发它的onStartCommand()方法,相应地会触发它的onStart()方法,然后这里会将Intent通过Message的形式传递给我们前面创建的Handler中。
最后我们通过继承实现的onHandleIntent()方法来处理Message传递过来的数据,这个时候是运行在我们HandlerThread中,所以我们IntentService完全可以承担起异步任务的性能。
2.3、源码
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);}@Overridepublic 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;}@Overridepublic void onCreate() {super.onCreate();HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");thread.start();mServiceLooper = thread.getLooper();mServiceHandler = new ServiceHandler(mServiceLooper);}@Overridepublic void onStart(@Nullable Intent intent, int startId) {Message msg = mServiceHandler.obtainMessage();msg.arg1 = startId;msg.obj = intent;mServiceHandler.sendMessage(msg);}@Overridepublic int onStartCommand(@Nullable Intent intent, int flags, int startId) {onStart(intent, startId);return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;}@Overridepublic void onDestroy() {mServiceLooper.quit();}@Override@Nullablepublic IBinder onBind(Intent intent) {return null;}@WorkerThreadprotected abstract void onHandleIntent(@Nullable Intent intent);
}