默认情况下,当我们开启一个应用后,就会开启一条主线程。这个主线程中自在一个Looper对象,因此我们可以通过getMainLooper()方法获取。
通常我们如果要从工作线程向主线程发送消息通常要构建一个Handler对象:Handler handler' = new Handler(getMainLooper);
如果我们要从主线程向工作线程发送消息呢?当然也要使用Looper ,没有looper没有消息队列,更不能消息了。而我们自己创建的工作线程是没有Looper的。因此我们需要
通过
Looper,prepare();
Looper looper = Looper.myLooper();
去创建Looper对象,在加上之前的要创建工作线程,代码的十几行。
这时候我们可以通过handlerThread创建一个带有Looper的线程。
HandlerThread thread = new HandlerThread("");
thread.start():
Handler handler = new Handler(thread.getLooper());
三行就可以搞定。
<span style="font-size:14px;">public class HandlerThread extends Thread {
int mPriority;
int mTid = -1;
Looper mLooper;
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;
}
/**
* Call back method that can be explicitly overridden if needed to execute some
* setup before Looper loops.
*/
protected void onLooperPrepared() {
}
public void run() {
mTid = Process.myTid();
Looper.prepare();
synchronized (this) {
mLooper = Looper.myLooper();
notifyAll();
}
Process.setThreadPriority(mPriority);
onLooperPrepared();
Looper.loop();
mTid = -1;
}
/**
* This method returns the Looper associated with this thread. If this thread not been started
* or for any reason is isAlive() returns false, this method will return null. If this thread
* has been started, this method will block until the looper has been initialized.
* @return The looper.
*/
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;
}
/**
* Ask the currently running looper to quit. If the thread has not
* been started or has finished (that is if {@link #getLooper} returns
* null), then false is returned. Otherwise the looper is asked to
* quit and true is returned.
*/
public boolean quit() {
Looper looper = getLooper();
if (looper != null) {
looper.quit();
return true;
}
return false;
}
/**
* Returns the identifier of this thread. See Process.myTid().
*/
public int getThreadId() {
return mTid;
}
}</span>
原因就在这
<span style="font-size:14px;"> public void run() {
mTid = Process.myTid();
Looper.prepare();
synchronized (this) {
mLooper = Looper.myLooper();
notifyAll();
}
Process.setThreadPriority(mPriority);
onLooperPrepared();
Looper.loop();
mTid = -1;
}</span>
当我们调用startI()启动这个线程后,如果线程运行就会执行run()方法,
准备Looper对象
Looper.prepare();
<span style="font-family: Arial, Helvetica, sans-serif;"> //获取Looper对象</span>
<span style="font-family: Arial, Helvetica, sans-serif;">mLooper = Looper.myLooper();</span>
//迭代信息队列,有信息取出,无消息堵塞
Looper.loop();
获取工作线程中的Looper'对象‘
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();<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
} catch (InterruptedException e) {
}
}
}
return mLooper;
}
<span style="font-size:18px;">//我们调用start()方法启动线程后,线程处于就绪状态,并不一定马上运行,run()方法也就不一定执行,也就不一定有Looper对象,因此,这里有个wait().</span>