最近接触到一个比较有挑战性的项目,我发现里面使用大量的消息机制,现在这篇博客我想具体分析一下:android里面的消息到底是什么东西,消息机制到底有什么好处呢?

其实说到android消息机制,我们可能很快就会想到message/handle/looper这三个对象,没错在android里面就是通过此三个对象实现消息机制的。那么我想问的是,为什么我们需要消息机制呢?说到底就是为了更好、更流畅的用户体验,因为在app里面我们或多或少都有可能会设计到一些比较耗时的操作,这个时候如果我们只是一味的将当前所有的操作都停留在一个线程上面的话,那么该线程将特别的卡,导致用户界面会出现长时间的卡顿、没有响应的情况。那么,我们能不能聪明一点将一些耗时的操作放置在工作线程上面呢?当前是可以的,这个时候我们的主线程-UI线程,就可以只负责处理UI更新、展示的工作了,当工作线程完成之后,我们就可以通过某种机制通知UI进行更新了,说到这里你也许就能明白消息机制的产生初衷了。就是为了多个线程之间的互通互信,最常用的场景也许就是HTTP网络请求了。

好了,让我们先通过如下代码实例:

  1. btnMsgTest.setOnClickListener(new View.OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. WorkThread workThread = new WorkThread();
  5. workThread.start();
  6. }
  7. });

上面这段代码我只是订阅了一个按钮点击事件,在该按钮点击事件里面我们开启了一个新的工作线程,让我们来看看工作线程的实现代码:

  1. class WorkThread extends Thread {
  2.  
  3. @Override
  4. public void run() {
  5. btnMsgTest.setText("你好");
  6. }
  7. }

其实很简单,我们只是在工作线程里面修改一下按钮的text属性值,但是当前点击按钮的时候,却发现程序报错了:

  1. android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
  2. at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6556)
  3. at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:942)
  4. at android.view.ViewGroup.invalidateChild(ViewGroup.java:5081)
  5. at android.view.View.invalidateInternal(View.java:12713)
  6. at android.view.View.invalidate(View.java:12677)
  7. at android.view.View.invalidate(View.java:12661)
  8. at android.widget.TextView.checkForRelayout(TextView.java:7159)
  9. at android.widget.TextView.setText(TextView.java:4342)
  10. at android.widget.TextView.setText(TextView.java:4199)
  11. at android.widget.TextView.setText(TextView.java:4174)
  12. at example.xiaocai.com.testgumpcome.MainActivity$WorkThread.run(MainActivity.java:157)
  13. 03-05 04:15:00.741 1600-1613/example.xiaocai.com.testgumpcome E/Surface getSlotFromBufferLocked: unknown buffer: 0xee9f20e0

这是为什么呢?让我们来分析一下上面的exception提示,意思是说:UI更新只能在它自己原始的线程上面。那么问题就来了,我们怎么将工作线程里面的更新通知到UI上面呢?这个时候消息机制就可以大展拳脚了。

我们先来看一下message对象,这个对象就像理解的字面意思一样就是真正的消息对象,我们可以通过如下代码定义一个消息:

  1. Message message=handler.obtainMessage();
  2. message.what=1;
  3. message.obj="hello";
  4. handler.sendMessage(message);

注意我们这里是从利用obtainMessage方法,创建一个message对象,而不是利用new Message创建新的message对象。说点题外话,这两者有什么区别吗?通过源码分析我们会发现obtainMessage是从message池里面分配一个对象,这样就避免创建新的对象了。

细心的你,也许从上面的代码里面还能看到handler对象,下面我们就来说一说handler对象吧!我们创建好message之后,怎样发送出去呢?然后又是怎样调度处理的呢?我们可以直接在代码里面创建一个handler对象,如下代码:

  1. Handler handler2=new Handler(){
  2. @Override
  3. public void handleMessage(Message msg) {
  4. super.handleMessage(msg);
  5. }
  6. };

然后我们就可以通过该handler2对象调用sendMessage方法就可以将消息发送出去了。通过这个流程分析,我们大概就能猜出handler的作用了,它就消息的具体操作对象,通过handler我们可以发送各种类型的message,可以处理消息等。

说到这里,我们还有个疑问就是如果如果我想要发送多个message对象,那么不同的message对象又是怎样调度执行的呢?请看下面的代码:

  1. Message message=handler.obtainMessage();
  2. message.what=1;
  3. message.obj="hello";
  4. handler.sendMessage(message);
  5.  
  6. Message message1=handler.obtainMessage();
  7. message1.what=2;
  8. message1.obj="world";
  9. handler.sendMessage(message1);

这种情况下,是不是就意味着后台会有个MessageQuene呢?要不然,多个消息是放在哪里呢?然后又是通过哪种方式进行message对象调度呢?没错,多个message会通过looper对象进行调度执行。

说到这里,让我们利用消息机制重新来修改最上面的实例代码:

  1. class WorkThread extends Thread {
  2.  
  3. @Override
  4. public void run() {
  5. // btnMsgTest.setText("你好");
  6. Message message=handler.obtainMessage();
  7. message.what=1;
  8. message.obj="修改按钮文本";
  9. handler.sendMessage(message);
  10. }
  11. }

这个时候我们在工作线程里面就不是直接更新UI了,而是发送一个message,然后在handler里面就行UI更新,让我们来看看这块的代码:

  1. private Handler handler = new Handler() {
  2. @Override
  3. public void handleMessage(Message msg) {
  4. switch (msg.what) {
  5. case 1:
  6. btnMsgTest.setText("你好");
  7. break;
  8. }
  9. }
  10. };

看到这里也许聪明的你就会感到奇怪了,我怎么没有看到looper的身影呢?这样new出来的Handler就能确定一定是在UI线程上面吗?亲,先别着急,让我们来看看与上面同等的一段代码,也许你就明白了。

  1. private Handler handler2=new Handler(Looper.getMainLooper(),new Handler.Callback() {
  2. @Override
  3. public boolean handleMessage(Message msg) {
  4. btnMsgTest.setText("通过looper设置");
  5. return false;
  6. }
  7. });

在这段代码里面我们就能看到Looper的身影了,同时我们调用了getMainLooper方法获取到了主线程也就是我们需要的UI线程。看到这里我们就能够通过下面这幅图来形象的说明三者之间的关系了。

通过上面的分析,我们能够了解android消息机制到底是怎么回事,同时还能够比较熟练的使用它们。下面让我们再深入一步,研究一下android消息机制的源码,更加直观的了解到message是怎样发送、调度、处理的。首先来看看message源码:

  1. public final class Message implements Parcelable

我们可以看到message实现了一个Parcelable接口,该接口的主要作用就是包装、传输数据。没错,message里面我们当然需要传输数据了。然后让我们来看看message里面包括的常用属性信息代码:

  1. /**
  2. * User-defined message code so that the recipient can identify
  3. * what this message is about. Each {@link Handler} has its own name-space
  4. * for message codes, so you do not need to worry about yours conflicting
  5. * with other handlers.
  6. */
  7. public int what;
  8.  
  9. /**
  10. * arg1 and arg2 are lower-cost alternatives to using
  11. * {@link #setData(Bundle) setData()} if you only need to store a
  12. * few integer values.
  13. */
  14. public int arg1;
  15.  
  16. /**
  17. * arg1 and arg2 are lower-cost alternatives to using
  18. * {@link #setData(Bundle) setData()} if you only need to store a
  19. * few integer values.
  20. */
  21. public int arg2;
  22.  
  23. /**
  24. * An arbitrary object to send to the recipient. When using
  25. * {@link Messenger} to send the message across processes this can only
  26. * be non-null if it contains a Parcelable of a framework class (not one
  27. * implemented by the application). For other data transfer use
  28. * {@link #setData}.
  29. *
  30. * <p>Note that Parcelable objects here are not supported prior to
  31. * the {@link android.os.Build.VERSION_CODES#FROYO} release.
  32. */
  33. public Object obj;

以上四个属性,在我们设置一个message信息的时候,可能会经常使用到,具体的时候场景大家可以在注释里面看出。最后让我们来看看主要的方法:

  1. /**
  2. * Return a new Message instance from the global pool. Allows us to
  3. * avoid allocating new objects in many cases.
  4. */
  5. public static Message obtain() {
  6. synchronized (sPoolSync) {
  7. if (sPool != null) {
  8. Message m = sPool;
  9. sPool = m.next;
  10. m.next = null;
  11. sPoolSize--;
  12. return m;
  13. }
  14. }
  15. return new Message();
  16. }

通过上面的代码我们可以看出,如果消息池不为空的话,那么我们就同步的从消息池里面分配消息,否则的话就实例化一个message对象。这样的话,我们一般创建一个message对象的时候,就建议使用obtain方法,这样就可以很好的提高应用性能。message里面还有个回收方法,代码如下:

  1. /**
  2. * Return a Message instance to the global pool. You MUST NOT touch
  3. * the Message after calling this function -- it has effectively been
  4. * freed.
  5. */
  6. public void recycle() {
  7. clearForRecycle();
  8.  
  9. synchronized (sPoolSync) {
  10. if (sPoolSize < MAX_POOL_SIZE) {
  11. next = sPool;
  12. sPool = this;
  13. sPoolSize++;
  14. }
  15. }
  16. }
  1. /*package*/ void clearForRecycle() {
  2. flags = 0;
  3. what = 0;
  4. arg1 = 0;
  5. arg2 = 0;
  6. obj = null;
  7. replyTo = null;
  8. when = 0;
  9. target = null;
  10. callback = null;
  11. data = null;
  12. }

我们可以看到其实在recycle方法里面实现是很简单的,只是将一些变量置为默认值,同时将消息池变大。剩下其他的方法就是实现Parcelable接口,实现数据的包装。

接下来让我们看看Handler里面最主要的方法,首当其冲的就是消息分发方法,源码如下:

  1. /**
  2. * Handle system messages here.
  3. */
  4. public void dispatchMessage(Message msg) {
  5. if (msg.callback != null) {
  6. handleCallback(msg);
  7. } else {
  8. if (mCallback != null) {
  9. if (mCallback.handleMessage(msg)) {
  10. return;
  11. }
  12. }
  13. handleMessage(msg);
  14. }
  15. }

大意就是说:如果我们在message里面已经写了回调方法那么就直接调用message里面的方法进行处理。如果message里面没有实现回调处理,那么Handler里面的接口回调,代码如下:

  1. /**
  2. * Callback interface you can use when instantiating a Handler to avoid
  3. * having to implement your own subclass of Handler.
  4. *
  5. * @param msg A {@link android.os.Message Message} object
  6. * @return True if no further handling is desired
  7. */
  8. public interface Callback {
  9. public boolean handleMessage(Message msg);
  10. }

这也就是为什么我们能够直接在new一个Handler的时候,复写里面的handlerMessage方法进行消息处理的原因。其次就是刚刚我们看到的构造方法,源码如下所示:

  1. /**
  2. * Use the provided {@link Looper} instead of the default one and take a callback
  3. * interface in which to handle messages.
  4. *
  5. * @param looper The looper, must not be null.
  6. * @param callback The callback interface in which to handle messages, or null.
  7. */
  8. public Handler(Looper looper, Callback callback) {
  9. this(looper, callback, false);
  10. }

通过一个Looper对象获取线程环境,然后实现Callback回调。最后比较重要的方法就是一系列发送消息的方法,源码如下:

  1. /**
  2. * Pushes a message onto the end of the message queue after all pending messages
  3. * before the current time. It will be received in {@link #handleMessage},
  4. * in the thread attached to this handler.
  5. *
  6. * @return Returns true if the message was successfully placed in to the
  7. * message queue. Returns false on failure, usually because the
  8. * looper processing the message queue is exiting.
  9. */
  10. public final boolean sendMessage(Message msg)
  11. {
  12. return sendMessageDelayed(msg, 0);
  13. }
  14.  
  15. /**
  16. * Sends a Message containing only the what value.
  17. *
  18. * @return Returns true if the message was successfully placed in to the
  19. * message queue. Returns false on failure, usually because the
  20. * looper processing the message queue is exiting.
  21. */
  22. public final boolean sendEmptyMessage(int what)
  23. {
  24. return sendEmptyMessageDelayed(what, 0);
  25. }
  26.  
  27. /**
  28. * Sends a Message containing only the what value, to be delivered
  29. * after the specified amount of time elapses.
  30. * @see #sendMessageDelayed(android.os.Message, long)
  31. *
  32. * @return Returns true if the message was successfully placed in to the
  33. * message queue. Returns false on failure, usually because the
  34. * looper processing the message queue is exiting.
  35. */
  36. public final boolean sendEmptyMessageDelayed(int what, long delayMillis) {
  37. Message msg = Message.obtain();
  38. msg.what = what;
  39. return sendMessageDelayed(msg, delayMillis);
  40. }
  41.  
  42. /**
  43. * Sends a Message containing only the what value, to be delivered
  44. * at a specific time.
  45. * @see #sendMessageAtTime(android.os.Message, long)
  46. *
  47. * @return Returns true if the message was successfully placed in to the
  48. * message queue. Returns false on failure, usually because the
  49. * looper processing the message queue is exiting.
  50. */
  51.  
  52. public final boolean sendEmptyMessageAtTime(int what, long uptimeMillis) {
  53. Message msg = Message.obtain();
  54. msg.what = what;
  55. return sendMessageAtTime(msg, uptimeMillis);
  56. }
  57.  
  58. /**
  59. * Enqueue a message into the message queue after all pending messages
  60. * before (current time + delayMillis). You will receive it in
  61. * {@link #handleMessage}, in the thread attached to this handler.
  62. *
  63. * @return Returns true if the message was successfully placed in to the
  64. * message queue. Returns false on failure, usually because the
  65. * looper processing the message queue is exiting. Note that a
  66. * result of true does not mean the message will be processed -- if
  67. * the looper is quit before the delivery time of the message
  68. * occurs then the message will be dropped.
  69. */
  70. public final boolean sendMessageDelayed(Message msg, long delayMillis)
  71. {
  72. if (delayMillis < 0) {
  73. delayMillis = 0;
  74. }
  75. return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
  76. }
  77.  
  78. /**
  79. * Enqueue a message into the message queue after all pending messages
  80. * before the absolute time (in milliseconds) <var>uptimeMillis</var>.
  81. * <b>The time-base is {@link android.os.SystemClock#uptimeMillis}.</b>
  82. * You will receive it in {@link #handleMessage}, in the thread attached
  83. * to this handler.
  84. *
  85. * @param uptimeMillis The absolute time at which the message should be
  86. * delivered, using the
  87. * {@link android.os.SystemClock#uptimeMillis} time-base.
  88. *
  89. * @return Returns true if the message was successfully placed in to the
  90. * message queue. Returns false on failure, usually because the
  91. * looper processing the message queue is exiting. Note that a
  92. * result of true does not mean the message will be processed -- if
  93. * the looper is quit before the delivery time of the message
  94. * occurs then the message will be dropped.
  95. */
  96. public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
  97. MessageQueue queue = mQueue;
  98. if (queue == null) {
  99. RuntimeException e = new RuntimeException(
  100. this + " sendMessageAtTime() called with no mQueue");
  101. Log.w("Looper", e.getMessage(), e);
  102. return false;
  103. }
  104. return enqueueMessage(queue, msg, uptimeMillis);
  105. }
  106.  
  107. /**
  108. * Enqueue a message at the front of the message queue, to be processed on
  109. * the next iteration of the message loop. You will receive it in
  110. * {@link #handleMessage}, in the thread attached to this handler.
  111. * <b>This method is only for use in very special circumstances -- it
  112. * can easily starve the message queue, cause ordering problems, or have
  113. * other unexpected side-effects.</b>
  114. *
  115. * @return Returns true if the message was successfully placed in to the
  116. * message queue. Returns false on failure, usually because the
  117. * looper processing the message queue is exiting.
  118. */
  119. public final boolean sendMessageAtFrontOfQueue(Message msg) {
  120. MessageQueue queue = mQueue;
  121. if (queue == null) {
  122. RuntimeException e = new RuntimeException(
  123. this + " sendMessageAtTime() called with no mQueue");
  124. Log.w("Looper", e.getMessage(), e);
  125. return false;
  126. }
  127. return enqueueMessage(queue, msg, 0);
  128. }

上面方法的不同之处就在于发送的时间、发送的一些属性内容不同罢了。

好了,让我们来看看Looper里面的源码,首先最主要的方法有prepare,从名字你也许就能够看出,该方法最主要就是准备好messagequeue,然后调用loop方法从该messagequeue里面调度message对象,源码如下:

  1. /**
  2. * Run the message queue in this thread. Be sure to call
  3. * {@link #quit()} to end the loop.
  4. */
  5. public static void loop() {
  6. final Looper me = myLooper();
  7. if (me == null) {
  8. throw new RuntimeException("No Looper; Looper.prepare() wasn't called on this thread.");
  9. }
  10. final MessageQueue queue = me.mQueue;
  11.  
  12. // Make sure the identity of this thread is that of the local process,
  13. // and keep track of what that identity token actually is.
  14. Binder.clearCallingIdentity();
  15. final long ident = Binder.clearCallingIdentity();
  16.  
  17. for (;;) {
  18. Message msg = queue.next(); // might block
  19. if (msg == null) {
  20. // No message indicates that the message queue is quitting.
  21. return;
  22. }
  23.  
  24. // This must be in a local variable, in case a UI event sets the logger
  25. Printer logging = me.mLogging;
  26. if (logging != null) {
  27. logging.println(">>>>> Dispatching to " + msg.target + " " +
  28. msg.callback + ": " + msg.what);
  29. }
  30.  
  31. msg.target.dispatchMessage(msg);
  32.  
  33. if (logging != null) {
  34. logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
  35. }
  36.  
  37. // Make sure that during the course of dispatching the
  38. // identity of the thread wasn't corrupted.
  39. final long newIdent = Binder.clearCallingIdentity();
  40. if (ident != newIdent) {
  41. Log.wtf(TAG, "Thread identity changed from 0x"
  42. + Long.toHexString(ident) + " to 0x"
  43. + Long.toHexString(newIdent) + " while dispatching to "
  44. + msg.target.getClass().getName() + " "
  45. + msg.callback + " what=" + msg.what);
  46. }
  47.  
  48. msg.recycle();
  49. }
  50. }

大意如下:循环遍历message对象,同时分发message对象。然后还有两个比较重要的方法就是getMainLooper和getMyLooper,源码如下:

  1. /**
  2. * Initialize the current thread as a looper, marking it as an
  3. * application's main looper. The main looper for your application
  4. * is created by the Android environment, so you should never need
  5. * to call this function yourself. See also: {@link #prepare()}
  6. */
  7. public static void prepareMainLooper() {
  8. prepare(false);
  9. synchronized (Looper.class) {
  10. if (sMainLooper != null) {
  11. throw new IllegalStateException("The main Looper has already been prepared.");
  12. }
  13. sMainLooper = myLooper();
  14. }
  15. }
  16.  
  17. /** Returns the application's main looper, which lives in the main thread of the application.
  18. */
  19. public static Looper getMainLooper() {
  20. synchronized (Looper.class) {
  21. return sMainLooper;
  22. }
  23. }
  24.  
  25. /**
  26. * Return the Looper object associated with the current thread. Returns
  27. * null if the calling thread is not associated with a Looper.
  28. */
  29. public static Looper myLooper() {
  30. return sThreadLocal.get();
  31. }

好了,今天博客就先到这里吧,如果有什么不对的地方欢迎拍砖。

android开发系列之消息机制的更多相关文章

  1. Android 开发系列教程之(一)Android基础知识

    什么是Android Android一词最早是出现在法国作家维里耶德利尔·亚当1986年发表的<未来夏娃>这部科幻小说中,作者利尔·亚当将外表像人类的机器起名为Android,这就是And ...

  2. Android消息传递之Handler消息机制

    前言: 无论是现在所做的项目还是以前的项目中,都会遇见线程之间通信.组件之间通信,目前统一采用EventBus来做处理,在总结学习EventBus之前,觉得还是需要学习总结一下最初的实现方式,也算是不 ...

  3. Android Framework 分析---2消息机制Native层

    在Android的消息机制中.不仅提供了供Application 开发使用的java的消息循环.事实上java的机制终于还是靠native来实现的.在native不仅提供一套消息传递和处理的机制,还提 ...

  4. Android学习笔记之消息机制

    Android的消息机制主要是指Handler的运行机制以及Handler所附带的MessageQueue和Looper的工作过程.   1.为什么要使用Handler? Android规定访问UI只 ...

  5. Cocos2d-x游戏开发中的消息机制:CCNotificationCenter的使用

    在HTML5游戏开发中,js可以使用Event对象的addEventListener(添加事件监听).dispatchEvent(触发事件)实现监听机制,如果在coocos2d-x中,去实现这种机制该 ...

  6. android开发系列之aidl

    aidl在android开发中的主要作用就是跨进程通讯来着,说到进程相比很多人都是非常熟悉了,但是为什么会有跨进程通讯这个概念呢?原来在android系统中,有这么一套安全机制,为了各个Apk数据的独 ...

  7. Android Handle,Looper,Message消息机制

    尊重原创,转载请标明出处    http://blog.csdn.net/abcdef314159 我们知道在Android中更新UI都是在主线程中,而操作一些耗时的任务则须要在子线程中.假设存在多个 ...

  8. Android Handler MessageQueue Looper 消息机制原理

    提到Android里的消息机制,便会提到Message.Handler.Looper.MessageQueue这四个类,我先简单介绍以下这4个类 之间的爱恨情仇. Message 消息的封装类,里边存 ...

  9. Android10_原理机制系列_Android消息机制(Handler)详述

    概述 在Android中的多进程.多线程中提过,只有主线程(UI线程)可以更新UI,其他线程不可以,所以一般耗时操作放到子线程.子线程可以通过Handler将相关信息通知到主线程. Android的消 ...

随机推荐

  1. php get传递数据

    url:?goods[]=924&goods[]=967&goods[]=993 <?php  if($_GET){            print_r($_GET);  } ...

  2. 洛谷P2728 纺车的轮子 Spinning Wheels

     P2728 纺车的轮子 Spinning Wheels 29通过 66提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目背景 一架纺车 ...

  3. 用代码打开FORM里面用到的数据源

    修改动态报表的时候,尝尝需要根据当前设计里指定的数据源,然后打开AOT去查找,相当的不方便. 于是产生写了一个方法,可以根据传过来的数据源名,去AOT找到TABLE或者VIEW, 直接打开,以便修改. ...

  4. .NET中使用log4net

    一,加载log4net引用 下载log4net.dll,我们这里使用的是.NET2.0 下载地址:http://files.cnblogs.com/gosky/log4net-1.2.13-bin-n ...

  5. CSS3新增伪类

    p:last-of-type         选择其父元素的最后的一个P元素 p:last-child            选择其父元素的最后子元素(一定是P才行) p:first-of-type ...

  6. zencart用chrome无法登录后台

    再本地安装完zencart后,可以使用ie和Firefox登录网站后台,但是使用chrome登录时,页面闪一下,然后又跳转到登录页面. 按如下设置可以解决该问题: 中文版:商店设置->Sessi ...

  7. 01-实现图片按钮的缩放、动画效果(block的初步应用)

    #import "ViewController.h" #define kDelta 60 @interface ViewController () @end @implementa ...

  8. 初识 css3中counter属性

    最近看到counter属性,好奇是做什么用的,于是去查了查. 1.简单介绍 counter是为css中插入计数器.[注明]在CSS2.1中counter()只能被使用在content属性上.关于浏览器 ...

  9. SequoiaDB(巨杉数据库)(社区版)安装配置使用图解

    SequoaiDB是一款新型企业级分布式非关系型数据库,提供了基于PC服务器的大规模集群数据平台.作为全球第一家企业级文档式 NoSQL分布式数据库,为用户提供了一个高扩展性.高可用性.高性能.易维护 ...

  10. Entity Framework 5问题集锦

    ORM框架万万千,一直都使用NHibernate,没用过其他的.最近闲来学习下微软自家的Entity Framework,记录一些我学习过程中遇到的头疼问题.(不断更新中...) 教程:http:// ...