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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值