新建线程与UI线程间的通信
现在用一个实例来演示一下自己的新建线程与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());
} } }; }

新建线程与UI线程间的通信的更多相关文章
- C# 委托 / 跨线程访问UI / 线程间操作无效: 从不是创建控件“Form1”的线程访问它
C# 委托 / 跨线程访问UI / 线程间操作无效: 从不是创建控件“Form1”的线程访问它 网上的代码都比较复杂,还是这个简单 见代码, 简易解决办法: 主窗体代码 using System; ...
- [Android学习笔记]子线程更新UI线程方法之Handler
关于此笔记 不讨论: 1.不讨论Handler实现细节 2.不讨论android线程派发细节 讨论: 子线程如何简单的使用Handler更新UI 问题: android开发时,如何在子线程更新UI? ...
- OkHttp3几个简单的例子和在子线程更新UI线程的方法
okHttp用于android的http请求.据说很厉害,我们来一起尝尝鲜.但是使用okHttp也会有一些小坑,后面会讲到如何掉进坑里并爬出来. 首先需要了解一点,这里说的UI线程和主线程是一回事儿. ...
- C#中后台线程和UI线程的交互
在C#中,从Main()方法开始一个默认的线程,一般称之为主线程,如果在这个进行一些非常耗CPU的计算,那么UI界面就会被挂起而处于假死状态,也就是说无法和用户进行交互了,特别是要用类似进度条来实时显 ...
- Android ActivityThread(主线程或UI线程)简介
1. ActivityThread功能 它管理应用进程的主线程的执行(相当于普通Java程序的main入口函数),并根据AMS的要求(通过IApplicationThread接口,AMS为Client ...
- WPF线程获取UI线程
WPF中只能是UI线程才可以改变UI控件相关,当采用多线程工作时,可用以下代码获取 UI线程进行操作: App.Current.Dispatcher.Invoke((Action)delegate() ...
- C#用副线程改主线程(UI线程)的控件属性的方法(包括Winform和WPF)
C#用副线程去试图修改主线程的UI控件会报出异常,解决方案是使用副线程注册事件通知主线程自己去修改UI控件 在winform中,方法如下 private void button1_Click(obje ...
- 主线程与UI线程简介
---------------siwuxie095 Java 程序的主线程 当 Java 程序启动时,一个线程立刻运行,该线程通常叫做程 ...
- C#在非UI线程调用UI线程的控件
首先需要定义一个委托(delegate): private delegate void delegateSetProcessBarVal(int value); 然后定义一个方法来执行具体的操作: p ...
随机推荐
- HDU 1078 FatMouse and Cheese (记忆化搜索+dp)
详见代码 #include <iostream> #include <cstdio> #include <cstdlib> #include <memory. ...
- HTML5 移动应用开发环境搭建及原理分析
开发环境搭建: 一.Android 开发平台搭建 安装java jdk:\\10.194.151.132\Mewfile\tmp\ADT 配置java jdk 1) 新建系统变量,JAVA_HOME ...
- 一步一步写一个简单通用的makefile(一)
经常会用写一些小的程序有的是作为测试,但是每次都需要写一些简单的GCC 命令,有的时候移植一些项目中的部分代码到小程序里面进行测试,这个时候GCC 命令并不好些,如果写啦一个比较常用的makefile ...
- Axure原型用pmdaniu在线托管尝试
这次把原型中语音模块的坑填了一部分,实现了拖拽按钮控制的界面效果 http://www.pmdaniu.com/prototype/view?id=WXpVNwNhUmYMPFN3AkA
- 5 approach to load UIView from Xib
After the past few years I found that the only manageable way for creating/maintaining view (or any ...
- 4 weekend110的textinputformat对切片规划的源码分析 + 倒排索引的mr实现 + 多个job在同一个main方法中提交
好的,现在,来weekend110的textinputformat对切片规划的源码分析, Inputformat默认是textinputformat,一通百通. 这就是今天,weekend110的te ...
- DiscreteSeekBar---->SeekBar的使用
build: compile 'org.adw.library:discrete-seekbar:1.0.0' 在布局中的使用: <org.adw.library.widgets.discret ...
- Code Forces 711D Directed Roads
D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Java和Tomcat类加载机制
转自:http://blog.csdn.net/codolio/article/details/5027423 加载类是运行程序的基础,了解Java和Tomcat的类加载机制对更有效地开发.调试Web ...
- mongodb 学习笔记 09 -- shard分片
概述 shard 分片 就是 把不同的数据分在不同的server 模型 当中: 用户对mongodb的操作都是向mongs请求的 configsvr 用于保存,某条数据保存在哪个sha ...