Handler发送消息
Handler发送消息小结
- obtainMessage()得到一个Message对象。
创建一个Message然后发送是这么写的:
Message msg = new Message();
msg.arg1 = 1;
msg.arg2 = 2;
msg.what = 3;
msg.obj = Object;
handler.sendMessage(msg);
性能优化后这样写:
Message msg = handler.obtainMessage();
msg.arg1 = 1;
msg.arg2 = 2;
msg.what = 3;
msg.obj = Object;
handler.sendMessage(msg);
相当于:
Message msg = handler.obtainMessage(int what,int arg1, int arg2, Object obj )
handler.sendMessage(msg);
再简练一点(这段代码在创建的Message中只传入两个参数,并让handler延迟10ms发送消息):
handler.sendMessageDelayed(handler.obtainMessage(int what,Object obj), 10);
至于接受消息,肯定是在handleMessage(Message msg)中:
Handler handler = new Handler(){
public void handleMessage(Message msg) {
super.handleMessage(msg);
//接收Message发送的消息 msg.what msg.arg1 msg.arg2 msg.obj
//这里msg.obj需要强转成你传过来的数据类型
}
};
给个实例看一看:
public boolean onTouch(View view, MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_UP) {
handler.sendMessageDelayed(handler.obtainMessage(TOUCH_EVENT_ID,view), 10);
}
return false;
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//msg.obj强转成View类型(传过来的obj是一个View类型的参数)
View scroller = (View)msg.obj;
if(msg.what==TOUCH_EVENT_ID) {
......
}
}
};
Handler发送消息的更多相关文章
- [Android]Handler的消息机制
最经面试中,技术面试中有一个是Handler的消息机制,细细想想,我经常用到的Handler无非是在主线程(或者说Activity)新建一个Handler对象,另外一个Thread是异步加载数据,同时 ...
- Handler实现消息的定时发送
话不多说,直接上代码 private Handler mHandler = new Handler() { @Override public void handleMessage(Message ms ...
- (原)Android在子线程用handler发送的消息,主线程是怎么loop到的?
来自知乎:https://www.zhihu.com/question/48130951?sort=created 大家都知道Android的Looper是ThreadLocal方式实现,每个线程 ...
- 使用Handlerf发送消息或使用Handler轮询时,报错IllegalStateException:This message is already in use.;
java.lang.IllegalStateException: { when=-107ms what=9 obj=com.saicmaxus.maxuslife.model.CarInfo@be47 ...
- Android 使用handler实现线程间发送消息 (主线程 与 子线程之间)、(子线程 与 子线程之间)
keyword:Android 使用handler实现线程间发送消息 (主线程 与 子线程之间).(子线程 与 子线程之间) 相信大家平时都有使用到异步线程往主线程(UI线程)发送消息的情况. 本文主 ...
- Android Handler类 发送消息-post()和postDelay(), Looper讲解
https://blog.csdn.net/weixin_41101173/article/details/79701832 首先,post和postDelay都是Handler的方法,用以在子线程中 ...
- android handler传递消息机制
当工作线程给主线程发送消息时,因为主线程是有looper的,所以不需要初始化looper,注意给谁发消息就关联谁的handler,此时用的就是主线程的handler handler会把消息发送到Mes ...
- Thread+Handler 线程 消息循环(转载)
近来找了一些关于android线程间通信的资料,整理学习了一下,并制作了一个简单的例子. andriod提供了 Handler 和 Looper 来满足线程间的通信.例如一个子线程从网络上下载了一副图 ...
- Android 主线程和线程之间相互发送消息
通过分析Activity源码,我们知道每个Activity都有一个Looper,所以主线程在接收Message是不需要调用Looper.prepare()和Looper.loop(),但是线程是不带L ...
随机推荐
- VoToucher
VoToucher package com.isoftstone.pcis.policy.common.utils; import com.isoftstone.pcis.policy.common. ...
- 【原创】用Pwnage + Redsnow 制作完美越狱固件
原帖我发表在威锋论坛 现在貌似IOS 7.X系 大行其道,就算不是IOS7.X ,很多人也装着IOS 6.X系. 进入正文前首先介绍一下自己目前的"环境" 设备:iphone4 G ...
- HDU 4637 Rain on your Fat brother 线段与半圆和线段交 简单题
题意: 应该不难读懂. 做法: 我们可以把雨滴看做静止不动,然后maze(这题的那个人)就是往左上方运动就可以了,计算出maze能跑到的最远的点,然后就是求起点和终点所构成的线段与每个雨滴交的时间,注 ...
- winform 在指定目录下已经生成资源Image图片的方式
假设在项目目录下存在一个Image目录,注意其中图片已经都设置成为:生成方式为资源文件. /// <summary> /// 得到要绘置的图片对像 /// </summary> ...
- 凯尔卡C68全球版汽车电脑诊断仪
产品简介: C68汽车故障诊断仪是凯尔卡公司新推出的一款集经济.简约.稳定.耐用于一体的汽车诊断设备, 该产品采用了最新的智能移植技术,集成度高:C68车型覆盖广,测试功能强大.数据准确等优点, 是目 ...
- SQL with(unlock)与with(readpast) (转)
所有Select加 With (NoLock)解决阻塞死锁,在查询语句中使用 NOLOCK 和 READPAST 处理一个数据库死锁的异常时候,其中一个建议就是使用 NOLOCK 或者 READPAS ...
- iframe式ajax调用示例
1.新建 a.html <!doctype html> <html> <head> <meta charset='utf-8'> <title&g ...
- Codeforces Round #342 (Div. 2) B. War of the Corporations 贪心
B. War of the Corporations 题目连接: http://www.codeforces.com/contest/625/problem/B Description A long ...
- UVA 573 (13.08.06)
The Snail A snail is at the bottom of a 6-foot well and wants to climb to the top.The snail can cl ...
- 微软ASP.NET网站部署指南(4):配置项目属性
1. 综述 有些部署设置能够在项目属性里设置的,而且保持到项目文件中(.csproj或.vbproj). 大多数情况下.你都能够在Visual Studio 选择项目属性Project Proper ...