class="java" name="code"> package android.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 HandlerTest extends Activity { /** Called when the activity is first created. */ private Button startButton; private Button endButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //根据id获得控件对象 startButton = (Button)findViewById(R.id.startButton); endButton = (Button)findViewById(R.id.endButton); //为控件设置监听器 startButton.setOnClickListener(new StartButtonListener()); endButton.setOnClickListener(new EndButtonListener()); } class StartButtonListener implements OnClickListener{ public void onClick(View v) { //调用Handler的post()方法,将要执行的线程对象放到队列当中 handler.post(updateThread); } } class EndButtonListener implements OnClickListener{ public void onClick(View v) { //调用Handler的removeCallbacks()方法,删除队列当中未执行的线程对象 handler.removeCallbacks(updateThread); } } //创建Handler对象 Handler handler = new Handler(); //新建一个线程对象 Runnable updateThread = new Runnable(){ //将要执行的操作写在线程对象的run方法当中 public void run(){ System.out.println("updateThread"); //调用Handler的postDelayed()方法 //这个方法的作用是:将要执行的线程对象放入到队列当中,待时间结束后,运行制定的线程对象 //第一个参数是Runnable类型:将要执行的线程对象 //第二个参数是long类型:延迟的时间,以毫秒为单位 handler.postDelayed(updateThread, 3000); } }; }
package android.handler; 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 ProgressBarHandlerTest extends Activity { /** Called when the activity is first created. */ private ProgressBar progressBar; private Button startButton; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); progressBar = (ProgressBar)findViewById(R.id.progressbar); startButton = (Button)findViewById(R.id.startButton); startButton.setOnClickListener(new ProgressBarOnClickListener()); } class ProgressBarOnClickListener implements OnClickListener{ public void onClick(View v) { //设置进度条为可见状态 progressBar.setVisibility(View.VISIBLE); updateBarHandler.post(updateThread); } } //使用匿名内部类来复写Handler当中的handlerMessage()方法 Handler updateBarHandler = new Handler(){ @Override public void handleMessage(Message msg) { progressBar.setProgress(msg.arg1); updateBarHandler.post(updateThread); //将要执行的线程放入到队列当中 } }; //线程类,该类使用匿名内部类的方式进行声明 Runnable updateThread = new Runnable(){ int i = 0; public void run() { // TODO Auto-generated method stub System.out.println("Begin Thread"); i+=10; //得到一个消息对象,Message类是android系统提供的 Message msg = updateBarHandler.obtainMessage(); //将Message对象的arg1参数的值设置为i msg.arg1 = i; //用arg1、arg2这两个成员变量传递消息,优点是系统性能消耗较少 try{ Thread.sleep(1000); //让当前线程休眠1000毫秒 }catch(InterruptedException ex){ ex.printStackTrace(); } //将Message对象加入到消息队列当中 updateBarHandler.sendMessage(msg); //如果i的值等于100 if (i == 100){ //将线程对象从队列中移除 updateBarHandler.removeCallbacks(updateThread); } } }; }