概述:Android中的异步处理机制由四部分组成:Handler+Looper+MessageQueue+message,用于实现线程间的通信。

用到的概念:

  1. Handler: 主要作用是发送消息和处理消息。
  2. Message:封装了线程间需要传输的信息。
  3. MessageQueue:是FIFO的消息队列,用于存放Handler发送的信息,在调用Looper.prepard()方法实例化。
  4. Looper:线程通过Looper建立自己的消息循环,Looper负责从MessageQueue中取出消息,并且分发到消息指定目标Handler对象。

异步处理的流程:

  1. 首先需要在主线程中创建一个Handler对象,并且重写handlerMessage()方法
  2. 然后当子线程需要进行UI操作时,就创建一个Message对象,并通过Handler对象将这条消息发送出去。
  3. 之后这条消息会被添加到MessageQueue的队列中等待被处理。
  4. 而Looper则会一直尝试从MessageQueue中取出待处理的消息,最后发回Handler的handlerMessage()方法中。
  5. 由于Handler是在主线程中创建的,所以此时handlerMessage()方法中的代码也在主线程中执行,于是就可以安心的进行UI操作了。     

例子:在介绍原理之前,我们先介绍Android线程通讯的一个例子,这个例子实现点击按钮之后从主线程发送消息”hello”到另外一个名为” CustomThread”的线程。

LooperThreadActivity.java

package com.zhuozhuo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

public class LooperThreadActivity extends Activity{
   
 
 private final int MSG_HELLO = 0;
    private Handler mHandler;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        new CustomThread().start();//新建并启动CustomThread实例
       
        findViewById(R.id.send_btn).setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {//点击界面时发送消息
    String str = “hello”;
          Log.d(“Test”, “MainThread is ready to send msg:” + str);
    mHandler.obtainMessage(MSG_HELLO, str).sendToTarget();//发送消息到CustomThread实例
    
   }
  });
       
    }
   
   
   
   
   
    class CustomThread extends Thread {
     @Override
     public void run() {
      //建立消息循环的步骤
      Looper.prepare();//1、初始化Looper
      mHandler = new Handler(){//2、绑定handler到CustomThread实例的Looper对象
       public void handleMessage (Message msg) {//3、定义处理消息的方法
        switch(msg.what) {
        case MSG_HELLO:
         Log.d(“Test”, “CustomThread receive msg:” + (String) msg.obj);
        }
       }
      };
      Looper.loop();//4、启动消息循环
     }
    }
}

main.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android
    android:orientation=”vertical”
    android:layout_width=”fill_parent”
    android:layout_height=”fill_parent”
    >
<TextView 
    android:layout_width=”fill_parent”
    android:layout_height=”wrap_content”
    android:text=”@string/hello”
    />
<Button android:text=”发送消息” android:id=”@+id/send_btn” android:layout_width=”wrap_content” android:layout_height=”wrap_content”></Button>
</LinearLayout>

原理:

我们看到,为一个线程建立消息循环有四个步骤:

  1、 初始化Looper

  2、 绑定handler到CustomThread实例的Looper对象

  3、 定义处理消息的方法

  4、 启动消息循环

下面我们以这个例子为线索,深入Android源代码,说明Android Framework是如何建立消息循环,并对消息进行分发的。

1、 初始化Looper : Looper.prepare()

Looper.java

private static final ThreadLocal sThreadLocal = new ThreadLocal();
public static final void prepare() {
        if (sThreadLocal.get() != null) {
            throw new RuntimeException(“Only one Looper may be created per thread”);
        }
        sThreadLocal.set(new Looper());
}

一个线程在调用Looper的静态方法prepare()时,这个线程会新建一个Looper对象,并放入到线程的局部变量中,而这个变量是不和其他线程共享的(关于ThreadLocal的介绍)。下面我们看看Looper()这个构造函数:

Looper.java

final MessageQueue mQueue;
private Looper() {
        mQueue = new MessageQueue();
        mRun = true;
        mThread = Thread.currentThread();
    }

可以看到在Looper的构造函数中,创建了一个消息队列对象mQueue,此时,调用Looper. prepare()的线程就建立起一个消息循环的对象(此时还没开始进行消息循环)。

2、 绑定handler到CustomThread实例的Looper对象 : mHandler= new Handler()

Handler.java

final MessageQueue mQueue;
 final Looper mLooper;
public Handler() {
        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 = null;
}

Handler通过mLooper = Looper.myLooper();绑定到线程的局部变量Looper上去,同时Handler通过mQueue =mLooper.mQueue;获得线程的消息队列。此时,Handler就绑定到创建此Handler对象的线程的消息队列上了。

3、定义处理消息的方法:Override public void handleMessage (Message msg){}

子类需要覆盖这个方法,实现接受到消息后的处理方法。

4、启动消息循环 : Looper.loop()

所有准备工作都准备好了,是时候启动消息循环了!Looper的静态方法loop()实现了消息循环。

Looper.java

public static final void loop() {
        Looper me = myLooper();
        MessageQueue queue = me.mQueue;
       
        // Make sure the identity of this thread is that of the local process,
        // and keep track of what that identity token actually is.
        Binder.clearCallingIdentity();
        final long ident = Binder.clearCallingIdentity();
       
        while (true) {
            Message msg = queue.next(); // might block
            //if (!me.mRun) {
            //    break;
            //}
            if (msg != null) {
                if (msg.target == null) {
                    // No target is a magic identifier for the quit message.
                    return;
                }
                if (me.mLogging!= null) me.mLogging.println(
                        “>>>>> Dispatching to ” + msg.target + ” ”
                        + msg.callback + “: ” + msg.what
                        );
                msg.target.dispatchMessage(msg);
                if (me.mLogging!= null) me.mLogging.println(
                        “<<<<< Finished to    ” + msg.target + ” ”
                        + msg.callback);
               
                // Make sure that during the course of dispatching the
                // identity of the thread wasn’t corrupted.
                final long newIdent = Binder.clearCallingIdentity();
                if (ident != newIdent) {
                    Log.wtf(“Looper”, “Thread identity changed from 0x”
                            + Long.toHexString(ident) + ” to 0x”
                            + Long.toHexString(newIdent) + ” while dispatching to ”
                            + msg.target.getClass().getName() + ” ”
                            + msg.callback + ” what=” + msg.what);
                }
               
                msg.recycle();
            }
        }
    }

while(true)体现了消息循环中的“循环“,Looper会在循环体中调用queue.next()获取消息队列中需要处理的下一条消息。当msg != null且msg.target != null时,调用msg.target.dispatchMessage(msg);分发消息,当分发完成后,调用msg.recycle();回收消息。

msg.target是一个handler对象,表示需要处理这个消息的handler对象。Handler的void dispatchMessage(Message msg)方法如下:

Handler.java

public void dispatchMessage(Message msg) {
        if (msg.callback != null) {
            handleCallback(msg);
        } else {
            if (mCallback != null) {
                if (mCallback.handleMessage(msg)) {
                    return;
                }
            }
            handleMessage(msg);
        }
}

可见,当msg.callback== null 并且mCallback == null时,这个例子是由handleMessage(msg);处理消息,上面我们说到子类覆盖这个方法可以实现消息的具体处理过程。

总结:从上面的分析过程可知,消息循环的核心是Looper,Looper持有消息队列MessageQueue对象,一个线程可以把Looper设为该线程的局部变量,这就相当于这个线程建立了一个对应的消息队列。Handler的作用就是封装发送消息和处理消息的过程,让其他线程只需要操作Handler就可以发消息给创建Handler的线程。

可参考文章:Android异步消息处理机制完全解析,带你从源码的角度彻底理解

Handler+Looper+MessageQueue深入详解的更多相关文章

  1. Android异步处理三:Handler+Looper+MessageQueue深入详解

    在<Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面>中,我们讲到使用Thread+Handler的方式来实现界面的更新,其实是在非UI线程发送消息到U ...

  2. 讲讲Handler+Looper+MessageQueue 关系

    Handler+Looper+MessageQueue这三者的关系其实就是Android的消息机制.这块内容相比开发人员都不陌生,在面试中,或者日常开发中都会碰到,今天就来讲这三者的关系. 概述: H ...

  3. Handler Looper MessageQueue 之间的关系

    Handler Looper MessageQueue 之间的关系 handler在安卓开发中常用于更新界面ui,以及其他在主线程中的操作.内部结构大概图为: 1.handler持有一个Looper对 ...

  4. android Handler机制之ThreadLocal详解

    概述 我们在谈Handler机制的时候,其实也就是谈Handler.Message.Looper.MessageQueue之间的关系,对于其工作原理我们不做详解(Handler机制详解). Messa ...

  5. Handler,Looper,MessageQueue流程梳理

    目的:handle的出现主要是为了解决线程间通讯. 举个例子,android是不允许在主线程中访问网络,因为这样会阻塞主线程,影响性能,所以访问网络都是放在子线程中执行,对于网络返回的结果则需要显示在 ...

  6. android学习11——Handler,Looper,MessageQueue工作原理

    Message是Handler接收和处理的消息对象. 每个线程只能拥有一个Looper.它的loop方法读取MessageQueue中的消息,读到消息之后就把消息交给发送该消息的Handler进行处理 ...

  7. 【Android面试查漏补缺】之Handler详解,带你全面理解Handler消息机制

    在安卓面试中,关于 Handler 的问题是必备的,但是这些关于 Handler 的知识点你都知道吗? 一.题目层次 Handler 的基本原理 子线程中怎么使用 Handler MessageQue ...

  8. ProGuard代码混淆技术详解

    前言     受<APP研发录>启发,里面讲到一名Android程序员,在工作一段时间后,会感觉到迷茫,想进阶的话接下去是看Android系统源码呢,还是每天继续做应用,毕竟每天都是画UI ...

  9. Handler Looper 解析

    文章讲述Looper/MessageQueue/Handler/HandlerThread相关的技能和使用方法. 什么是Looper?Looper有什么作用? Looper是用于给线程(Thread) ...

随机推荐

  1. Veins(车载通信仿真框架)入门教程

    Veins入门教程——教你如何下手研究 目录 Veins入门教程——教你如何下手研究 目录 废话少说! 讲解omnetpp.ini!(挑关键的) 讲解RSUExampleScnario.ned! 注意 ...

  2. BFM模型介绍及可视化实现(C++)

    BFM模型介绍及可视化实现(C++) BFM模型基本介绍 Basel Face Model是一个开源的人脸数据库,其基本原理是3DMM,因此其便是在PCA的基础上进行存储的. 目前有两个版本的数据库( ...

  3. 透明度设置opacity

    透明度设置opacity属性 示例 <!DOCTYPE html> <html> <head> <style> div { background-col ...

  4. 从java到python

    从java到python 从事java开发主要是web方向,这个方向已经相当成熟,基本你想开发什么样的app,总能百度出来相关的项目或者github中搜索到,你需要做的就是掌握调试的技巧,基本你能想到 ...

  5. 替换word中的数据,并给导入word的图片添加水印

    public static void ExportWord(string tempFilePath, string outPath, Dictionary<string, string> ...

  6. 什么是浮点型?什么是单精度浮点数(float)以及双精度浮点数(double)?

    前言 作为一名java学习者,怎能不懂这些java基础中的基础呢?本文就带各位温顾温顾java浮点型.单精度浮点数.双精度浮点数. 浮点型 首先明确java中浮点型数据类型主要有:单精度float.双 ...

  7. C语言I—2019秋作业01

    1您对软件工程专业或计算机科学与技术专业了解是什么? 工程专业将成为一个新的热门专业.软件工程专业以计算机科学与技术学科为基础,突出软件开发的工程性,使学生在掌握计算机科学与技术方面知识和技能的基础上 ...

  8. CSPS模拟 78

    大敛好稳啊..居然在模拟赛拿了540.. 有点畏惧.jpg 而我就是什么什么不行级人物了.. 真正在联赛拉开那么多分怎么追啊.. T1kmp?hash? T2 概率小到炸精时,对答案也就没贡献了 然后 ...

  9. Android 开发中是否应该使用枚举?

    本文由咕咚发布在个人博客,转载请注明出处. 本文永久地址:https://gudong.name/2019/11/04/use-enum-or-not.html 在 Android 官方文档推出性能优 ...

  10. 最新JetBrainsPyCharm自动部署Python(Django,tornado等)项目至远程服务器

    每次开发Python项目时,对于所有Python开发人员来说,最枯燥的不是修改代码,而是实时将自己的代码上传至远程服务器,进行测试或者部署,本人最初开发也是这样,通过使用Xshell 5,WinSCP ...