项目1:HandlerTest
=>>HandlerActivity.java
package mars.handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class HandlerActivity extends Activity {
/** Called when the activity is first created. */
//声明两个按钮控件
private Button startButton = null;
private Button endButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//根据控件的ID得到代表控件的对象,并未这两个按钮设置相应的监听器
startButton = (Button)findViewById(R.id.startButton);
startButton.setOnClickListener(new StartButtonListener());
endButton = (Button)findViewById(R.id.endButton);
endButton.setOnClickListener(new EndButtonListener());
}
class StartButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
//调用Handler的post方法,将要执行的线程对象添加到队列当中
handler.post(updateThread);
}
}
class EndButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
handler.removeCallbacks(updateThread);
}
}
//创建一个Handler对象
Handler handler = new Handler();
//将要执行的操作写在线程对象的run方法当中
Runnable updateThread = new Runnable(){
@Override
public void run() {
System.out.println("UpdateThread");
//在run方法内部,执行postDelayed或者是post方法
handler.postDelayed(updateThread, 3000);
}
};
}
项目2:ProgressBarHandler
=>>TestBarHandler.java
package mars.barhandler;
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 TestBarHandler extends Activity {
/** Called when the activity is first created. */
//声明控件变量
ProgressBar bar = null;
Button startButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//根据控件的ID得到代表控件的对象,并为按钮设置监听器
bar = (ProgressBar)findViewById(R.id.bar);
startButton = (Button)findViewById(R.id.startButton);
startButton.setOnClickListener(new ButtonListener());
}
//当点击startButton按钮时,就会执行ButtonListener的onClick方法
class ButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bar.setVisibility(View.VISIBLE);
updateBarHandler.post(updateThread);
}
}
//使用匿名内部类来复写Handler当中的handleMessage方法
Handler updateBarHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
bar.setProgress(msg.arg1);
updateBarHandler.post(updateThread);
}
};
//线程类,该类使用匿名内部类的方式进行声明
Runnable updateThread = new Runnable(){
int i = 0 ;
@Override
public void run() {
System.out.println("Begin Thread");
i = i + 10 ;
//得到一个消息对象,Message类是有Android操作系统提供
Message msg = updateBarHandler.obtainMessage();
//将msg对象的arg1参数的值设置为i,用arg1和arg2这两个成员变量传递消息,优点是系统性能消耗较少
msg.arg1 = i ;
try {
//设置当前显示睡眠1秒
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//将msg对象加入到消息队列当中
updateBarHandler.sendMessage(msg);
if( i == 100){
//如果当i的值为100时,就将线程对象从handler当中移除
updateBarHandler.removeCallbacks(updateThread);
}
}
};
}