新建线程与UI线程间的通信

来源:互联网 发布:智慧城市大数据概念股 编辑:程序博客网 时间:2024/04/28 08:01
现在用一个实例来演示一下自己的新建线程与UI线程间的通信。

UI界面包含3个控件:

一个输入框,用来输入数字;

一个显示框,用来显示从2开始,到输入数字之间的所有质数;

一个按钮,点击后获取输入框输入的数字,交给新建线程处理,线程计算质数后把结果传给UI线程,UI线程显示结果到显示框。

XML如下:

 1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="fill_parent" 4     android:layout_height="fill_parent" > 5  6     <ScrollView 7         android:id="@+id/scrollView1" 8         android:layout_width="match_parent" 9         android:layout_height="fill_parent" >10 11         <LinearLayout12             android:layout_width="fill_parent"13             android:layout_height="fill_parent"14             android:orientation="vertical"15             android:weightSum="5" >16 17             <TextView18                 android:id="@+id/textView_for"19                 android:layout_width="wrap_content"20                 android:layout_height="wrap_content"21                 android:layout_gravity="center"22                 android:layout_weight="3"23                 android:text="Large Text"24                 android:textAppearance="?android:attr/textAppearanceLarge" />25 26             <EditText27                 android:id="@+id/editText1_upper"28                 android:layout_width="wrap_content"29                 android:layout_height="wrap_content"30                 android:layout_gravity="center"31                 android:ems="10"32                 android:inputType="number" />33 34             <Button35                 android:id="@+id/button_start_thread"36                 android:layout_width="wrap_content"37                 android:layout_height="wrap_content"38                 android:layout_gravity="center"39                 android:onClick="cal"40                 android:text="Button" />41         </LinearLayout>42     </ScrollView>43 44 </RelativeLayout>

逻辑代码如下:

  1 package com.example.mystudy;  2   3 import android.app.Activity;  4 import android.os.Bundle;  5 import android.os.Handler;  6 import android.os.Looper;  7 import android.os.Message;  8 import android.view.View;  9 import android.widget.EditText; 10 import android.widget.TextView; 11  12 import java.util.ArrayList; 13 import java.util.List; 14  15 public class MyThreadTest extends Activity { 16     static final String UPPER_NUM = "upper"; 17     static final String NUMS = "nums"; 18     TextView textView; 19     EditText editText; 20     MyThread myThread; 21  22     @Override 23     protected void onCreate(Bundle savedInstanceState) { 24  25         super.onCreate(savedInstanceState); 26         setContentView(R.layout.button_and_textview); 27         editText = (EditText) findViewById(R.id.editText1_upper); 28         textView = (TextView) findViewById(R.id.textView_for); 29  30         myThread = new MyThread(); 31         myThread.start();// 创建线程 32  33     } 34  35     public void cal(View v) {// 点击事件 36  37         if (editText.getText().toString().equals("")) { 38             textView.setText(""); 39             return; 40         } 41         Message message = new Message(); 42         Bundle bundle = new Bundle(); 43         bundle.putInt(UPPER_NUM, Integer.parseInt(editText.getText().toString()));// 将输入值发给新线程 44         message.setData(bundle); 45         message.what = 0x123; 46         myThread.mHander.sendMessage(message);// 调用自己的线程中的hander来发送message,将消息放进线程的消息队列中等待hander处理 47  48     } 49  50     class MyThread extends Thread { 51         public Handler mHander;// 本线程的hander 52  53         @Override 54         public void run() { 55             Looper.prepare();// 创建本线程的looper 56             mHander = new Handler() { // 实现自己的handler 57  58                 @Override 59                 public void handleMessage(Message msg) { 60                     if (msg.what == 0x123) {// 计算质数 61                         int upper = msg.getData().getInt(UPPER_NUM); 62                         List<Integer> nums = new ArrayList<Integer>(); 63                         outer: 64                         for (int i = 2; i <= upper; i++) { 65                             for (int j = 2; j < Math.sqrt(i); j++) { 66                                 if (i != 2 && i % j == 0) { 67                                     continue outer; 68                                 } 69                             } 70                             nums.add(i); 71                         } 72  73                         Message msg1 = new Message(); 74                         Bundle bundle = new Bundle(); 75                         bundle.putCharSequence(NUMS, nums.toString()); 76                         msg1.setData(bundle); 77                         msg1.what = 0x124; 78                         mainHandler.sendMessage(msg1);// 发送给主线程 79  80                     } 81  82                 } 83  84             }; 85             Looper.loop();// 启动looper 86         } 87     } 88  89     Handler mainHandler = new Handler() {// UI线程的hander 90  91         @Override 92         public void handleMessage(Message msg) { 93             if (msg.what == 0x124) { 94                 String nums = msg.getData().getCharSequence(NUMS, "no result").toString(); 95                 textView.setText(nums.toString()); 96             } 97  98         } 99 100     };101 102 }

 

0 0
原创粉丝点击