android线程间通信
来源:互联网 发布:ubuntu usr在那里 编辑:程序博客网 时间:2024/04/29 20:35
andriod提供了 Handler 和 Looper 来满足线程间的通信。例如一个子线程从网络上下载了一副图片,当它下载完成后会发送消息给主线程,这个消息是通过绑定在主线程的Handler来传递的。
在Android,这里的线程分为有消息循环的线程和没有消息循环的线程,有消息循环的线程一般都会有一个Looper,这个事android的新 概念。我们的主线程(UI线程)就是一个消息循环的线程。针对这种消息循环的机制,我们引入一个新的机制Handle,我们有消息循环,就要往消息循环里 面发送相应的消息,自定义消息一般都会有自己对应的处理,消息的发送和清除,消息的的处理,把这些都封装在Handle里面,注意Handle只是针对那 些有Looper的线程,不管是UI线程还是子线程,只要你有Looper,我就可以往你的消息队列里面添加东西,并做相应的处理。
但是这里还有一点,就是只要是关于UI相关的东西,就不能放在子线程中,因为子线程是不能操作UI的,只能进行数据、系统等其他非UI的操作。
在Android,这里的线程分为有消息循环的线程和没有消息循环的线程,有消息循环的线程一般都会有一个Looper,这个是android的新概念。我们的主线程(UI线程)就是一个消息循环的线程。针对这种消息循环的机制,我们引入一个新的机制Handler,我们有消息循环,就要往消息循环里面发送相应的消息,自定义消息一般都会有自己对应的处理,消息的发送和清除,把这些都封装在Handler里面,注意Handler只是针对那 些有Looper的线程,不管是UI线程还是子线程,只要你有Looper,我就可以往你的消息队列里面添加东西,并做相应的处理。
但是这里还有一点,就是只要是关于UI相关的东西,就不能放在子线程中,因为子线程是不能操作UI的,只能进行数据、系统等其他非UI的操作。
一个Handler的创建它就会被绑定到这个线程的消息队列中,如果是在主线程创建的,那就不需要写代码来创建消息队列了,默认的消息队列会在主线程被创建。但是如果是在子线程的话,就必须在创建Handler之前先初始化线程的消息队列。如下面的代码:
view source
print?
01
classChildThreadextendsThread {
02
03
publicvoidrun() {
04
05
/*
06
* 创建 handler前先初始化Looper.
07
*/
08
Looper.prepare();
09
10
/*
11
* 在子线程创建handler,所以会绑定到子线程的消息队列中
12
*
13
*/
14
mChildHandler = new Handler() {
15
16
public void handleMessage(Message msg) {
17
18
/*
19
* Do some expensive operations there.
20
*/
21
}
22
};
23
24
/*
25
* 启动该线程的消息队列
26
*/
27
Looper.loop();
28
}
29
}
当Handler收到消息后,就会运行handleMessage(…)的回调函数,可以在里面做一些耗时的操作。
最后完成了操作要结束子线程时,记得调用quit()来结束消息循环队列。
view source
print?
1
mChildHandler.getLooper().quit();
下面是一个线程间通信的小例子:
view source
print?
001
/**
002
*
003
* @author allin.dev
004
* http://allin.cnblogs.com
005
*
006
*/
007
publicclassMainThreadextendsActivity {
008
009
privatestaticfinalString TAG ="MainThread";
010
privateHandler mMainHandler, mChildHandler;
011
privateTextView info;
012
privateButton msgBtn;
013
014
@Override
015
publicvoidonCreate(Bundle savedInstanceState) {
016
super.onCreate(savedInstanceState);
017
setContentView(R.layout.main);
018
019
info = (TextView) findViewById(R.id.info);
020
msgBtn = (Button) findViewById(R.id.msgBtn);
021
022
mMainHandler =newHandler() {
023
024
@Override
025
publicvoidhandleMessage(Message msg) {
026
Log.i(TAG,"Got an incoming message from the child thread - "
027
+ (String) msg.obj);
028
// 接收子线程的消息
029
info.setText((String) msg.obj);
030
}
031
032
};
033
034
newChildThread().start();
035
036
037
msgBtn.setOnClickListener(newOnClickListener() {
038
039
@Override
040
publicvoidonClick(View v) {
041
042
if(mChildHandler !=null) {
043
044
//发送消息给子线程
045
Message childMsg = mChildHandler.obtainMessage();
046
childMsg.obj = mMainHandler.getLooper().getThread().getName() +" says Hello";
047
mChildHandler.sendMessage(childMsg);
048
049
Log.i(TAG,"Send a message to the child thread - "+ (String)childMsg.obj);
050
051
052
}
053
}
054
});
055
056
}
057
058
publicvoidonDestroy() {
059
super.onDestroy();
060
Log.i(TAG,"Stop looping the child thread's message queue");
061
062
mChildHandler.getLooper().quit();
063
}
064
065
classChildThreadextendsThread {
066
067
privatestaticfinalString CHILD_TAG ="ChildThread";
068
069
publicvoidrun() {
070
this.setName("ChildThread");
071
072
//初始化消息循环队列,需要在Handler创建之前
073
Looper.prepare();
074
075
mChildHandler =newHandler() {
076
@Override
077
publicvoidhandleMessage(Message msg) {
078
Log.i(CHILD_TAG,"Got an incoming message from the main thread - "+ (String)msg.obj);
079
080
081
try{
082
083
//在子线程中可以做一些耗时的工作
084
sleep(100);
085
086
Message toMain = mMainHandler.obtainMessage();
087
toMain.obj ="This is "+this.getLooper().getThread().getName() +
088
". Did you send me /""+ (String)msg.obj +"/"?";
089
090
mMainHandler.sendMessage(toMain);
091
092
Log.i(CHILD_TAG,"Send a message to the main thread - "+ (String)toMain.obj);
093
094
}catch(InterruptedException e) {
095
// TODO Auto-generated catch block
096
e.printStackTrace();
097
}
098
}
099
100
};
101
102
Log.i(CHILD_TAG,"Child handler is bound to - "+ mChildHandler.getLooper().getThread().getName());
103
104
//启动子线程消息循环队列
105
Looper.loop();
106
}
107
}
108
}
- android线程间通信
- Android线程间通信
- Android线程间通信
- android 线程间通信
- Android线程间通信
- Android线程间通信
- android线程间通信
- Android 线程间通信
- android 线程间的通信
- android 线程间的通信
- android 线程间的通信
- android 线程间的通信
- android 线程间的通信
- android 线程间的通信
- android 线程间的通信
- Android中线程间通信
- android 线程间的通信
- android 线程间的通信
- asp.net中使用urlrewrite的方法
- 如何使我开发的程序的图标不使用系统的玻璃效果
- [转贴] 网管实战:Linux服务器静态路由配置
- Crossed ladders
- lucene
- android线程间通信
- 使用 /proc 文件系统来控制系统
- WARNING: Application does not specify an API level requirement!
- 光脚丫学LINQ系列演示
- php做DES加密,仿PKCS5填充方式做自定义字符填充
- Windows Mobile 打包过程
- 点击下载
- 就业感言:痛并快乐的4个月,圆梦之旅
- 使用VC打开超链接