现在用一个实例来演示一下自己的新建线程与UI线程间的通信。
UI界面包含3个控件:
一个输入框,用来输入数字;
一个显示框,用来显示从2开始,到输入数字之间的所有质数;
一个按钮,点击后获取输入框输入的数字,交给新建线程处理,线程计算质数后把结果传给UI线程,UI线程显示结果到显示框。
XML如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="fill_parent" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="5" > <TextView
android:id="@+id/textView_for"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="3"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" /> <EditText
android:id="@+id/editText1_upper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ems="10"
android:inputType="number" /> <Button
android:id="@+id/button_start_thread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="cal"
android:text="Button" />
</LinearLayout>
</ScrollView> </RelativeLayout>
逻辑代码如下:
package com.example.mystudy; import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView; import java.util.ArrayList;
import java.util.List; public class MyThreadTest extends Activity {
static final String UPPER_NUM = "upper";
static final String NUMS = "nums";
TextView textView;
EditText editText;
MyThread myThread; @Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.button_and_textview);
editText = (EditText) findViewById(R.id.editText1_upper);
textView = (TextView) findViewById(R.id.textView_for); myThread = new MyThread();
myThread.start();// 创建线程 } public void cal(View v) {// 点击事件 if (editText.getText().toString().equals("")) {
textView.setText("");
return;
}
Message message = new Message();
Bundle bundle = new Bundle();
bundle.putInt(UPPER_NUM, Integer.parseInt(editText.getText().toString()));// 将输入值发给新线程
message.setData(bundle);
message.what = 0x123;
myThread.mHander.sendMessage(message);// 调用自己的线程中的hander来发送message,将消息放进线程的消息队列中等待hander处理 } class MyThread extends Thread {
public Handler mHander;// 本线程的hander @Override
public void run() {
Looper.prepare();// 创建本线程的looper
mHander = new Handler() { // 实现自己的handler @Override
public void handleMessage(Message msg) {
if (msg.what == 0x123) {// 计算质数
int upper = msg.getData().getInt(UPPER_NUM);
List<Integer> nums = new ArrayList<Integer>();
outer:
for (int i = 2; i <= upper; i++) {
for (int j = 2; j < Math.sqrt(i); j++) {
if (i != 2 && i % j == 0) {
continue outer;
}
}
nums.add(i);
} Message msg1 = new Message();
Bundle bundle = new Bundle();
bundle.putCharSequence(NUMS, nums.toString());
msg1.setData(bundle);
msg1.what = 0x124;
mainHandler.sendMessage(msg1);// 发送给主线程 } } };
Looper.loop();// 启动looper
}
} Handler mainHandler = new Handler() {// UI线程的hander @Override
public void handleMessage(Message msg) {
if (msg.what == 0x124) {
String nums = msg.getData().getCharSequence(NUMS, "no result").toString();
textView.setText(nums.toString());
} } }; }