当前位置:网站首页>handler+message [message mechanism]
handler+message [message mechanism]
2022-07-30 04:34:00 【bad ambassador】
作者 : 不良使
潜力创作新星 华为云享专家
Python+Android
博客记录学习的思路,项目和错误,寻找志同道合的朋友
如果觉得有帮助记得一键三连 ┗|`O′|┛ 嗷~~

引言
handler Mainly to solve the time-consuming operation of the same page.Take a look before looking at the methodAndroidThe time-consuming operation of the medium message mechanism is generally not performed in the main thread,Because it may cause blockage,加载慢,程序崩溃等.在Android项目中经常有碰到这样的问题,在子线程中完成耗时操作之后要更新UI,下面就自己经历的一些项目总结一下更新的方法.There are many ways to solve time-consuming operations that the main thread cannot complete.
🥭🥭1、Handler(消息机制)
🥭🥭2、runOnUiThread(在子线程中new一个主线程)
🥭🥭3、Looper
这篇文章讲解Handler(消息机制)
handler实现
handlerThe only way to deal with time-consuming operations that cannot be performed by child threads is 等价于 Assign time-consuming operations to others by way of tasks,The operation of assigning tasks is performed in the child thread(非耗时操作).举个例子吧,It takes a long time for a big company to do a big project,But assigning to a subsidiary does not require this time-consuming operation.It is only necessary to perform assignment of tasks to subsidiaries,The subsidiary can accept the project after completion.It is equivalent to completing a large project independently to the end, only needing to assign tasks and review project operations.
Create handlers and messages,发送数据.
Handler handler=new Handler();
Message message=new Message();
message.what=111;
message.obj=数据;//Data can be of primitive types、对象、列表、数组等
handler.sendMessage(message);
下面对handlerSome nouns and related terms are analyzed.
Handler handler=new Handler();
Handler:处理者,负责Message的发送及处理.使用Handler时,需要实现handleMessage(Message msg)方法来对特定的Message进行处理,例如更新UI等.new Handler()Equivalent to creating a handler.
Message message=new Message();
Message:消息,其中包含了消息ID,消息处理对象以及处理的数据等,由MessageQueue统一列队,终由Handler处理.new Message()相当于创建一个Message对象,Store data for later data transmission(任务分配)做准备
Speaking of newsmessage,How can you forget about message queues?
MessageQueue:消息队列,用来存放Handler发送过来的消息,并按照FIFO规则执行.当然,存放Message并非实际意义的保存,而是将Message以链表的方式串联起来的,等待Looper的抽取.looper下面会说
message.what=111;
这个相当于View.OnClickListener重写方法onClick(View v)的v.getId(),都是整型.
The following will use this,But it is recommended to write it in a class,便于项目管理.
message.obj=数据;//Data can be of primitive types、对象、列表、数组等
This code is the real core,Equivalent to assigning a value to thismessage(消息对象),The data type at this time may be that the data may be a primitive type、对象、列表、数组等
handler.sendMessage(message);
发送消息,将消息发送出去.
重写handleMessage方法,接收数据,执行耗时操作.
@Override
public boolean handleMessage(@NonNull Message msg) {
switch (msg.what){
case 111:
Toast.makeText(LoginMainActivity.this, "登陆失败" + mEtPassWord.getText().toString().trim(), Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return false;
}
上面的handlerSending messages can be based on
message.what=111;Send different data,handleMessage可以通过switch~case 区分出来.
handler有两种方法实现,原理都一样,一个直接new,A calling interface.There are two ways to choose according to the project.The above is the first method,但是原理是一样的,下面总结一下
第一种,接口implements

发送数据
Handler handler=new Handler();
Message message=new Message();
message.what=111;
message.obj=数据;//Data can be of primitive types、对象、列表、数组等
handler.sendMessage(message);
接收数据,执行耗时操作
@Override
public boolean handleMessage(@NonNull Message msg) {
switch (msg.what){
case 111:
Toast.makeText(LoginMainActivity.this, "登陆失败" + mEtPassWord.getText().toString().trim(), Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return false;
}
第二种,new个
The child thread sends data
private void updateWeather() {
new Thread(new Runnable(){
@Override
public void run() {
//耗时操作,完成之后发送消息给Handler,完成UI更新;
mHandler.sendEmptyMessage(0);
//传递数据
Message msg =new Message();
msg.obj = "数据";//Data can be of primitive types、对象、列表、数组等
mHandler.sendMessage(msg);
}
}).start();
}
主线程接收数据
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 0:
//拿到数据,执行耗时操作
String data = (String)msg.obj;
updateWeather();
textView.setText(data);
break;
case 1:
......
break;
...
default:
break;
}
}
};
🥭🥭2、runOnUiThread(在子线程中new一个主线程)
🥭🥭3、Looper见下一篇文章
嗯~~,看的不尽兴.*
在刷题之前先介绍一下牛客.Leetcode有的刷题牛客都有,除此之外牛客里面还有招聘(社招和校招)、一些上岸大厂的大佬的面试经验.
牛客是可以伴随一生的编程软件(完全免费),从学校到社会工作,时时刻刻你都可以用到,感兴趣的可以去注册试试可以伴随一生的刷题app
觉得有用的可以给个三连,关注一波!!!带你了解更多的Android小知识
边栏推荐
- Mini Program wx.miniProgram.navigateTo jump address cannot be tabbar address
- The 2nd Shanxi Province Network Security Skills Competition (Enterprise Group) Partial WP (10)
- Is the end of the universe a bank?Talk about those things about doing software testing in the bank
- 1. Get data - requests.get()
- What is CDH/CDP?
- 山西省第二届网络安全技能大赛(企业组)部分赛题WP(九)
- 网页元素解析a标签
- golang八股文整理(持续搬运)
- - B + tree index and MySQL series 】 【 what is the difference between a HASH index
- 【软件工程之美 - 专栏笔记】31 | 软件测试要为产品质量负责吗?
猜你喜欢

2.6基数排序(桶排序)

SSM框架简单介绍

sql statement - how to query data in another table based on the data in one table

handler+message【消息机制】

Introduction to database - MySQL simple introduction

sqlmap use tutorial Daquan command Daquan (graphics)
![[SQL] at a certain correlation with a table of data update another table](/img/66/4dff4383509e5d25890d8a24720de6.png)
[SQL] at a certain correlation with a table of data update another table

@WebServlet注解(Servlet注解)

精品MySQL面试题,备战八月99%必问!过不了面试算我的

2.4希尔排序
随机推荐
Repetition XXL - JOB scheduling center background arbitrary command execution
MySQL 安装报错的解决方法
How to use labelme
[The Mystery of Cloud Native] Cloud Native Background && Definition && Detailed explanation of related technologies?
权值线段树+线段树分裂/合并+CF1659D
unity初学5 摄像机跟随,边界控制以及简单的粒子控制(2d)
Go study notes (84) - Go project directory structure
[SQL] at a certain correlation with a table of data update another table
PyG builds R-GCN to realize node classification
SSM框架简单介绍
2021山东省网络搭建与应用赛项试题
Install MySQL Database on Kylin V10 Operating System
Solve the go environment can not compile exe
MySQL 字符串拼接 - 多种字符串拼接实战案例
Building and sharing the root of the digital world: Alibaba Cloud builds a comprehensive cloud-native open source ecosystem
获取本机IP和Request的IP
labelme的使用技巧
Atomic Guarantees of Redis Distributed Locks
Shanxi group (enterprises) in the second network security skills competition part problem WP (7)
Introduction to database - MySQL simple introduction