浅谈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发送消息,必须创建之初绑定到其他线程上。

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

 

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

转载于:https://www.cnblogs.com/hxy0107/p/4933098.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值