码迷,mamicode.com
首页 > 移动开发 > 详细

Android学习之----Handler消息

时间:2014-11-09 20:40:31      阅读:363      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   color   ar   os   使用   

Android系统规定,一些耗时的操作不能放在UI线程中去执行,这样会报一个ANR错误。所以为了避免该问题,我们需要开启一个新的线程去执行一些耗时操作;开启新的线程,将耗时的操作在新线程里面去执行, 但是子线程中不能更新UI界面,所以我们使用android的Handler机制可以解决这个问题。

 详细解释如下:     

         当应用程序启动时,Android首先会开启一个主线程 (也就是UI线程) , 主线程为管理界面中的UI控件,进行事件分发, 比如说, 你要是点击一个 Button ,Android会分发事件到Button上,来响应你的操作。  如果此时需要一个耗时的操作,例如: 联网读取数据,    或者读取本地较大的一个文件的时候,你不能把这些操作放在主线程中,如果你放在主线程中的话,界面会出现假死现象, 如果5秒钟还没有完成的话,,会收到Android系统的一个错误提示  "强制关闭".  这个时候我们需要把这些耗时的操作,放在一个子线程中,因为子线程涉及到UI更新,Android主线程是线程不安全的,也就是说,更新UI只能在主线程中更新,子线程中操作是危险的. 这个时候,Handler就出现了.,来解决这个复杂的问题 ,    由于Handler运行在主线程中(UI线程中),  它与子线程可以通过Message对象来传递数据, 这个时候,Handler就承担着接受子线程传过来的(子线程用sedMessage()方法传弟)Message对象,(里面包含数据)  , 把这些消息放入主线程队列中,配合主线程进行更新UI。

1。图示说明:

bubuko.com,布布扣

用一个实例来说明:

2.XML代码:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context=".MainActivity" >
10 
11     <TextView
12         android:id="@+id/msg"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content" />
15 
16     <Button
17         android:id="@+id/btn"
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
20         android:text="更新UI界面" />
21 
22 </RelativeLayout>

3.java代码:

 1 package com.example.handler;
 2 
 3 import android.os.Bundle;
 4 import android.os.Handler;
 5 import android.os.Message;
 6 import android.app.Activity;
 7 import android.view.Menu;
 8 import android.view.View;
 9 import android.view.View.OnClickListener;
10 import android.widget.Button;
11 import android.widget.TextView;
12 
13 public class MainActivity extends Activity {
14     private Mythread mythread;
15     private TextView mTextView;
16     private Button mButton;
17     public  class  myhandler extends Handler{
18         @Override
19         public void handleMessage(Message msg) {
20     
21             // TODO Auto-generated method stub
22             super.handleMessage(msg);
23             int count =    msg.arg1;
24             String  name =(String) msg.obj;
25             mTextView.setText(name);
26         }
27     }
28 
29     private final Handler handler = new Handler() {
30         public void handleMessage(Message msg) {
31             super.handleMessage(msg);
32             String name = (String) msg.obj;
33             int m = msg.arg1;
34             mTextView.append(name + m);
35         };
36     };
37 
38     @Override
39     protected void onCreate(Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41         setContentView(R.layout.activity_main);
42         mTextView = (TextView) this.findViewById(R.id.msg);
43         mButton = (Button) this.findViewById(R.id.btn);
44         mButton.setOnClickListener(new OnClickListener() {
45             @Override
46             public void onClick(View arg0) {
47                 mythread = new Mythread();
48                 new Thread(mythread).start();
49             }
50         });
51     }
52 
53     private class Mythread implements Runnable {
54         @Override
55         public void run() {
56             int count = 0;
57             while (count >= 20) {
58                 
59                 try {
60                     Thread.sleep(500);
61                 } catch (InterruptedException e) {
62                     // TODO Auto-generated catch block
63                     e.printStackTrace();
64                 }
65                 Message message = Message.obtain();//这里虽然可以new一个消息实例,但是最好别new.
66                 message.arg1 = count;
67                 message.obj = "tom";
68                 count++;
69                 handler.sendMessage(message);
70             }
71         }
72     }
73     
74     @Override
75     public boolean onCreateOptionsMenu(Menu menu) {
76         // Inflate the menu; this adds items to the action bar if it is present.
77         getMenuInflater().inflate(R.menu.main, menu);
78         return true;
79     }
80 }

 

Android学习之----Handler消息

标签:android   style   blog   http   io   color   ar   os   使用   

原文地址:http://www.cnblogs.com/summers/p/4085734.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!