Filter与AsyncQueryHander实现基本类似,它包含两个Hander:RequestHandler和 ResultHandler,以及一个HandlerThread:名为Filter的线程。RequestHandler与Filter线程绑定,过滤请求都是通过 RequestHandler发送给Filter线程。当过滤产生后,通过mResultHandler将数据push给UI显示。
首先看下Filter的构造方法
public Filter() {
mResultHandler = new ResultsHandler();
}
private class ResultsHandler extends Handler {
/**
* <p>Messages received from the request handler are processed in the
* UI thread. The processing involves calling
* {@link Filter#publishResults(CharSequence,
* android.widget.Filter.FilterResults)}
* to post the results back in the UI and then notifying the listener,
* if any.</p>
*
* @param msg the filtering results
*/
@Override
public void handleMessage(Message msg) {
RequestArguments args = (RequestArguments) msg.obj;
//push 给UI显示
publishResults(args.constraint, args.results);
if (args.listener != null) {
int count = args.results != null ? args.results.count : -1;
args.listener.onFilterComplete(count);
}
}
}
上一篇文章中说到搜索的方法
adapter.getFilter.filter(*******),实际上调用的就是Filter的filter方法。如下
private static final String THREAD_NAME = "Filter";
public final void filter(CharSequence constraint, FilterListener listener) {
synchronized (mLock) {
if (mThreadHandler == null) {
HandlerThread thread = new HandlerThread(
THREAD_NAME, android.os.Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
mThreadHandler = new RequestHandler(thread.getLooper());
}
final long delay = (mDelayer == null) ? 0 : mDelayer.getPostingDelay(constraint);
Message message = mThreadHandler.obtainMessage(FILTER_TOKEN);
RequestArguments args = new RequestArguments();
// make sure we use an immutable copy of the constraint, so that
// it doesn't change while the filter operation is in progress
args.constraint = constraint != null ? constraint.toString() : null;
args.listener = listener;
message.obj = args;
mThreadHandler.removeMessages(FILTER_TOKEN);
mThreadHandler.removeMessages(FINISH_TOKEN);
mThreadHandler.sendMessageDelayed(message, delay);
}
}
首先开启了一个名字为Filter的HandlerThread。我们看下HandlerThread
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;
}
@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;
}
public int getThreadId() {
return mTid;
}
}
该线程中的run方法中什么都没有做,只是提供给我们一个该线程的Looper,就是说该线程是有消息循环的线程,既然有消息循环,那么就应该有自己的Handler来处理消息。得到该线程的handler
mThreadHandler = new RequestHandler(thread.getLooper());
看下RequestHandler
private class RequestHandler extends Handler {
public RequestHandler(Looper looper) {
super(looper);
}
public void handleMessage(Message msg) {
int what = msg.what;
Message message;
switch (what) {
case FILTER_TOKEN:
RequestArguments args = (RequestArguments) msg.obj;
try {
args.results = performFiltering(args.constraint);
} catch (Exception e) {
args.results = new FilterResults();
Log.w(LOG_TAG, "An exception occured during performFiltering()!", e);
} finally {
message = mResultHandler.obtainMessage(what);
message.obj = args;
message.sendToTarget();
}
synchronized (mLock) {
if (mThreadHandler != null) {
Message finishMessage = mThreadHandler.obtainMessage(FINISH_TOKEN);
mThreadHandler.sendMessageDelayed(finishMessage, 3000);
}
}
break;
case FINISH_TOKEN:
synchronized (mLock) {
if (mThreadHandler != null) {
mThreadHandler.getLooper().quit();
mThreadHandler = null;
}
}
break;
}
}
}
根据构造方法中的looper参数将RequestHandler和名为Filter的HandlerThread绑定,这样在其他线程(主线程或者其他子线程)就可以通过该handler给名为Filter的HandlerThread发消息,处理消息的实际操作在handler中处理。回头看filter方法中就往RequestHandler发送了一个消息FILTER_TOKEN。RequestHandler的handleMessage中调用performFiltering来进行查询,查询得到结果后给主线程发送消息更新UI。并且给自己发送一个FINISH_TOKEN的消息,结束HandlerThread线程。
至此Filter类就分析完了。其中比较有实用价值的就是线程间的通信,在写其他应用中可以借鉴这种方式