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

时间:2023-03-09 04:46:24
[Android学习笔记]子线程更新UI线程方法之Handler

关于此笔记

不讨论:

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

 package com.example.handlertest;

 import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends Activity { private TextView textView; // define an handler
private Handler handler; private Runnable runnable; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); initData(); initViews();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} private void initData() {
handler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
// you can get data from Message
String temp = (String)msg.obj;
textView.setText(temp);
break;
}
}
}; runnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub // you want to something , in new thread
//
// do something
//
// done , then send message to UI Thread
Message message = new Message();
message.what = 1;
message.obj = "success!";
handler.sendMessage(message);
} };
} private void initViews() {
textView = (TextView)findViewById(R.id.textViewTest);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub // create a new Thread to do something
new Thread(runnable).start();
}
});
}
}

Java