必威体育Betway必威体育官网
当前位置:首页 > IT技术

Message源码解析

时间:2019-07-22 03:42:13来源:IT技术作者:seo实验室小编阅读:79次「手机版」
 

message

message

标签(空格分隔): Message


630行,百行码

public final class Message implements Parcelable {
    //消息标签
    public int what;
    //携带参数
    public int arg1;
    //携带参数
    public int arg2;
    //携带对象
    public Object obj;
    //IPC的通信返回
    public messenger replyTo;
    public int sendingUid = -1;
    //消息使用标志
    static final int FLAG_IN_USE = 1 << 0;
    static final int FLAG_ASYNCHRONOUS = 1 << 1;
    static final int FLAGS_TO_CLEAR_ON_COPY_FROM = FLAG_IN_USE;
    int flags;
    //执行时间
    long when;
    //传递数据
    Bundle data;
    //消息分发目标
    handler target;
    //消息回调
    Runnable callback;
    //下一个消息
    // sometimes we store linked lists of these things
    Message next;

    //同步锁
    public static final Object sPoolSync = new Object();
    //指向回收的Message
    private static Message sPool;
    //当前消息池的数量
    private static int sPoolSize = 0;
    //消息池大小
    private static final int MAX_POOL_SIZE = 50;
    //检查是否可回收
    private static boolean gCheckRecycle = true;

    //从消息池中获取一个消息
    public static Message obtain() {
        synchronized (sPoolSync) {
            if (sPool != null) {
                Message m = sPool;
                sPool = m.next;
                m.next = null;
                m.flags = 0; // clear in-use flag
                sPoolSize--;
                return m;
            }
        }
        //消息池中没有就构建一个消息对象返回
        return new Message();
    }

    //消息内容传进去,赋值返回
    public static Message obtain(Message orig) {
        Message m = obtain();
        m.what = orig.what;
        m.arg1 = orig.arg1;
        m.arg2 = orig.arg2;
        m.obj = orig.obj;
        m.replyTo = orig.replyTo;
        m.sendingUid = orig.sendingUid;
        if (orig.data != null) {
            m.data = new Bundle(orig.data);
        }
        m.target = orig.target;
        m.callback = orig.callback;

        return m;
    }

    //指定目标target
    public static Message obtain(Handler h) {
        Message m = obtain();
        m.target = h;

        return m;
    }

    //指定目标target和Runable执行体
    public static Message obtain(Handler h, Runnable callback) {
        Message m = obtain();
        m.target = h;
        m.callback = callback;

        return m;
    }

    //指定target和消息标签what
    public static Message obtain(Handler h, int what) {
        Message m = obtain();
        m.target = h;
        m.what = what;

        return m;
    }

    //赋值返回Message
    public static Message obtain(Handler h, int what, Object obj) {
        Message m = obtain();
        m.target = h;
        m.what = what;
        m.obj = obj;

        return m;
    }

    //赋值返回Message
    public static Message obtain(Handler h, int what, int arg1, int arg2) {
        Message m = obtain();
        m.target = h;
        m.what = what;
        m.arg1 = arg1;
        m.arg2 = arg2;

        return m;
    }

    //赋值返回Message
    public static Message obtain(Handler h, int what,
            int arg1, int arg2, Object obj) {
        Message m = obtain();
        m.target = h;
        m.what = what;
        m.arg1 = arg1;
        m.arg2 = arg2;
        m.obj = obj;

        return m;
    }

    //5.0以下需要指定标签gCheckRecycle为false,默认为true
    public static void updateCheckRecycle(int targetSdkVersion) {
        if (targetSdkVersion < build.VERSION_CODES.LOLLIPOP) {
            gCheckRecycle = false;
        }
    }

    //双重检查通过就释放Message
    public void recycle() {
        if (isInUse()) {
            if (gCheckRecycle) {
                throw new IllegalStateException("This message cannot be recycled because it "
                        + "is still in use.");
            }
            return;
        }
        recycleUnchecked();
    }

    //引用置空
    void recycleUnchecked() {
        // Mark the message as in use while it remains in the recycled object pool.
        // Clear out all other details.
        flags = FLAG_IN_USE;
        what = 0;
        arg1 = 0;
        arg2 = 0;
        obj = null;
        replyTo = null;
        sendingUid = -1;
        when = 0;
        target = null;
        callback = null;
        data = null;

        synchronized (sPoolSync) {
            if (sPoolSize < MAX_POOL_SIZE) {
                next = sPool;
                sPool = this;
                sPoolSize++;
            }
        }
    }

    //复制一个Message
    public void copyFrom(Message o) {
        this.flags = o.flags & ~FLAGS_TO_CLEAR_ON_COPY_FROM;
        this.what = o.what;
        this.arg1 = o.arg1;
        this.arg2 = o.arg2;
        this.obj = o.obj;
        this.replyTo = o.replyTo;
        this.sendingUid = o.sendingUid;

        if (o.data != null) {
            this.data = (Bundle) o.data.clone();
        } else {
            this.data = null;
        }
    }

    //获取执行时间
    public long getWhen() {
        return when;
    }

    //设置执行target
    public void setTarget(Handler target) {
        this.target = target;
    }

    //获取执行target
    public Handler getTarget() {
        return target;
    }

    //获取回调(Handler-post调用的)
    public Runnable getCallback() {
        return callback;
    }

    /** @hide */
    public Message setCallback(Runnable r) {
        callback = r;
        return this;
    }

    //获取Bundle传递的数据
    public Bundle getData() {
        if (data == null) {
            data = new Bundle();
        }

        return data;
    }

    //有点像getData(),不过他是有什么返回什么,null就返回null
    public Bundle peekData() {
        return data;
    }

    //设置Bundle数据
    public void setData(Bundle data) {
        this.data = data;
    }

    //设置what标签
    public Message setWhat(int what) {
        this.what = what;
        return this;
    }

    //发送给目标target
    public void sendToTarget() {
        target.sendMessage(this);
    }

    //判断消息是异步的吗?我们在Handler解析的时候
    public boolean isAsynchronous() {
        return (flags & FLAG_ASYNCHRONOUS) != 0;
    }

    //设置消息为异步
    public void setAsynchronous(boolean async) {
        if (async) {
            flags |= FLAG_ASYNCHRONOUS;
        } else {
            flags &= ~FLAG_ASYNCHRONOUS;
        }
    }

    //标签消息是否将被使用
    boolean isInUse() {
        return ((flags & FLAG_IN_USE) == FLAG_IN_USE);
    }

    //设置标签消息为将被使用
    void markInUse() {
        flags |= FLAG_IN_USE;
    }

    //构建一个Message
    public Message() {
    }

    @Override
    public String toString() {
        return toString(SystemClock.uptimeMillis());
    }

    String toString(long now) {
        stringbuilder b = new StringBuilder();
        b.APPend("{ when=");
        TimeUtils.formatDuration(when - now, b);

        if (target != null) {
            if (callback != null) {
                b.append(" callback=");
                b.append(callback.getClass().getName());
            } else {
                b.append(" what=");
                b.append(what);
            }

            if (arg1 != 0) {
                b.append(" arg1=");
                b.append(arg1);
            }

            if (arg2 != 0) {
                b.append(" arg2=");
                b.append(arg2);
            }

            if (obj != null) {
                b.append(" obj=");
                b.append(obj);
            }

            b.append(" target=");
            b.append(target.getClass().getName());
        } else {
            b.append(" barrier=");
            b.append(arg1);
        }

        b.append(" }");
        return b.toString();
    }

    void writeToProto(Protooutputstream proto, long fieldId) {
        final long messageToken = proto.start(fieldId);
        proto.write(MessageProto.WHEN, when);

        if (target != null) {
            if (callback != null) {
                proto.write(MessageProto.CALLBACK, callback.getClass().getName());
            } else {
                proto.write(MessageProto.WHAT, what);
            }

            if (arg1 != 0) {
                proto.write(MessageProto.ARG1, arg1);
            }

            if (arg2 != 0) {
                proto.write(MessageProto.ARG2, arg2);
            }

            if (obj != null) {
                proto.write(MessageProto.OBJ, obj.toString());
            }

            proto.write(MessageProto.TARGET, target.getClass().getName());
        } else {
            proto.write(MessageProto.BARRIER, arg1);
        }

        proto.end(messageToken);
    }

    //实现了Parcelable可序列化接口
    public static final Parcelable.Creator<Message> CREATOR
            = new Parcelable.Creator<Message>() {
        public Message createFromParcel(Parcel source) {
            Message msg = Message.obtain();
            msg.readFromParcel(source);
            return msg;
        }

        public Message[] newArray(int size) {
            return new Message[size];
        }
    };

    public int describecontents() {
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        if (callback != null) {
            throw new runtimeexception(
                "Can't marshal callbacks across processes.");
        }
        dest.writeInt(what);
        dest.writeInt(arg1);
        dest.writeInt(arg2);
        if (obj != null) {
            try {
                Parcelable p = (Parcelable)obj;
                dest.writeInt(1);
                dest.writeParcelable(p, flags);
            } catch (classcastexception e) {
                throw new RuntimeException(
                    "Can't marshal non-Parcelable objects across processes.");
            }
        } else {
            dest.writeInt(0);
        }
        dest.writeLong(when);
        dest.writeBundle(data);
        Messenger.writeMessengerOrNullToParcel(replyTo, dest);
        dest.writeInt(sendingUid);
    }

    private void readFromParcel(Parcel source) {
        what = source.readInt();
        arg1 = source.readInt();
        arg2 = source.readInt();
        if (source.readInt() != 0) {
            obj = source.readParcelable(getClass().getClassLoader());
        }
        when = source.readLong();
        data = source.readBundle();
        replyTo = Messenger.readMessengerOrNullFromParcel(source);
        sendingUid = source.readInt();
    }
}


流程上就没有什么好说的了


相关阅读

Handle的原理(Looper、Handler、Message三者关系)

转载请注明出处:http://blog.csdn.net/lowprofile_coding/article/details/72580044 介绍 前面的内容对Handler做了介绍,也讲解

Message.obtain() 和Handler.obtainMessage()的区别

Message.obtain() 和Handler.obtainMessage()的区别 性能更优越 尽管Message的构造器是公开的,但是获取Message对象的最好

MessageBox()用法简介

MessageBox()  功能显示一个消息对话框。  语法MessageBox(text,title{,icon{,button{,default}}})  参数title:string类型,

前端 postMessage

window.postMessage() 方法可以安全地实现跨域通信和页面间数据通信。 postMessage 可用于解决以下方面的问题: 页面和其打开的新

Messager(消息窗口)$.messager.show

开发十年,就只剩

分享到:

栏目导航

推荐阅读

热门阅读