zoukankan      html  css  js  c++  java
  • 浅谈Looper handler Message MessageQueue bundle parcel parcelable

    ---恢复内容开始---

    这些概念有些时候有点搞不清楚,我们看下源码进行分析.

    1.首先是parcelable,public interface Parcelable,它是一个接口,是所有传递消息相关class都遵循的接口。它的作用是它的子类的实例可以写入Parcel也可以从Parcel中读出,我们可以理解成是Android序列化的特有表现形式,对象的参数写入包裹传递,消息处理后再从包裹取出。Bundle, Message等都实现了此接口。我们也可以自己设计结构实现它的接口,用于某种消息传递,必须实现Parcelable.Creator.

    <pre>
     * public class MyParcelable implements Parcelable {
     *     private int mData;
     *
     *     public int describeContents() {
     *         return 0;
     *     }
     *
     *     public void writeToParcel(Parcel out, int flags) {
     *         out.writeInt(mData);
     *     }
     *
     *     public static final Parcelable.Creator&lt;MyParcelable&gt; CREATOR
     *             = new Parcelable.Creator&lt;MyParcelable&gt;() {
     *         public MyParcelable createFromParcel(Parcel in) {
     *             return new MyParcelable(in);
     *         }
     *
     *         public MyParcelable[] newArray(int size) {
     *             return new MyParcelable[size];
     *         }
     *     };
     *     
     *     private MyParcelable(Parcel in) {
     *         mData = in.readInt();
     *     }
     * }</pre>

    2.public final class Parcel ,它不可继承.我们看一段官网上关于它的描述

    * Container for a message (data and object references) that can
    * be sent through an IBinder. A Parcel can contain both flattened data
    * that will be unflattened on the other side of the IPC (using the various
    * methods here for writing specific types, or the general
    * {@link Parcelable} interface), and references to live {@link IBinder}
    * objects that will result in the other side receiving a proxy IBinder
    * connected with the original IBinder in the Parcel.

    Parcel是ipc通信传输数据和对象的容器,通过IBinder来实现本地和远程的交互。由此它是特殊的序列化,更加的高效,所有的操作都在内存中进行.它定义了各种数据类型的读写操作。

    3.public final class Bundle implements Parcelable, Cloneable,我们看下它官网的描述 A mapping from String values to various Parcelable types.

    Bundle是string 到Parcelable对象的映射,实质就是个ArrayMap<String,Parcelable>,用来打包数据,轻量级.

    4.public final class Message implements Parcelable,Message是MessageQueue的元素,用于线程的消息处理。它的数据域包含Bundle。

    一般Message.obtain();或者Handler.obtainMessage();来获取消息,可以复用资源池回收的消息。定义的MAX_POOL_SIZE = 50;这里的资源池实际上是个链表.

    5.public final class MessageQueue,它其实也是个链表,实现一些进出队列的操作。它通过Handler和具体的线程绑定。

    6.public final class Looper,UI线程的Looper是由Android framework来构建的,一般的线程是没有Looper的。Looper可以理解成是包含MessageQueue的类,线程由此处理传递过来的消息。一般使用上

      *  class LooperThread extends Thread {
      *      public Handler mHandler;
      *
      *      public void run() {
      *          Looper.prepare();
      *
      *          mHandler = new Handler() {
      *              public void handleMessage(Message msg) {
      *                  // process incoming messages here
      *              }
      *          };
      *
      *          Looper.loop();
      *      }
      *  }
    View Code

    其他线程也可以获取Looper来进行消息处理.

    7.Handler 用于传递消息到MessageQueue,根据回调来HandleMessage();值得注意的是一般都是Handler在创建的时候自动和创建它的线程绑定,默认下new Handler()是向UI线程绑定,由此向UI线程的MessageQueue发送Message;如果向其他线程创建的Looper发送消息,必须创建之初绑定到其他线程上。

    以上,大致理清了之间的关系.

    ---恢复内容结束---

  • 相关阅读:
    所有抽样单元都有相等的被选取机会 ,说法错误
    银行存款余额表由谁编制的
    资本公积——资本溢价与资本公积——其他资本公积
    货币单元抽祥
    企业安全生产费用账务的处理
    Tableau代写制作地图可视化和树形图、条形图
    Tableau 代写数据可视化:探索性图形分析新生儿死亡率数据
    R、Python代写、Open Refine采集pdf数据,清理数据和格式化数据
    使用GIS编程代写制作静态地图和处理地理数据
    用R语言编程代写制作交互式图表和地图
  • 原文地址:https://www.cnblogs.com/hxy0107/p/4933098.html
Copyright © 2011-2022 走看看