Android线程Handler用法(二)---结合进度条例子(ProgressBar)

(好好理解这个例子,会明白线程与Handler的关系。)

HandlerActivity .java文件

package com.proper;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class HandlerActivity extends Activity {
/** Called when the activity is first created. */
private Button start;
private ProgressBar bar=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("点击按钮前");
setContentView(R.layout.main);
start=(Button)findViewById(R.id.start);
bar=(ProgressBar)findViewById(R.id.bar);
start.setText("start");
start.setOnClickListener(new StartButtonListener());
}
class StartButtonListener implements OnClickListener{

public void onClick(View v) {
bar.setVisibility(View.VISIBLE);
//将线程对象压进队列,立刻执行Rannable中的run方法。
handler.post(updateThread);
// TODO Auto-generated method stub
System.out.println("--------------1当前线程Id"+Thread.currentThread().getId());
System.out.println("--------------1当前线程名字"+Thread.currentThread().getName());
}
}
Handler handler=new Handler(){

@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
System.out.println("--------------3当前线程Id"+Thread.currentThread().getId());
System.out.println("--------------3当前线程名字"+Thread.currentThread().getName());
bar.setProgress(msg.arg1);
System.out.println("~~~~~~~~~~~~~~~~~~"+msg.arg1);

//将线程对象压进队列,立刻执行Rannable中的run方法。
handler.post(updateThread);
if(msg.arg1==50)
{
System.out.println("!!!!!!!!!!!^^^^^!!!!!!!!!!!!");
handler.removeCallbacks(updateThread);
System.out.println("****************************");
}
}

};
Runnable updateThread=new Runnable() {
int i=0;
public void run() {
// TODO Auto-generated method stub
System.out.println("--------------2当前线程Id"+Thread.currentThread().getId());
System.out.println("--------------2当前线程名字"+Thread.currentThread().getName());
i=i+10;
//得到一个消息对象
Message msg=handler.obtainMessage();
msg.arg1=i;
try{
Thread.sleep(1000);
}
catch(InterruptedException e){e.printStackTrace();}
//将消息压进消息队列里面,然后异步执行Handle中的handleMessage方法
handler.sendMessage(msg);
}

};
}

main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ProgressBar
android:layout_width="200dp"
android:id="@+id/bar"
style="?android:attr/progressBarStyleHorizontal"
android:visibility="gone"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
System.out输出结果

最终效果图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android中,线程倒计时可以通过多种方式实现,包括使用postDelayed、runOnUiThread和AsyncTask。 1. 使用postDelayed方式实现线程倒计时: ``` final Handler handler = new Handler(); handler.postDelayed(new Runnable() { int count = 10; @Override public void run() { // 更新UI tvCountDown.setText("" + count); count--; if (count >= 0) { handler.postDelayed(this, 1000); // 一秒后再次执行 } else { // 倒计时结束 } } }, 1000); // 延迟一秒后执行 ``` 2. 使用runOnUiThread方式实现线程倒计时: ``` new Thread(new Runnable() { int count = 10; @Override public void run() { while (count >= 0) { runOnUiThread(new Runnable() { @Override public void run() { // 更新UI tvCountDown.setText("" + count); } }); try { Thread.sleep(1000); // 暂停一秒 } catch (InterruptedException e) { e.printStackTrace(); } count--; } // 倒计时结束 } }).start(); ``` 3. 使用AsyncTask方式实现线程倒计时: ``` private class CountDownTask extends AsyncTask<Void, Integer, Void> { @Override protected Void doInBackground(Void... voids) { int count = 10; while (count >= 0) { publishProgress(count); try { Thread.sleep(1000); // 暂停一秒 } catch (InterruptedException e) { e.printStackTrace(); } count--; } return null; } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); // 更新UI tvCountDown.setText("" + values[0]); } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); // 倒计时结束 } } // 启动AsyncTask new CountDownTask().execute(); ``` 需要注意的是,以上三种实现方式都有可能会出现卡顿的情况,特别是在倒计时时间较长的情况下。为了避免卡顿,可以考虑使用CountDownTimer类来实现线程倒计时。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值