Android 使用Handler实现Thread间通信
来源:互联网 发布:windows虚拟机装mac 编辑:程序博客网 时间:2024/05/21 11:07
http://blog.csdn.net/z496844387/article/details/6414261
1. 主线程传递消息给自己
public class test3_6 extends Activity implements OnClickListener{
private Button button;
private TextView text;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.button);
text = (TextView)findViewById(R.id.text);
button.setOnClickListener(this);
}
public void onClick(View v) {
Handler handler = new MyHandler();
Message msg = handler.obtainMessage(1, "开开心心每一天!");
handler.sendMessage(msg);
}
class MyHandler extends Handler{
public void handleMessage(Message msg) {
text.setText((String)msg.obj);
}
}
}
2. 在主线程中发送消息给其他线程
public class test3_6 extends Activity implements OnClickListener{
private Button button;
private TextView text;
private MyHandler myHandler;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.button);
text = (TextView)findViewById(R.id.text);
button.setOnClickListener(this);
//创建一个线程
MyThread myThread = new MyThread();
myThread.start();
// 不能在myThread.start();之后就立即使用myHandler,此时myHandler还没初始化成功
// Message msg = myHandler.obtainMessage(1, "开开心心每一天!");
// myHandler.sendMessage(msg);
}
public void onClick(View v) {
Message msg = myHandler.obtainMessage(1, "开开心心每一天!");
myHandler.sendMessage(msg);
}
class MyThread extends Thread{
public void run() {
Looper.prepare();
myHandler = new MyHandler(Looper.myLooper());
Looper.loop();
}
}
class MyHandler extends Handler{
public MyHandler(Looper looper) {
super(looper);
}
public void handleMessage(Message msg) {
System.out.println((String)msg.obj);
//不能在这里调用text.setText((String)msg.obj); 因为它违背了单线程模型:Android UI操作并不是线程安全的并且这些操作必须在UI线程中执行。
}
}
}
知识点:
1. Android系统中Looper负责管理线程的消息队列和消息循环。
2. 可以通过Loop.myLooper()得到当前线程的Looper对象,通过Loop.getMainLooper()可以获得当前进程的主线程的 Looper对象。
3. 可以通过Looper.prepare()创建消息队列,然后调用Looper.loop()进入消息循环。
4. 在主线程(UI线程)里,如果创建Handler时不传入Looper对象,那么将直接使用主线程(UI线程)的Looper对象(系统已经帮我们创建了);
在其它线程里,如果创建Handler时不传入Looper对象,那么,这个Handler将不能接收处理消息。
5. Handler的处理过程运行在创建Handler的线程里
6. 一个Looper对应一个MessageQueue
7. 一个线程对应一个Looper
8. 一个Looper可以对应多个Handler
9. 可以实现功能,刷新UI界面。但是这样是不行的,因为它违背了单线程模型:Android UI操作并不是线程安全的并且这些操作必须在UI线程中执行。
new Thread( new Runnable() {
public void run() {
myView.invalidate();
}
}).start();
3. 给子线程添加消息队列,使得UI线程和子线程护发消息
package com.android.test3;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class test3_6 extends Activity implements OnClickListener{
private Button button;
private TextView text;
private MyHandler myHandler;
private MainHandler mainHandler;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.button);
text = (TextView)findViewById(R.id.text);
button.setOnClickListener(this);
//创建一个线程
MyThread myThread = new MyThread();
myThread.start();
}
public void onClick(View v) {
if(myHandler != null){
Message msg = myHandler.obtainMessage(1, "开开心心每一天!");
myHandler.sendMessage(msg);
}
}
class MainHandler extends Handler{
public MainHandler(Looper looper) {
super(looper);
}
public void handleMessage(Message msg) {
text.setText((String)msg.obj);
}
}
class MyThread extends Thread{
public void run() {
//实例化mainHandler
mainHandler = new MainHandler(getMainLooper());
//实例化myHandler
Looper.prepare();
myHandler = new MyHandler(Looper.myLooper());
Looper.loop();
}
}
class MyHandler extends Handler{
public MyHandler(Looper looper) {
super(looper);
}
public void handleMessage(Message msg) {
if(mainHandler != null){
Message msg2 = mainHandler.obtainMessage(1, msg.obj);
mainHandler.sendMessage(msg2);
}
}
}
}
- Android 使用Handler实现Thread间通信
- Android 使用Handler实现Thread间通信
- Android 使用Handler实现Thread间通信
- android Thread/Handler 实现
- Android:Handler+Looper+MessageQueue+Thread(线程间的通信)随记
- Android使用Thread+Handler实现非UI线程更新界面
- Android handler实现线程间通信
- 使用handler实现进程间通信
- Android Handler+Thread实现更新Ui
- android Thread+handler 实现UI更新
- android handler和thread的使用
- Android-------Handler,Thread,Message使用范例
- android异步处理Handler+Thread使用进阶
- android handler和thread的使用
- Android 多线程:使用Thread和Handler
- android handler和thread的使用
- 关于Android中由 Thread 、Handler、MessageQueue、Looper组成的“线程间通信 套件” 的分析
- Android主线程、子线程通信(Thread+handler)
- Android 预定样式
- Android Activity
- 怎样学好编程(个人编程感受)!
- 不平凡的一年:项目管理故事50则(第2版)
- C#使用Dotfuscator混淆代码的加密方法
- Android 使用Handler实现Thread间通信
- Linux下批量杀掉 包含某个关键字的 程序进程
- 有用的一些网址:
- js判断中文
- 计算机经典书籍之程序设计语言
- va_start、va_end、va_list的使用
- 程序员未来的发展规划
- XCode工程中Targets详解
- SQL解释执行顺序