Android-初识Handler-子线程异步更新UI

本文介绍如何在Android中通过创建子线程实现异步更新UI的功能,同时确保主线程的操作不受影响,保持界面的流畅性。通过使用Handler和Looper机制,演示了如何在不阻塞主线程的情况下定时更新界面元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

简单的例子:子线程异步的更新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();

	}

	@Override
	public 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);
		}

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			setTitle((String) msg.obj);
		}
	}
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值