HandlerTest

 
<?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:id="@+id/bar"
	style="?android:attr/progressBarStyleHorizontal"
	android:layout_width="200dp"
	android:layout_height="wrap_content"/>
	<Button 
	android:id="@+id/start"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="start"/>
    <Button 
	android:id="@+id/pause"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="pause"/>
    <Button 
	android:id="@+id/end"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="end"/>
</LinearLayout>

package hyz.com;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

public class HandlerTestActivity extends Activity implements OnClickListener
{
	ProgressBar bar;
	Button start,pause,end; 
	int count = 0;
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
        bar = (ProgressBar)findViewById(R.id.bar); 
        start = (Button)findViewById(R.id.start);
        start.setOnClickListener(this);
        pause = (Button)findViewById(R.id.pause);
        pause.setOnClickListener(this);
        end = (Button)findViewById(R.id.end);
        end.setOnClickListener(this); 
 
    } 	
    @Override
	public void onClick(View v) 
    {
		if(v.equals(start))
		{
			Log.i("hyz", "onClick(startButton)"+Thread.currentThread().getId());	
			//把线程加入到线程队列	
			//注意:此方法并没有新建一个线程,通过输出日志你会发现它跟主Activity线程ID一样
			//实际上,post()方法执行的是run()方法			
			handler.post(updateThread);			
		}
		else
			if(v.equals(pause))
			{
				Log.i("hyz", "onClick(endButton)");
				start.setText("continue");
				handler.removeCallbacks(updateThread);
			}
			else
				{
					Log.i("hyz", "onClick(endButton)");
					count = 0;
					bar.setProgress(0);
					start.setText("start");
					handler.removeCallbacks(updateThread);
				}		
	}
    //线程类,该类使用匿名内部类的方式进行声明
    Runnable updateThread = new Runnable()
    {    	
		public void run() 
		{
			Log.i("hyz", "run(updateThread)-->count="+count);		
			Log.i("hyz","handlerId:"+Thread.currentThread().getId()+" handlerName:"+Thread.currentThread().getName());
			count = count + 10 ;
			//得到一个消息对象,Message类是有Android操作系统提供
			Message msg = handler.obtainMessage();
			//将msg对象的arg1参数的值设置为i,用arg1和arg2这两个成员变量传递消息,优点是系统性能消耗较少
			msg.arg1 = count ;	
			/*
			 * 将msg对象加入到消息队列当中,handler已存在2个队列	
			 * 注意:handler只管将msg发送到消息队列,而不管它执不执行,然后立即返回继续执行下一句代码。因为是异步
			 * 处理,在继续执行下一句代码的同时,handleMessage()方法也同时在处理消息。
			 */
			handler.sendMessage(msg);			
		}
    };
    //使用匿名内部类来复写Handler当中的handleMessage方法
    Handler handler = new Handler()
    {
    	public void handleMessage(Message msg) 
    	{
    		
    		Log.i("hyz", "handleMessage()");
			bar.setProgress(msg.arg1);
			if( count == 100)
			{
				Log.i("hyz", "over");
				//如果当i的值为100时,就将线程对象从handler当中移除
				handler.removeCallbacks(updateThread);
			}	 
			else
			{
				handler.postDelayed(updateThread, 1000);
			}			
		}    	
    };
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值