摘要:在程序开发时,对于比较耗时的操作,通常会为其开辟一个单独的线程来执行,以尽可能减少用户的等待时间。
在程序开发时,对于比较耗时的操作,通常会为其开辟一个单独的线程来执行,以尽可能减少用户的等待时间。在Android中,默认情况下,所有的操作都是在主线程中进行的,主线程负责与UI相关的事件。而在自己新建的线程中,不能对UI进行操作。因此Android提供了消息处理传递机制来解决这一问题。
Message,消息类。存放于MessageQueue中,包含数据类型,用户自定义的消息代码等。
MessageQueue,消息队列。在MessageQueue中,存放的消息按照FIFO(先进先出)的原则执行。
Handler,消息发送类。发送或者处理Message对象到所在线程的MessageQueue中。
Looper,循环者。用来循环读取存放于MessageQueue中的消息。一个线程对应一个Looper,一个Looper对象对应一个MessageQueue。Android中新增的线程是没有开启消息循环的,但是主线程除外。系统自动为主线程创建Looper对象。
一:在非主线程中创建Looper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class LooperThread extends Thread
{ public Handler
mHandler; //声明一个Handler对象 public void run()
{ Looper.prepare(); //初始化Looper对象 mHandler
= new Handler()
{ public void handleMessage(Message
msg) { //重写方法 //
process incoming messages here } }; Message
m=mHandler.obtainMessage(); //获取一个消息 m.what= 0x11 ;设置Message的what属性的值 mHandler.sendMessage(); //发送消息 Looper.loop(); //启动Looper } } |
二:一个打地鼠游戏
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
public class MainActivity extends Activity
{ private int i
= 0 ; //
记录其打到了几只地鼠 private ImageView
mouse; //
声明一个ImageView对象 private Handler
handler; //
声明一个Handler对象 public int [][]
position = new int [][]
{ { 231 , 325 },
{ 424 , 349 }, { 521 , 256 },
{ 543 , 296 },
{ 719 , 245 },
{ 832 , 292 }, { 772 , 358 }
}; //
创建一个表示地鼠位置的数组 @Override public void onCreate(Bundle
savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); mouse
= (ImageView) findViewById(R.id.imageView1); //
获取ImageView对象 mouse.setOnTouchListener( new OnTouchListener()
{ @Override public boolean onTouch(View
v, MotionEvent event) { v.setVisibility(View.INVISIBLE); //
设置地鼠不显示 i++; Toast.makeText(MainActivity. this , "打到[
" +
i + "
]只地鼠!" , Toast.LENGTH_SHORT).show(); //
显示消息提示框 return false ; } }); handler
= new Handler()
{ @Override public void handleMessage(Message
msg) { int index
= 0 ; if (msg.what
== 0x101 )
{ index
= msg.arg1; //
获取位置索引值 mouse.setX(position[index][ 0 ]); //
设置X轴位置 mouse.setY(position[index][ 1 ]); //
设置Y轴位置 mouse.setVisibility(View.VISIBLE); //
设置地鼠显示 } super .handleMessage(msg); } }; Thread
t = new Thread( new Runnable()
{ @Override public void run()
{ int index
= 0 ; //
创建一个记录地鼠位置的索引值 while (!Thread.currentThread().isInterrupted())
{ index
= new Random().nextInt(position.length); //
产生一个随机数 Message
m = handler.obtainMessage(); //
获取一个Message m.what
= 0x101 ; //
设置消息标识 m.arg1
= index; //
保存地鼠标位置的索引值 handler.sendMessage(m); //
发送消息 try { Thread.sleep( new Random().nextInt( 500 )
+ 500 ); //
休眠一段时间 } catch (InterruptedException
e) { e.printStackTrace(); } } } }); t.start(); //
开启线程 } } |
http://www.bdqn.cn/news/201405/13954.shtml