这两个类的原理很简单,前提是在掌握Android 最详细的消息机制解析的基础上。
在上一篇文章的最后提到了如何在子线程中使用Handler,并且讲了HandlerThread的使用,这里回顾一下HandlerThread的使用:
private Handler mHandler;
private void test() {
HandlerThread ht = new HandlerThread("thread-0");
ht.start();
mHandler = new Handler(ht.getLooper()) {
@Override
public void handleMessage(@NonNull Message msg) {
// 处理子线程消息
}
};
}
- 创建
HandlerThread对象; - 调用
HandlerThread的start()方法; - 创建处理子线程事件的
Handler;
这个顺序是固定的。
HandlerThread源码分析
public class HandlerThread extends Thread {
int mPriority;
int mTid = -1;
Looper mLooper;
private @Nullable Handler mHandler;
public HandlerThread(String name) {
super(name);
mPriority = Process.THREAD_PRIORITY_DEFAULT;
}
/**
* Constructs a HandlerThread.
* @param name
* @param priority The priority to run the thread at. The value supplied must be from
* {@link android.os.Process} and not from java.lang.Thread.
*/
public HandlerThread(String name, int priority) {
super(name);
mPriority = priority;
}
}
HandlerThread继承Thread类,它有两个构造方法,通过构造方法中的super(name)可以确定,传入的name就是线程的名称;
当调用了start()方法,虚拟机便会在新线程中执行run()方法,HandlerThread的run()方法如下:
@Override
public void run() {
mTid = Process.myTid();
Looper.prepare();
synchronized (this) {
mLooper = Looper.myLooper();
notifyAll();
}
Process.setThreadPriority(mPriority);
onLooperPrepared();
Looper.loop();
mTid = -1;
}
第3行,获取当前线程的线程id;
第4行,Looper.prepare()创建当前线程的Looper对象;
第6行,获取当前线程的Looper对象;
第9行,设置线程的优先级;
第10行,onLooperPrepared()是个protected修饰的空方法;
第11行,Looper.loop()开启子线程消息循环;
第12行,线程执行完毕,即将进入TERMINATED状态,将线程id置为-1;
关于Looper的创建过程在上一篇Android 最详细的消息机制解析已经进行了详细的介绍,不再赘述。这里分析一下,第7行notifyAll()的作用。看下面一段代码:
private Handler mHandler;
private void test() {
HandlerThread ht = new HandlerThread("thread-0");
ht.start();
Looper looper = ht.getLooper();
}
假设test()方法是在主线程中执行,ht.getLooper()是获取子线程的Looper对象,而子线程的Looper对象是在线程run()方法中创建的,这就导致ht.getLooper()的时机可能会早于子线程Looper创建时机。为了确保ht.getLooper()获取到的Looper对象的可靠性,当发现子线程Looper对象未创建时,让主线程处于WAITING状态。
上面的notifyAll()方法在Looper创建后调用,这样主线程就从WAITING状态切换到了RUNNABLE状态,并且可以拿到Looper对象,具体看下HandlerThread#getLooper()方法实现:
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;
}
第8行,判断线程存活并且mLooper == null代表mLooper对象还未初始化,这个时候将调用线程wait(),等待notify()。
HandlerThread中也有getHandler()方法,不过这个方法平时用的不多,原因是返回的Handler我们不能处理Handler#sendXXXMessageXX()相关消息,只能用于Handler#postRunnable()。
/**
* @return a shared {@link Handler} associated with this thread
* @hide
*/
@NonNull
public Handler getThreadHandler() {
if (mHandler == null) {
mHandler = new Handler(getLooper());
}
return mHandler;
}
IntentService源码分析
Service和IntentService的异同
-
IntentService主要处理耗时操作,Service不建议处理耗时操作,15s会ANR; -
IntentService执行完后自动停止销毁,Service不会。Service的销毁如下:当通过startService()启动的服务需要通过stopService()或者Service#stopSelf()停止服务器,当通过bindService()启动的服务需要通过unbindService()停止服务,当通过startService()+bindService()启动的服务需要通过stopService()+unbindService()停止服务; -
IntentService继承自Service; - 两者使用都必须在
AndroidMenifest.xml中注册。
它的实现是基于Service+HandlerThread+Handler
基本使用
定义IntentService
class MyIntentService extends IntentService {
public MyIntentService(String name) {
super(name);
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
// 处理耗时操作
}
}
启动Service
Intent intent = new Intent(this,MyIntentService.class);
startService(intent);
源码解析
public abstract class IntentService extends Service {
private volatile ServiceHandler mServiceHandler;
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);
}
}
@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(@Nullable Intent intent, int startId) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = intent;
mServiceHandler.sendMessage(msg);
}
@WorkerThread
protected abstract void onHandleIntent(@Nullable Intent intent);
@Override
public void onDestroy() {
mServiceLooper.quit();
}
}
-
Service创建时会创建HandlerThread对象,并且创建用户在子线程中发送消息的Handler; -
onStart()中构造一个Message,并通过Handler发送该消息; -
ServiceHandler的handleMessage()收到这个消息并调用onHandleIntent()进行耗时操作处理; - 处理完耗时操作后,通过调用
stopSelf(msg.arg1)停止服务; - 服务停止会回调
onDestroy()方法,onDestroy()中调用Looper#quit()方法,将消息队列清空;
只要掌握了Handler-Looper-MessageQueue-Message原理,HandlerThread和IntentService的原理掌握起来就很容易了。











网友评论