Android开发之Handler

我们都知道应用程序开启后,安卓会开启一个主线程(UI线程),主线程管理UI控件,进行事件分发。那为什么会出现Handler呢?

例如你要是点击一个 Button ,Android会分发事件到Button上,来响应你的操作。  如果此时需要一个耗时的操作,例如: 联网读取数据, 或者读取本地较大的一个文件的时候,你把这些操作放在主线程中, 如果5秒钟还没有完成的话,界面会出现假死现象,会收到Android系统的一个错误提示  "强制关闭"。  这个时候我们需要把这些耗时的操作,放在一个子线程中,因为子线程涉及到UI更新,Android主线程是线程不安全的, 也就是说,更新UI只能在主线程中更新,子线程中操作是危险的。 这个时候,安卓开发为了解决这个问题Handler就出现了。

handler可以分发Message对象和Runnable对象到主线程中, 每个Handler实例,都会绑定到创建他的线程中(一般是位于主线程),它有两个作用:

(1)安排消息或Runnable 在某个主线程中某个地方执行;

(2)安排一个动作在不同的线程中执行。

Handler分发消息的一些方法:

post(Runnable)

postAtTime(Runnable,long)

postDelayed(Runnable long)

sendEmptyMessage(int)

sendMessage(Message)

sendMessageAtTime(Message,long)

sendMessageDelayed(Message,long)

post类方法允许你排列一个Runnable对象到主线程队列中,

sendMessage类方法, 允许你安排一个带数据的Message对象到队列中,等待更新。

下面是一个简单的实例:

 1 package com.example.nihongdeng;
 2 import java.util.Timer;
 3 import java.util.TimerTask;
 4 import android.app.Activity;
 5 import android.os.Bundle;
 6 import android.os.Handler;
 7 import android.os.Message;
 8 import android.widget.TextView;
 9 
10 public class FrameLayoutTest extends Activity {
11 
12     private int currentColor=0;
13     final int []colors=new int[]
14             {
15                 R.color.color1,
16                 R.color.color2,
17                 R.color.color3,
18                 R.color.color4,
19                 R.color.color5,
20                 R.color.color6
21             };
22     final int []names=new int[]
23             {
24                 R.id.view01,
25                 R.id.view02,
26                 R.id.view03,
27                 R.id.view04,
28                 R.id.view05,
29                 R.id.view06
30             };
31     TextView []views=new TextView[names.length];
32     Handler handler=new Handler()
33     {
34         public void handleMessage(Message msg)//要接收数据必须重写此方法
35         {
36             if (msg.what==0x123)//接收到数据所进行的操作
37             {
38                 for (int i=0;i<names.length;i++)
39                 {
40                     views[i].setBackgroundResource(colors[(i+currentColor)%colors.length]);
41                 }
42                 currentColor++;
43             }
44             super.handleMessage(msg);
45         }
46     };
47     
48     @Override
49     protected void onCreate(Bundle savedInstanceState) {
50         super.onCreate(savedInstanceState);
51         setContentView(R.layout.main);
52         for (int i=0;i<names.length;i++)
53         {
54             views[i]=(TextView)findViewById(names[i]);
55         }
56         new Timer().schedule(
57                 new TimerTask()
58                 {
59 
60                     @Override
61                     public void run() {
62                         // TODO Auto-generated method stub
63                         handler.sendEmptyMessage(0x123);
64                     }
65                     
66                 },0,200);
67     }
68 }

这个例子的效果是实现一种霓虹灯的效果:

布局代码:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent">"
 5     <TextView 
 6         android:id="@+id/view01"
 7         android:layout_gravity="center"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:width="320px"
11         android:height="320px"
12         android:background="@color/color1"/>
13     <TextView 
14         android:id="@+id/view02"
15         android:layout_gravity="center"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:width="280px"
19         android:height="280px"
20         android:background="@color/color2"/>
21     <TextView 
22         android:id="@+id/view03"
23         android:layout_gravity="center"
24         android:layout_width="wrap_content"
25         android:layout_height="wrap_content"
26         android:width="240px"
27         android:height="240px"
28         android:background="@color/color3"/>
29     <TextView 
30         android:id="@+id/view04"
31         android:layout_gravity="center"
32         android:layout_width="wrap_content"
33         android:layout_height="wrap_content"
34         android:width="200px"
35         android:height="200px"
36         android:background="@color/color4"/>
37     <TextView 
38         android:id="@+id/view05"
39         android:layout_gravity="center"
40         android:layout_width="wrap_content"
41         android:layout_height="wrap_content"
42         android:width="160px"
43         android:height="160px"
44         android:background="@color/color5"/>
45     <TextView 
46         android:id="@+id/view06"
47         android:layout_gravity="center"
48         android:layout_width="wrap_content"
49         android:layout_height="wrap_content"
50         android:width="120px"
51         android:height="120px"
52         android:background="@color/color6"/>
53 
54 </FrameLayout>

代码我们通过Timer类来实现一个线程:

Timer是一种定时器工具,用来在一个后台线程计划执行指定任务。它可以计划执行一个任务一次或反复多次。
TimerTask一个抽象类,它的子类代表一个可以被Timer计划的任务。

schedule的意思(时间表、进度表)

timer.schedule(task,1000, 1000); //task是所要执行的任务,延时1000ms后执行,1000ms执行一次。

以上案例则是不延时执行,每0.2秒发送一次消息,接收到消息后对UI界面进行更新。

原文地址:https://www.cnblogs.com/hsshy/p/4752366.html