Android-初识Handler-子线程异步更新UI
来源:互联网 发布:网络歌曲钢琴简谱 编辑:程序博客网 时间:2024/05/08 14:56
简单的例子:子线程异步的更新UI,同时不影响主线程的操作本身界面的操作
下面是main.xml配置文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btnFirst" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="按钮1" > </Button> <Button android:id="@+id/btnSecond" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="按钮2" > </Button></LinearLayout>
代码:
package com.liujin.game;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.view.View.OnClickListener;import android.widget.Button;public class TestHandlerImage extends Activity implements OnClickListener {private Button btnFirst, btnSecond;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);btnFirst = (Button) this.findViewById(R.id.btnFirst);//得到按钮btnSecond = (Button) this.findViewById(R.id.btnSecond);btnFirst.setOnClickListener(this);//设置监控btnSecond.setOnClickListener(this);MyThread myThread = new MyThread();//开启子线程myThread.start();}@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.btnFirst:btnFirst.setText("我点击按钮1");btnSecond.setText("等待点击");break;case R.id.btnSecond:btnFirst.setText("等待点击");btnSecond.setText("我点击按钮2");break;}}class MyThread extends Thread {TitleEventHandler handler;public int sleepTime=1000;public int Interval=0;public void run() {Looper mainLooper = Looper.getMainLooper();//得到主线程的Looper,每一个Activity都默认自带一个Looper看上一篇的源码handler = new TitleEventHandler(mainLooper);handler.removeMessages(0);Message msg = null;Long start,end;int time = 15;while (true) {//下面的代码很简单就是定时的每过一秒发送一个消息给主线程try {time--;start=System.currentTimeMillis();msg = handler.obtainMessage(1, 1, 1, "当前还剩" + (time + 1)+ "秒");handler.sendMessage(msg);//发送消息end=System.currentTimeMillis();Interval=(int) (end-start);if(Interval<sleepTime){//定时睡觉疫苗Thread.sleep(sleepTime-Interval);}} catch (InterruptedException e) {}}}}class TitleEventHandler extends Handler {public TitleEventHandler() {super();}public TitleEventHandler(Looper looper) {super(looper);}@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);setTitle((String) msg.obj);}}}
- Android-初识Handler-子线程异步更新UI
- Android:在子线程中更新UI,解析异步消息处理机制(Handler)
- Android线程1 Handler处理更新UI子线程问题
- Android异步更新UI-线程池-Future-Handler实例分析
- Android子线程更新UI主线程方法之Handler
- Android子线程更新UI主线程方法之Handler
- 在Android子线程中初始化handler后,为什么该子线程也能更新UI?
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面 .
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面 .
- Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面
- HR与程序员是怎么读你的求职简历的
- Perl入门(一)
- 项目管理九大知识领域五大过程组
- VC中使用ADO操作数据库的方法
- 《那些年啊,那些事——一个程序员的奋斗史》——50
- Android-初识Handler-子线程异步更新UI
- vmware 下 ubuntu 全屏显示
- 设计模式学习笔记(原型模式)
- 数据源控件
- 过滤器
- 提升g++的编译速度的第一选择:ccache
- ubuntu 11.10通过rvm安装Ruby 1.9.2, Rails 3
- Ubuntu中Vmware Tools的安装与卸载
- 设计模式学习笔记(工厂方法模式)