Handler机制小结

简介: Handler笔记什么是handler机制?handler机制的主要成员1、handler:负责发送处理消息2、message:消息的信息的载体3、messageQueue:存放message的队列4、looper:handler机制的动力...

Handler笔记

什么是handler机制?
handler机制的主要成员
1、handler:负责发送处理消息
2、message:消息的信息的载体
3、messageQueue:存放message的队列
4、looper:handler机制的动力,无限循环的从messageQueue队列中取出message给handler

img_ca281cf242ba504d9c49bea0847ec784.jpe
图1

1、Handler

(1)我们先来看看handler的构造方法

      public Handler() {
        this(null, false);
    }
    
     public Handler(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 that has not called Looper.prepare()");
        }
        mQueue = mLooper.mQueue;
        mCallback = callback;
        mAsynchronous = async;
    }

(2)默认的构造方法走了 Handler(Callback callback, boolean async) 构造方法,此方法用 mLooper = Looper.myLooper();获取了looper,然后又用looper创建了messageQueue

2、Looper

在Looper类中

    /**
     * Return the Looper object associated with the current thread.  Returns
     * null if the calling thread is not associated with a Looper.
     */
    public static @Nullable Looper myLooper() {
        return sThreadLocal.get();
    }  

注意上面的注释 返回一个与当前线程关联的Looper对象
那Looper是怎么创建的呢,既然有get方法,那应该就有set方法。搜索sThreadLocal.set,果然找到了

 private static void prepare(boolean quitAllowed) {
        if (sThreadLocal.get() != null) {
            throw new RuntimeException("Only one Looper may be created per thread");
        }
        sThreadLocal.set(new Looper(quitAllowed));
    }

说明prepare方法直接创建了一个当前线程的Looper

在UI主线程中,会默认的创建好Looper对象,而在子线程中要使用本线程的Handler需要手动创建Looper对象,如下

class LooperThread extends Thread {
        public Handler mHandler;

        public void run() {
            Looper.prepare();

            mHandler = new Handler() {
                public void handleMessage(Message msg) {
                    
                }
            };

            Looper.loop();
        }
    }   

3、MessageQueue

在handler中我们知道MessageQueue对象通过
mQueue = mLooper.mQueue;
其中的消息是怎么来的呢我们我们通过handler的sendMessage()方法来进行分析

public final boolean sendMessage(Message msg)
   {
       return sendMessageDelayed(msg, 0);
   }  

handler的sendMessage()方法又调用了

 public final boolean sendMessageDelayed(Message msg, long delayMillis)
    {
        if (delayMillis < 0) {
            delayMillis = 0;
        }
        return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
    }

sendMessageAtTime()又调用了

 public boolean sendMessageAtTime(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对象mQueue最后通过MessageQueue.enqueueMessage()将message存到了messageQueue队列中

private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
        msg.target = this;
        if (mAsynchronous) {
            msg.setAsynchronous(true);
        }
        return queue.enqueueMessage(msg, uptimeMillis);
    }
通过上面的分析我们知道了messageQueue是怎么进数据的,后边我们分析一下怎么从里面取数据

我们在子线程中使用Handler的示例中最后一步 调用Looper.loop();然后我们来看看

    public static void loop() {
        final Looper me = myLooper();
        ······
        for (;;) {
            Message msg = queue.next(); // might block
            ······
             try {
                msg.target.dispatchMessage(msg);
                end = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
            } finally {
                if (traceTag != 0) {
                    Trace.traceEnd(traceTag);
                }
            }
            ······
        }
    }

在Looper.loop();方法中我们可以看到for循环中从queue中取出message,然后message是怎么给到handler呢?
在msg.target.dispatchMessage(msg);这一步中我们先看看target是个什么?转到message类中会发现

/*package*/ Handler target;

target是个handler实例 由此可以得出整个message的传播回路

总结:

在一个线程中创建handler、looper、messageQueue
handler把带有信息的message通过sendmessage()发送给messageQueue队列,然后looper通过Looper.loop()无限循环从messageQueue中取出message,然后在发送给handler,handler在handleMessage()方法中处理信息

最后感谢前人的好文章的指导,个人根据个人的理解总结如有不对的地方,请留言。
https://blog.csdn.net/reakingf/article/details/52054598
https://blog.csdn.net/pgg_cold/article/details/79400435?utm_source=blogxgwz2
https://blog.csdn.net/qian520ao/article/details/78262289?locationNum=2&fps=1
https://blog.csdn.net/qq_32770809/article/details/79132363?utm_source=blogxgwz1

相关文章
|
8天前
|
人工智能 安全 Linux
【OpenClaw保姆级图文教程】阿里云/本地部署集成模型Ollama/Qwen3.5/百炼 API 步骤流程及避坑指南
2026年,AI代理工具的部署逻辑已从“单一云端依赖”转向“云端+本地双轨模式”。OpenClaw(曾用名Clawdbot)作为开源AI代理框架,既支持对接阿里云百炼等云端免费API,也能通过Ollama部署本地大模型,完美解决两类核心需求:一是担心云端API泄露核心数据的隐私安全诉求;二是频繁调用导致token消耗过高的成本控制需求。
5166 9
|
16天前
|
人工智能 JavaScript Ubuntu
5分钟上手龙虾AI!OpenClaw部署(阿里云+本地)+ 免费多模型配置保姆级教程(MiniMax、Claude、阿里云百炼)
OpenClaw(昵称“龙虾AI”)作为2026年热门的开源个人AI助手,由PSPDFKit创始人Peter Steinberger开发,核心优势在于“真正执行任务”——不仅能聊天互动,还能自动处理邮件、管理日程、订机票、写代码等,且所有数据本地处理,隐私完全可控。它支持接入MiniMax、Claude、GPT等多类大模型,兼容微信、Telegram、飞书等主流聊天工具,搭配100+可扩展技能,成为兼顾实用性与隐私性的AI工具首选。
21104 114
|
7天前
|
JavaScript Linux API
保姆级教程,通过GACCode在国内使用Claudecode、Codex!
保姆级教程,通过GACCode在国内使用Claudecode、Codex!
4652 1
保姆级教程,通过GACCode在国内使用Claudecode、Codex!
|
12天前
|
人工智能 安全 前端开发
Team 版 OpenClaw:HiClaw 开源,5 分钟完成本地安装
HiClaw 基于 OpenClaw、Higress AI Gateway、Element IM 客户端+Tuwunel IM 服务器(均基于 Matrix 实时通信协议)、MinIO 共享文件系统打造。
8064 7
|
14天前
|
人工智能 JavaScript API
保姆级教程:OpenClaw阿里云/本地部署配置Tavily Search skill 实时联网,让OpenClaw“睁眼看世界”
默认状态下的OpenClaw如同“闭门造车”的隐士,仅能依赖模型训练数据回答问题,无法获取实时新闻、最新数据或训练截止日期后的新信息。2026年,激活其联网能力的最优方案是配置Tavily Search技能——无需科学上网、无需信用卡验证,每月1000次免费搜索额度完全满足个人需求,搭配ClawHub技能市场,还能一键拓展天气查询、邮件管理等实用功能。
8082 5

热门文章

最新文章