Android Handler机制(不含源码解析,适合新手理解)
一、官方解释
A Handler allows you to send and process
[Message](https://developer.android.com/reference/android/os/Message.html)
and Runnable objects associated with a thread's[MessageQueue](https://developer.android.com/reference/android/os/MessageQueue.html)
. 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 at 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)](https://developer.android.com/reference/android/os/Handler.html#post(java.lang.Runnable))
,[postAtTime(Runnable, long)](https://developer.android.com/reference/android/os/Handler.html#postAtTime(java.lang.Runnable,%20long))
,[postDelayed(Runnable, Object, long)](https://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable,%20java.lang.Object,%20long))
,[sendEmptyMessage(int)](https://developer.android.com/reference/android/os/Handler.html#sendEmptyMessage(int))
,[sendMessage(Message)](https://developer.android.com/reference/android/os/Handler.html#sendMessage(android.os.Message))
,[sendMessageAtTime(Message, long)](https://developer.android.com/reference/android/os/Handler.html#sendMessageAtTime(android.os.Message,%20long))
, and[sendMessageDelayed(Message, long)](https://developer.android.com/reference/android/os/Handler.html#sendMessageDelayed(android.os.Message,%20long))
methods. The post versions allow you to enqueue Runnable objects to be called by the message queue when they are received; the sendMessageversions allow you to enqueue a[Message](https://developer.android.com/reference/android/os/Message.html)
object containing a bundle of data that will be processed by the Handler's[handleMessage(Message)](https://developer.android.com/reference/android/os/Handler.html#handleMessage(android.os.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.
翻译:
处理程序允许您发送和处理与线程的MessageQueue相关联的消息和可运行对象。每个处理程序实例都与单个线程和线程的消息队列相关联。当您创建一个新的Handler时,它将绑定到正在创建它的线程的线程/消息队列——从那时起,它将向该消息队列传递消息和可运行文件,并在它们从消息队列中出来时执行它们。
处理程序有两种主要用途:(1)调度消息和将来某个时间点要执行的可运行文件;(2)为要在不同线程上执行的操作排队。
调度消息是通过post(Runnable)、postAtTime(Runnable,long)、postDelayed(Runnable,Object,long)、sendEmptyMessage(int)、sendMessage(Message)、sendMessageAtTime(Message,long)和sendMessageDelayed(Message,long)方法完成的。post版本允许您在收到消息队列时对要由消息队列调用的Runable对象进行排队;sendMessage版本允许您对消息对象进行排队,该消息对象包含将由Handler的handleMessage(Message)方法处理的数据束(需要实现汉德勒的一个亚类。
当发布或发送给处理程序时,您可以允许在消息队列准备就绪时立即处理项,或者指定在处理项之前的延迟或处理项的绝对时间。后两者允许您实现超时、滴答和其他基于时间的行为。
在为应用程序创建进程时,其主线程专用于运行消息队列,该队列负责管理顶级应用程序对象(活动、广播接收器等)及其创建的任何窗口。您可以创建自己的线程,并通过处理程序与主应用程序线程通信。这是通过调用相同的POST或SeNeMeST方法与以前一样,但是从新线程中调用。然后,给定的可运行或消息将被安排在处理程序的消息队列中,并在适当的时候进行处理。
装逼结束
二、个人理解(简书Handler作用与机制)
Handler,用于发送消息和处理消息。我们在开发中使用Handler一般用于更新UI,或者延时处理事件。 包括但不限于。
Handler机制,其工作核心主要包括四大部分:
Message、MessageQueue、Looper、Handler。这四大部分
其工作流程大致如下:
创建 消息(Massage ),
将消息存到消息队列(MessageQueue)当中,
由消息泵(Looper)将消息从消息队列中抽取出来,
并且交给Handler处理。
来个实践Demo感受一下吧。
代码如下:
注:xml代码我就不贴了,就是一个Button一个TextView。
public class MainActivity extends AppCompatActivity { Button button; TextView textView; private final int HANDLER_FLAG = 1; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case HANDLER_FLAG: textView.setText("我的点击后的值"); break; default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = findViewById(R.id.main_btn); textView = findViewById(R.id.main_tv); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handler.sendEmptyMessage(HANDLER_FLAG); } }); } }
下面是点击的效果图
点击前
点击之后
不仅仅可以用死值,当然也可以传值:
public class MainActivity extends AppCompatActivity { Button button; TextView textView; private final int HANDLER_FLAG = 1; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case HANDLER_FLAG: textView.setText(msg.obj.toString() + ""); break; default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = findViewById(R.id.main_btn); textView = findViewById(R.id.main_tv); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Message message = new Message(); message.obj = "这是点击后的值11111"; message.what = HANDLER_FLAG; handler.sendMessage(message); } }); } }
下面是效果图:
点击前就不贴了,直接上点击后的吧。
Handler中有很多消息,可以发送空消息,实体消息,还有延迟发送。
具体的使用可以去官方文档上看,或者百度,我这里仅仅贴出基本使用,和经常使用。
如果不足和建议,欢迎提出。
我的博客即将入驻“云栖社区”,诚邀技术同仁一同入驻。
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Android 多线程之线程池
Android 多线程系列 Android 多线程之几个基本问题 Android 多线程之阻塞队列 Android多线程之常见的线程形态 Android线程池的真正实现是ThreadPoolExecutor public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, defaultHandler); } corePoolSize:表示核心线程数。默认情况下核心线程会在线程池中一直存活,即使处于闲置状态。如果将ThreadPoolExecutor的allowCoreThreadTimeOut属性设置为true,那核心线程也会有超时策略,时间间隔由...
- 下一篇
记一次对`vulnhub`中`Android4`靶机渗透测试全过程
记一次对vulnhub中Android4靶机渗透测试全过程 首发于先知社区:https://xz.aliyun.com/t/2859 Android4靶机简介 名称:Android4 操作系统:Android v4.4 标志:/data/root/(在此目录中) 等级:初学者。 下载链接:Android4:https://download.vulnhub.com/android4/Android4.ova 搭建成功的Android平台 知识点 端口扫描和IP发现 使用adb_server_exec攻击端口8080代理 使用ADB连接到设备。 使用metasploit。 特权升级和找到答案 渗透过程 1:获取IP地址 使用命令:arp-scan -l 由此我们可以看到靶机的IP地址:192.168.232.146 2:扫描开放端口 在此扫描中,我们将使用最流行的工具nmap进行全端激进扫描 使用命令:nmap -p- -A 192.168.232.146 由此我确定存在与端口8080相关的某种网页。 没有任何延迟,我打开了网页,但什么也没找到。 大多数人都会确定使用POST方法涉及某种语...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8安装Docker,最新的服务器搭配容器使用
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS8编译安装MySQL8.0.19
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2整合Redis,开启缓存,提高访问速度
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Hadoop3单机部署,实现最简伪集群
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS6,7,8上安装Nginx,支持https2.0的开启