当前位置:网站首页>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小知识
边栏推荐
- 文件系统二
- Data Lake: Data Integration Tool DataX
- What is CDH/CDP?
- The difference between forward and redirect
- Atomic Guarantees of Redis Distributed Locks
- QT(39)-vs开发qt程序提示无法打开源文件
- Notes on "The Law of Construction"---Chapter 10 Typical Users and Scenarios
- 山西省第二届网络安全技能大赛(企业组)部分赛题WP(十)
- sql语句-如何以一个表中的数据为条件据查询另一个表中的数据
- KubeMeet 报名 | 「边缘原生」线上技术沙龙完整议程公布!
猜你喜欢
Repetition XXL - JOB scheduling center background arbitrary command execution
How to use labelme
Discourse Custom Header Links
在麒麟V10操作系统上安装MySQL数据库
数据库概论 - MySQL的简单介绍
Shell script basic editing specifications and variables
DAY17:弱口令的探测与测试
我的Go+语言初体验——祝福留言小系统,让她也可以感受到你的祝福
[MRCTF2020]Hello_misc
解决报错SyntaxError: (unicode error) ‘utf-8‘ codec can‘t decode byte 0xb7 in position 0: invalid start b
随机推荐
验证addShutdownHook钩子生效
代码开源设计实现思路
【翻译】Envoy Fundamentals,这是一个培训课程,使人们能够更快地采用Envoy Proxy。...
2.6归并排序
深圳见!云原生加速应用构建专场:来看云原生 FinOps、SRE、高性能计算场景最佳实践
2.4希尔排序
sqlmap use tutorial Daquan command Daquan (graphics)
软件测试员必看!数据库知识mysql查询语句大全
[MRCTF2020]Hello_ misc
MYSQL 唯一约束
js 操作在当前日期加减(天、周、月、年数)
Go 学习笔记(84)— Go 项目目录结构
【C语言】程序环境和预处理
(Problem practice) Conditional probability + weight line segment tree + FWT + suffix array
A brief introduction to the SSM framework
精品MySQL面试题,备战八月99%必问!过不了面试算我的
Unity beginner 5 cameras follow, border control and simple particle control (2 d)
【MySQL系列】-B+树索引和HASH索引有什么区别
[Redis Master Cultivation Road] Jedis - the basic use of Jedis
六、读取应用配置+日志配置