位置: 编程技术 - 正文

Android消息队列模型(android消息队列使用)

编辑:rootadmin

推荐整理分享Android消息队列模型(android消息队列使用),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:消息队列软件,消息队列如何使用,android消息队列简易,消息队列js,消息队列js,android的消息机制,Android消息队列机制,android消息队列使用,内容如对您有帮助,希望把文章链接给更多的朋友!

Google官方给Handler的解释如下:

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.

Scheduling messages is accomplished with the post(Runnable), postAtTime(Runnable, long), postDelayed(Runnable, long),sendEmptyMessage(int), sendMessage(Message), sendMessageAtTime(Message, long), and sendMessageDelayed(Message, long) methods. The post versions allow you to enqueue Runnable objects to be called by the message queue when they are received; the sendMessage versions allow you to enqueue a Message object containing a bundle of data that will be processed by the Handler's handleMessage(Message) method (requiring that you implement a subclass of Handler).

When posting or sending to a Handler, you can either allow the item to be processed as soon as the message queue is ready to do so, or specify a delay before it gets processed or absolute time for it to be processed. The latter two allow you to implement timeouts, ticks, and other timing-based behavior.

When a process is created for your application, its main thread is dedicated to running a message queue that takes care of managing the top-level application objects (activities, broadcast receivers, etc) and any windows they create. You can create your own threads, and communicate back with the main application thread through a Handler. This is done by calling the same post or sendMessage methods as before, but from your new thread. The given Runnable or Message will then be scheduled in the Handler's message queue and processed when appropriate.

消息队列模型:

消息系统模型一般包括以上七个部分:消息原型、消息队列、消息发送、消息循环、消息获取、消息派发、消息处理。对应关系如下:

1. Message为消息原型,包含消息描述和数据

2. MessageQueue为消息队列

3. Looper完成消息循环

4. Handler驾驭整个消息系统模型,统领Message、MessageQueue和Looper

三 Handler工作原理分析

  要了解Handler工作原理,先看一下这个系统模型具体组成的层次结构框架是个什么样的。

      

Looper:

  实现Thread的消息循环和消息派发,缺省情况下Thread是没有这个消息循环的既没有Looper;

需要主动去创建,然后启动Looper的消息循环loop;与外部的交互通过Handler进行;

MessageQueue:

  消息队列,由Looper所持有,但是消息的添加是通过Handler进行;

  

  消息循环和消息队列都是属于Thread,而Handler本身并不具有Looper和MessageQueue;

Android消息队列模型(android消息队列使用)

但是消息系统的建立和交互,是Thread将Looper和MessageQueue交给某个Handler维护建立消息系统模型。

  所以消息系统模型的核心就是Looper。消息循环和消息队列都是由Looper建立的,

而建立Handler的关键就是这个Looper。

  一个Thread同时可以对应多个Handler,一个Handler同时只能属于一个Thread。Handler属于哪个

Thread取决于Handler在那个Thread中建立。

  在一个Thread中Looper也是唯一的,一个Thread对应一个Looper,建立Handler的Looper来自哪个Thread,

Handler属于哪个Thread。

  故建立Thread消息系统,就是将Thread的Looper交给Handler去打理,实现消息系统模型,完成消息的异步处理。

  

Handler与Thread及Looper的关系可以用下面图来表示:

    

Handler并不等于Thread,必须通过Thread的Looper及其MessageQueue,

用来实现Thread消息系统模型,依附于Thread上。

在线程建立Handler时:

  使Handler满足消息系统需要的条件,将Thread中的Looper和MessageQueue交给Handler来负责维护。

在线程中建立Handler,需要做以下工作:

  l 获取Thread中的Looper交给Handler的成员变量引用维护;

  l 通过Looper获取MessageQueue交给Handler的成员变量引用维护。

  那么消息系统模型建立完成之后,按照消息系统运行,

从Handler来看就是发送消息派发消息,与此线程消息系统的交互都由Handler完成。

消息发送和派发接口:

  l post(runnable)消息,Runnable是消息回调,经过消息循环引发消息回调函数执行;

  l sendMessage(Message)消息,经过消息循环派发消息处理函数中处理消息;

  l dispatchMessage 派发消息,若是post或带有回调函数则执行回调函数,否则执行

 消息处理函数Handler的handleMessage(通常派生类重写)。

  

标签: android消息队列使用

本文链接地址:https://www.jiuchutong.com/biancheng/370962.html 转载请保留说明!

上一篇:Android快速使用SharedPreferences(android快捷键设置)

下一篇:Android解决屏幕适配问题(安卓手机屏幕不好使了怎么办)

  • 采购合同缴纳印花税依据
  • 内账需要哪些单据
  • 制造业的加工费可以放制造费用的二级科目加工费吗
  • 公司支付宝账户要交税吗
  • 即征即退进项税额为哪些
  • 企业期货投资收益税务案例
  • 出口退税进项税额转出是什么意思
  • 进口一批原材料,海关核定的关税完税价格
  • 发票对方认证不了怎么回事
  • 特许权使用费如何确认收入
  • 税务局代开票流程是怎样的?
  • 跨月冲红的发票怎么做账
  • 销项税额抵减是怎么回事
  • 开票资料中没有电话可以吗
  • 经营租赁的固定资产计提折旧吗
  • 当月认证失控发票怎么做账处理?
  • 股东房产用于公司经营的是否交房产税
  • 代缴社保工资怎么做账
  • 哪些发票备注栏必须注明
  • 应付账款转营业外收入需要什么条件
  • 纳税人拒绝代扣代缴,扣缴义务人应当
  • 以公司名义购买房产的利与弊
  • c盘扩展卷是灰色的,有空间
  • msconfig配置文件修改
  • thinkphp怎么用
  • 扶贫入股分红政策
  • 企业在弥补亏损和提取法定盈余公积前一般不得分配利润
  • PHP:pg_num_rows()的用法_PostgreSQL函数
  • 苹果macOSBigSur是什么型号
  • 认知度高的人
  • php字段
  • 工程建设质保金规定
  • phpstudy配置https
  • 退车船税分录
  • 增值税申报表上期留抵税额怎么填
  • php获取文本内容
  • vuecdn方式引入
  • css转换器
  • 模型参数是什么意思
  • 现金折扣账务处理
  • 工资计提与发放在一个月行吗
  • 以前年度漏记收入
  • 员工为公司垫付的钱会计分录
  • 外资企业是否属于国企
  • 企业付美金外汇要交税吗
  • 会计中金额的正负怎么算
  • 一般纳税人应交增值税
  • 普通费用发票不入账会有什么后果吗
  • 购买银行理财产品安全吗
  • 普通发票一般几个点
  • 给离退休人员发放以前年度在岗奖金需要计提工会经费吗
  • 漏扣员工年终绩效违法吗
  • 产权转移书据印花税减免政策
  • 进项大于销项会退税吗
  • 外出经营预缴税款表怎么填
  • 公司挂靠有资质的企业公司会计处理?
  • 负数发票跨月怎么作废
  • sql只能查询数据,不能修改数据
  • mysql 5.7.18 zip版安装配置方法图文教程(win7)
  • 主板如何清除cmos
  • win7还能用几年
  • mac上网速度慢
  • mac os ftp
  • centos7查看目录
  • 电脑卡 硬盘100%
  • Mac OS X El Capitan公测版下载地址及安装教程图解
  • 提示无法启动打印作业
  • windows8.1显卡驱动
  • windows 打开文件命令
  • 系统播放音乐
  • js跨域8种解决方案
  • 简述javascript中的函数
  • opengl glu
  • c# opengl 3d
  • 控制程序的先后顺序是怎样的
  • css样式不能制作体积更小下载更快的网页
  • ie8不支持
  • js asc
  • jquery设置背景颜色
  • 广东省电子税务局app下载手机版
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设