Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【Android 异步操作】Handler 机制 ( Android 提供的 Handler 源码解析 | Handler 构造与消息分发 | MessageQueue 消息队列相关方法 )

【Android 异步操作】Handler 机制 ( Android 提供的 Handler 源码解析 | Handler 构造与消息分发 | MessageQueue 消息队列相关方法 )

作者头像
韩曙亮
发布于 2023-03-28 10:49:13
发布于 2023-03-28 10:49:13
87400
代码可运行
举报
运行总次数:0
代码可运行

文章目录

一、Handler 构造函数


一般使用 Handler 时 , 调用 Handler 的普通 无参构造函数 ,

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class Handler {
    /**
     * 默认的构造函数 , 与当前线程相关联.
     * 如果该线程没有 Looper , 该 Handler 不能接受 Message 消息 , 并抛出异常
     */
    public Handler() {
        this(null, false);
    }
}

上面的无参构造函数调用了下面的构造方法 ,

第一个参数 Callback callback 是一个回调 , mCallback = callback , 该回调直接设置给了 mCallback 成员变量 ,

在该方法中 , 调用 mLooper = Looper.myLooper() 获取线程本地变量 Looper ;

获取 Looper 中的消息队列 MessageQueue , mQueue = mLooper.mQueue ;

主线程的 Looper 是在 ActivityThread 中的 main 函数 中 , 使用 Looper.prepareMainLooper() 创建的 ,

在 ActivityThread 的 main 函数最后调用了 Looper.loop() , 无限循环获取主线程 Looper 中封装的 MessageQueue 消息队列中的消息 ;

参考 : 【Android 异步操作】Handler ( 主线程中的 Handler 与 Looper | Handler 原理简介 ) ,

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class Handler {
    /**
     * Use the {@link Looper} for the current thread with the specified callback interface
     * and set whether the handler should be asynchronous.
     *
     * Handlers are synchronous by default unless this constructor is used to make
     * one that is strictly asynchronous.
     *
     * Asynchronous messages represent interrupts or events that do not require global ordering
     * with respect to synchronous messages.  Asynchronous messages are not subject to
     * the synchronization barriers introduced by {@link MessageQueue#enqueueSyncBarrier(long)}.
     *
     * @param callback The callback interface in which to handle messages, or null.
     * @param async If true, the handler calls {@link Message#setAsynchronous(boolean)} for
     * each {@link Message} that is sent to it or {@link Runnable} that is posted to it.
     *
     * @hide
     */
    public Handler(@Nullable Callback callback, boolean async) {
        if (FIND_POTENTIAL_LEAKS) {
            final Class<? extends Handler> klass = getClass();
            if ((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&
                    (klass.getModifiers() & Modifier.STATIC) == 0) {
                Log.w(TAG, "The following Handler class should be static or leaks might occur: " +
                    klass.getCanonicalName());
            }
        }

        mLooper = Looper.myLooper();
        if (mLooper == null) {
            throw new RuntimeException(
                "Can't create handler inside thread " + Thread.currentThread()
                        + " that has not called Looper.prepare()");
        }
        mQueue = mLooper.mQueue;
        mCallback = callback;
        mAsynchronous = async;
    }
}

Handler 中的 Callack 回调 接口 ;

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class Handler {
    /**
     * Callback interface you can use when instantiating a Handler to avoid
     * having to implement your own subclass of Handler.
     */
    public interface Callback {
        /**
         * @param msg A {@link android.os.Message Message} object
         * @return True if no further handling is desired
         */
        boolean handleMessage(@NonNull Message msg);
    }
}

二、Handler 消息分发


Handler 中的消息分发 , 在 Looper 的 loop 方法中 , 调用该消息 dispatchMessage 分发消息的方法 ,

在该分发消息方法中 , 首先会查看 消息 Message 中 是否有 Callback 回调 ,

如果有执行该回调 , 就是构造函数中赋值的 mCallback ,

如果没有就调用 Handler 中的 handleMessage 方法 ;

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class Handler {
    /**
     * 在这里处理 Message 消息.
     */
    public void dispatchMessage(@NonNull Message msg) {
        if (msg.callback != null) {
            handleCallback(msg);
        } else {
            if (mCallback != null) {
                if (mCallback.handleMessage(msg)) {
                    return;
                }
            }
            handleMessage(msg);
        }
    }
}

使用 Handler 发送消息时 , 会 调用各种发送消息的方法 , 如

  • public final boolean sendMessage(@NonNull Message msg)
  • public final boolean sendEmptyMessage(int what)
  • public final boolean sendEmptyMessageDelayed
  • public final boolean sendEmptyMessageAtTime
  • public final boolean sendMessageDelayed(@NonNull Message msg, long delayMillis)

等方法 , 所有的发送消息的方法 , 最终都会调用 public boolean sendMessageAtTime(@NonNull Message msg, long uptimeMillis) 方法 ,

在该方法中 , 调用 MessageQueue queue = mQueue , 获取 消息队列 MessageQueue ,

然后调用 enqueueMessage(queue, msg, uptimeMillis) 方法 , 将消息加入到 消息队列 MessageQueue 中 ;

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public class Handler {
    /**
     * Enqueue a message into the message queue after all pending messages
     * before the absolute time (in milliseconds) <var>uptimeMillis</var>.
     * <b>The time-base is {@link android.os.SystemClock#uptimeMillis}.</b>
     * Time spent in deep sleep will add an additional delay to execution.
     * You will receive it in {@link #handleMessage}, in the thread attached
     * to this handler.
     * 
     * @param uptimeMillis The absolute time at which the message should be
     *         delivered, using the
     *         {@link android.os.SystemClock#uptimeMillis} time-base.
     *         
     * @return Returns true if the message was successfully placed in to the 
     *         message queue.  Returns false on failure, usually because the
     *         looper processing the message queue is exiting.  Note that a
     *         result of true does not mean the message will be processed -- if
     *         the looper is quit before the delivery time of the message
     *         occurs then the message will be dropped.
     */
    public boolean sendMessageAtTime(@NonNull Message msg, long uptimeMillis) {
        MessageQueue queue = mQueue;
        if (queue == null) {
            RuntimeException e = new RuntimeException(
                    this + " sendMessageAtTime() called with no mQueue");
            Log.w("Looper", e.getMessage(), e);
            return false;
        }
        return enqueueMessage(queue, msg, uptimeMillis);
    }
}

三、MessageQueue 消息队列相关函数


下面的代码是将消息存储到消息队列中的 enqueueMessage 方法 ;

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public final class MessageQueue {
    boolean enqueueMessage(Message msg, long when) {
        if (msg.target == null) {
            throw new IllegalArgumentException("Message must have a target.");
        }
        if (msg.isInUse()) {
            throw new IllegalStateException(msg + " This message is already in use.");
        }

        synchronized (this) {
            if (mQuitting) {
                IllegalStateException e = new IllegalStateException(
                        msg.target + " sending message to a Handler on a dead thread");
                Log.w(TAG, e.getMessage(), e);
                msg.recycle();
                return false;
            }

            msg.markInUse();
            msg.when = when;
            Message p = mMessages;
            boolean needWake;
            // 如果当前消息为空 , 时间小于当前该消息的发送时间 , 需要马上将该消息发送出去
            // 将表头设置成发送进来的消息 
            if (p == null || when == 0 || when < p.when) {
                // New head, wake up the event queue if blocked.
                msg.next = p;
                mMessages = msg;
                needWake = mBlocked;
            } else {
            	// 如果该消息不急着发送 , 那么将该消息放在消息队列列表尾部 
                // Inserted within the middle of the queue.  Usually we don't have to wake
                // up the event queue unless there is a barrier at the head of the queue
                // and the message is the earliest asynchronous message in the queue.
                needWake = mBlocked && p.target == null && msg.isAsynchronous();
                Message prev;
                for (;;) {
                    prev = p;
                    p = p.next;
                    if (p == null || when < p.when) {
                        break;
                    }
                    if (needWake && p.isAsynchronous()) {
                        needWake = false;
                    }
                }
                msg.next = p; // invariant: p == prev.next
                prev.next = msg;
            }

            // We can assume mPtr != 0 because mQuitting is false.
            if (needWake) {
                nativeWake(mPtr);
            }
        }
        return true;
    }
}

从链表中取出数据 , 调用的是 消息队列 MessageQueue 的 next 方法 , 获取消息时 , 需要获取当前的时间 , 用于判定是否有需要延迟发送的消息 ;

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
public final class MessageQueue {
    @UnsupportedAppUsage
    Message next() {
        // Return here if the message loop has already quit and been disposed.
        // This can happen if the application tries to restart a looper after quit
        // which is not supported.
        final long ptr = mPtr;
        if (ptr == 0) {
            return null;
        }

        int pendingIdleHandlerCount = -1; // -1 only during first iteration
        int nextPollTimeoutMillis = 0;
        for (;;) {
            if (nextPollTimeoutMillis != 0) {
                Binder.flushPendingCommands();
            }

            nativePollOnce(ptr, nextPollTimeoutMillis);

            synchronized (this) {
             	// 获取当前的时间 , 需要判定是否有需要延迟发送的消息 
                // Try to retrieve the next message.  Return if found.
                final long now = SystemClock.uptimeMillis();
                Message prevMsg = null;
                Message msg = mMessages;
                if (msg != null && msg.target == null) {
                    // Stalled by a barrier.  Find the next asynchronous message in the queue.
                    do {
                        prevMsg = msg;
                        msg = msg.next;
                    } while (msg != null && !msg.isAsynchronous());
                }
                if (msg != null) {
                    if (now < msg.when) {
                        // Next message is not ready.  Set a timeout to wake up when it is ready.
                        nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
                    } else {
                        // Got a message.
                        mBlocked = false;
                        if (prevMsg != null) {
                            prevMsg.next = msg.next;
                        } else {
                            mMessages = msg.next;
                        }
                        msg.next = null;
                        if (DEBUG) Log.v(TAG, "Returning message: " + msg);
                        msg.markInUse();
                        return msg;
                    }
                } else {
                    // No more messages.
                    nextPollTimeoutMillis = -1;
                }

                // Process the quit message now that all pending messages have been handled.
                if (mQuitting) {
                    dispose();
                    return null;
                }

                // If first time idle, then get the number of idlers to run.
                // Idle handles only run if the queue is empty or if the first message
                // in the queue (possibly a barrier) is due to be handled in the future.
                if (pendingIdleHandlerCount < 0
                        && (mMessages == null || now < mMessages.when)) {
                    pendingIdleHandlerCount = mIdleHandlers.size();
                }
                if (pendingIdleHandlerCount <= 0) {
                    // No idle handlers to run.  Loop and wait some more.
                    mBlocked = true;
                    continue;
                }

                if (mPendingIdleHandlers == null) {
                    mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];
                }
                mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers);
            }

            // Run the idle handlers.
            // We only ever reach this code block during the first iteration.
            for (int i = 0; i < pendingIdleHandlerCount; i++) {
                final IdleHandler idler = mPendingIdleHandlers[i];
                mPendingIdleHandlers[i] = null; // release the reference to the handler

                boolean keep = false;
                try {
                    keep = idler.queueIdle();
                } catch (Throwable t) {
                    Log.wtf(TAG, "IdleHandler threw exception", t);
                }

                if (!keep) {
                    synchronized (this) {
                        mIdleHandlers.remove(idler);
                    }
                }
            }

            // Reset the idle handler count to 0 so we do not run them again.
            pendingIdleHandlerCount = 0;

            // While calling an idle handler, a new message could have been delivered
            // so go back and look again for a pending message without waiting.
            nextPollTimeoutMillis = 0;
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-11-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
大一C语言实训与总结
} 修正后的程序没有语法错误。执行结果将打印出 a=0 f=134.23,其中 a 的值为 0,f 的值为 134.23。
千山暮海雪
2024/12/25
1210
【python入门系列课程 第七课 计算机是如何做决定的(二)】
本系列课程是针对无基础的,争取用简单明了的语言来讲解,学习前需要具备基本的电脑操作能力,准备一个已安装python环境的电脑。如果觉得好可以分享转发,有问题的地方也欢迎指出,在此先行谢过。
叶子陪你玩
2020/03/12
4080
QTreeView使用总结13,自定义model示例,大大优化性能和内存[通俗易懂]
前面简单介绍过Qt的模型/视图框架,提到了Qt预定义的几个model类型: QStringListModel:存储简单的字符串列表 QStandardItemModel:可以用于树结构的存储,提供了层次数据 QFileSystemModel:本地系统的文件和目录信息 QSqlQueryModel、QSqlTableModel、QSqlRelationalTableModel:存取数据库数据
全栈程序员站长
2022/09/03
2.7K0
QTreeView使用总结13,自定义model示例,大大优化性能和内存[通俗易懂]
R7-1 学生平均成绩排序
假设学生的基本信息包括学号、姓名、三门课程成绩以及个人平均成绩,定义一个能够表示学生信息的结构类型。输入n(n<50)个学生的成绩信息,按照学生的个人平均分从高到低输出他们的信息。
苏泽
2024/03/01
2690
04:谁拿了最多奖学金
04:谁拿了最多奖学金 总时间限制:1000ms内存限制:65536kB描述 某校的惯例是在每学期的期末考试之后发放奖学金。发放的奖学金共有五种,获取的条件各自不同: 1)     院士奖学金,每人8000元,期末平均成绩高于80分(>80),并且在本学期内发表1篇或1篇以上论文的学生均可获得; 2)     五四奖学金,每人4000元,期末平均成绩高于85分(>85),并且班级评议成绩高于80分(>80)的学生均可获得; 3)     成绩优秀奖,每人2000元,期末平均成绩高于90分(>90)的学生均可
attack
2018/04/03
1.2K0
MySQL 经典30题,拿走不谢!!!
这里将开始我们的 sql 之旅,在这里希望对 sql 能力稍弱的同学,有一定的帮助。 如果大家在以下 sql 学习中,发现更具有优化性的建议,可以留言给小编或者加技术群交流,让我们一起成长。(底部有WeChat方式)
八点半的Bruce、D
2020/06/09
1.3K0
SQL之50个常用的SQL语句
50个常用的sql语句 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,score) 成绩表 Teacher(T#,Tname) 教师表 问题: 1、查询“001”课程比“002”课程成绩高的所有学生的学号;  select a.S# from (select s#,score from SC where C#='001') a,(select s#,score  from SC where C#='002') b  wher
互联网金融打杂
2018/04/03
7910
常见的50道数据库面试题
前言:2021年的第一篇公众号,希望自己在学到知识的同时也能分享给大家,一起进步!!
小雯子打豆豆
2021/01/18
1.8K0
SQL语句面试题目_sql基础知识面试题
表结构,节选自:http://www.cnblogs.com/qixuejia/p/3637735.html 题目一,节选,自:https://wenku.baidu.com/view/cda288f1b90d6c85ed3ac671.html 题目二,节选,自:http://www.cnblogs.com/qixuejia/p/3637735.html (为了满足“题目”查询条件,在原文的基础上,插入的测试语句中”增加了几条sc,新增了条Course,修改了student的部分年龄)
全栈程序员站长
2022/09/27
9510
SQL笔试50题(上)
本节内容,我们使用在入门内容部分介绍的在线SQL平台sql fiddle进行测试。
fireWang
2020/02/18
8680
【Python百日精通】深入理解Python条件语句的高级用法
条件语句不仅用于基本的条件判断,还可以扩展到更复杂的逻辑中。以下是一些扩展用法的示例:
屿小夏
2024/08/27
1990
【Python百日精通】深入理解Python条件语句的高级用法
掌握Python控制流:if语句的高级用法
在Python编程中,控制流语句是构建逻辑和执行流程的基础。if语句是控制流语句中最基本、最常用的语句之一。通过if语句,我们可以根据条件执行不同的代码块。本文将详细介绍Python中if语句的高级用法,包括嵌套if、elif的使用、条件表达式、逻辑运算符、组合条件、短路逻辑等,并提供具体的示例代码,帮助全面掌握if语句的高级用法。
sergiojune
2024/07/22
1930
掌握Python控制流:if语句的高级用法
每天 3 分钟,小闫带你学 Python(七)
今天暂时不分享英文名言,因为昨天晚上看到4句话,特别想分享给大家。第一句『物来顺应』告诫我们事情已经发生了,就要顺应去面对;第二句『未来不迎』劝告我们不要为没有发生的事情而焦虑;第三句『当事不杂』告诉我们专注于做好当前的事情;第四句『季过不恋』指明已经过去的事情,无论是好是坏,都不必去留恋。
小闫同学啊
2019/07/30
5320
每天 3 分钟,小闫带你学 Python(七)
超级经典的SQL练习题(MySQL版本),你还怕SQL不过关吗?
微信搜索公众号【C you again】,回复“SQL”下载无水印PDF版本,方便收藏
C you again 的博客
2021/07/08
1.5K0
Python入门教程笔记(二)控制语句
计算机之所以能做很多自动化的任务,因为它可以自己做条件判断,通过条件判断,选择做什么样的逻辑(当然,逻辑是需要我们提前写好的),我们称之为条件分支判断。
Lemon黄
2020/10/10
4600
Python 条件控制 — if语句
引言 生活中的判断几乎是无所不在的,我们每天都在做各种各样的选择,如果这样?如果那样?…… 程序中的判断 流程判断示意图 image.png 成绩等级判断 60分以下为不及格 D 60 - 70 为及格 C 70 - 80 为良好 B 80 - 100 为优秀 A image.png 流程图可以非常直观地描述一个工作过程。 Python中的 if 语句 if 语句基本语法 在 Python 中,if 语句 就是用来进行判断的,格式如下: if 要判断的条件: 条件成立时,要做的事情 .
忆想不到的晖
2021/12/06
7040
Python 条件控制 — if语句
Python语言练习
Python程序由指令组成,运行程序时,计算机依据预设好的指令执行程序。 print是最简单,但很常用的指令,它用于将一些信息输出至屏幕上。 下面演示Python中的print指令:
荣仔_最靓的仔
2021/02/02
3.1K0
Python语言练习
深入理解Python中的if语句
Python 中的 条件控制语句 (Conditional control statement) 是通过一条或者多条语句的执行结果(True 或者 False),来决定执行的代码逻辑 。
皮大大
2021/10/08
1K0
深入理解Python中的if语句
SQL数据库面试题以及答案(50例题)
来源:blog.csdn.net/hundan_520520/article/details/54881208
肉眼品世界
2020/11/11
3.2K0
Golang框架实战-KisFlow流式计算框架(12)-基于反射自适应注册FaaS形参类型
接下来我们来增强KisFlow中Function对业务数据处理的聚焦,将之前Function的写法:
刘丹冰Aceld
2024/07/23
1310
相关推荐
大一C语言实训与总结
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验