<?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);
}
}
};
}