标签:android handler messagedispatcher 模块化 消息分发
在第二篇文章《Android中利用Handler实现消息的分发机制(一)》中,我们讲到主线程的Looper是Android系统在启动App的时候,已经帮我们创建好了,而如果在子线程中需要去使用Handler的时候,我们就需要显式地去调用Looper的 prepare方法和loop方法,从而为子线程创建其唯一的Looper。
具体代码如下:
class LooperThread extends Thread { public Handler mHandler; public void run() { Looper.prepare(); mHandler = new Handler() { public void handleMessage(Message msg) { Log.v("Test", "Id of LooperThread : " + Thread.currentThread().getId()); ... } } }; Looper.loop(); } }
@Override public void run() { mTid = Process.myTid(); Looper.prepare(); synchronized (this) { mLooper = Looper.myLooper(); notifyAll(); } Process.setThreadPriority(mPriority); onLooperPrepared(); Looper.loop(); mTid = -1; }
而在日常开发中,当我们需要利用Handler在子线程中实现业务的处理的时候,我们就可以利用HandlerIntent来实现我们的需求。
一般情况下,我们会创建一个类,让其去继承HandlerThread, 如下:
public class MyHandlerThread extends HandlerThread { public MyHandlerThread(String name) { super(name); } } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v("Test", "Id of MainThread : " + Thread.currentThread().getId()); MyHandlerThread myHandlerThread = new MyHandlerThread("MyHandlerThread"); myHandlerThread.start(); Handler handler = new Handler(myHandlerThread.getLooper(), new Callback() { @Override public boolean handleMessage(Message msg) { Log.v("Test", "id of Thread by Callback : " + Thread.currentThread().getId()); return false; } }); handler.sendEmptyMessage(0); }
接着,就需要利用Handler其中一个构造函数Handler(Looper, Callback) ,将HandlerThread线程中的 Looper 赋给handler,而随之传入的,则是Handler.Callback的接口实现类,如上面代码所示。
最后调用 sendMessage方法,对应的结果如下:
10-28 17:24:50.438: V/Test(31694): Id of MainThread : 1 10-28 17:24:50.448: V/Test(31694): id of Thread by Callback : 91617
一般情况下,我们在创建handlerThread的时候,也会顺便实现Handler.Callback接口,将我们要实现的代码逻辑也封装在此线程中,让代码更具有可读性,如下:
public class MyHandlerThread extends HandlerThread implements Callback{ public MyHandlerThread(String name) { super(name); } @Override public boolean handleMessage(Message msg) { Log.v("Test", "id of Thread by Callback : " + Thread.currentThread().getId()); return true; } } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v("Test", "Id of MainThread : " + Thread.currentThread().getId()); MyHandlerThread myHandlerThread = new MyHandlerThread("MyHandlerThread"); myHandlerThread.start(); Handler handler = new Handler(myHandlerThread.getLooper(), myHandlerThread); handler.sendEmptyMessage(0); }
不同的业务逻辑,不同的功能,应该实现在不同的模块中,而模块与模块之间就可以通过一个消息来通信,而这种消息通讯方式,我们就可以利用Handler和HandlerThread来实现。
比如,最近做的一个浏览器的小Demo,其类图如下:
在其中,我们就利用了MessageDispatcher来存放各个模块的Handler,其结构如下:
private static MessageDispatcher mMsgDispatcher; private SparseArray<Handler> mHandlers; ... public void sendMessage(int target, int from, int msgWhat, Object obj){ Handler handler = mHandlers.get(target); if(handler == null){ Logger.v("There is no Handler registered by target " + target); return; } Message msg = handler.obtainMessage(); msg.what = msgWhat; msg.obj = obj; msg.arg1 = from; handler.sendMessage(msg); }; public void registerHanlder(int key, Handler handler){ mHandlers.put(key, handler); } public void unregisterHanlder(int key){ if(mHandlers.get(key) != null){ mHandlers.delete(key); } } public void destroy(){ mHandlers = null; }
比如,我们在BookmarkActivity中向BookmarkManager发送消息,如下:
mMessageDispatcher.sendMessage(MessageConstant.TARGET_BOOKMARK_MGR, MessageConstant.TARGET_BOOKMARK_ACTIVITY, MessageConstant.MSG_BOOKMARK_GET_ALL_DIR, sparseArray);
class BookmarkHandlerThread extends HandlerThread implements Callback{ public BookmarkHandlerThread(String name) { super(name); } @SuppressWarnings("unchecked") public boolean handleMessage(Message msg){ switch(msg.what){ case MessageConstant.MSG_BOOKMARK_GET_ALL_DIR: //Do Something
虽然只是一个不是很成熟的想法,但还是希望能够跟大家分享一下,在设计代码架构的时候,能够根据功能,业务需求或者基础框架来进行分层,分块,实现代码的松耦合。
结束。
标签:android handler messagedispatcher 模块化 消息分发
原文地址:http://blog.csdn.net/linmiansheng/article/details/40542793