代码改变世界

[Android学习笔记]子线程更新UI线程方法之Handler

2014-03-17 21:01  hellenism  阅读(492)  评论(0编辑  收藏  举报

关于此笔记

 

不讨论:

1.不讨论Handler实现细节

2.不讨论android线程派发细节

 

讨论:

子线程如何简单的使用Handler更新UI


 

问题:

android开发时,如何在子线程更新UI?

 

Handler:

UI线程主要负责监听UI控件用户输入,进行事件的分发,事件的相应管理。当我们在子线程做完工作之后,由于子线程无法操作UI(因为子线程和UI线程不处于同一个上下文中),所以子线程需要与UI线程进行通信,此时就会用到Handler。可见Handler主要负责不同线程之间的通信。

 

Message:

Android中消息被封装成为Message对象,在不同线程之间传递,通过Handler发送和接受

 

子线程更新UI线程的一般步骤:

1.创建Handler对象,此Handler与创建它的线程绑定(哪个线程中创建的Handler对象,则此Handler就属于谁,一般为UI线程)

2.子线程做完处理之后,把需要回传的数据封装成为Message,调用上文提到的Handler对象的SendMessage方法,把Message对象从子线程,传递到与Handler绑定的线程中

3.在UI线程中编写代码,获取子线程传入的数据,使用数据更新UI


 

Ex:

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context=".MainActivity" >


    <TextView
        android:id="@+id/textViewTest"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
    <Button android:id="@+id/btn"
        android:text="Click"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
   

</LinearLayout>
XML

 

 

 

 1 package com.example.handlertest;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.os.Handler;
 6 import android.os.Message;
 7 import android.view.Menu;
 8 import android.view.View;
 9 import android.widget.Button;
10 import android.widget.TextView;
11 
12 public class MainActivity extends Activity {
13 
14     private TextView textView;
15     
16     // define an handler
17     private Handler handler;
18 
19     private Runnable runnable;
20 
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity_main);
25 
26         initData();
27 
28         initViews();
29     }
30 
31     @Override
32     public boolean onCreateOptionsMenu(Menu menu) {
33         // Inflate the menu; this adds items to the action bar if it is present.
34         getMenuInflater().inflate(R.menu.main, menu);
35         return true;
36     }
37 
38     private void initData() {
39         handler = new Handler() {
40             public void handleMessage(Message msg) {
41                 switch (msg.what) {
42                 case 1:
43                     // you can get data from Message
44                     String temp = (String)msg.obj;
45                     textView.setText(temp);
46                     break;
47                 }
48             }
49         };
50 
51         runnable = new Runnable() {
52             @Override
53             public void run() {
54                 // TODO Auto-generated method stub
55 
56                 // you want to something , in new thread
57                 //
58                 // do something
59                 //
60                 // done , then send message to UI Thread
61                 Message message = new Message();
62                 message.what = 1;
63                 message.obj = "success!";
64                 handler.sendMessage(message);
65             }
66 
67         };
68     }
69 
70     private void initViews() {
71         textView = (TextView)findViewById(R.id.textViewTest);
72         Button btn = (Button) findViewById(R.id.btn);
73         btn.setOnClickListener(new View.OnClickListener() {
74 
75             @Override
76             public void onClick(View v) {
77                 // TODO Auto-generated method stub
78 
79                 // create a new Thread to do something
80                 new Thread(runnable).start();
81             }
82         });
83     }
84 }
Java