2011年11月1日 星期二

Android的thread與handler


想好幾天,想不出個所以然,今天總算試出來了

看下面程式,一直會認為當run_clock=true,Log會顯示true,當我設run_clock=false,Log應該顯示出false才對,結果Log是true,false 交互出現,在設一次run_clock=false變成true false false百思不解,亂try一通還真try出來了


 class clock_Thread extends Thread{
    public void run(){
    super.run();
    try{
    do{
    Thread.sleep(1000);

    Message m=new Message();
   
    if(run_clock){
    w_handler.sendMessage(m);
    
    Log.d("tag", "true");
    }else{
    w_handler.removeCallbacks(this);
    Log.d("tag", "false");
    }
    }while(true);

    }catch (Exception e) {
    Log.d("tag", "error");
    // TODO: handle exception
    }//cttch
    }//run
    }//thread


正確程式碼如下,當設run_clock=false在else應多寫w_handler.removeCallbacks(this),這樣才是正確讓Handler不送出message

 class clock_Thread extends Thread{
    public void run(){
    super.run();
    try{
    do{
    Thread.sleep(1000);

    Message m=new Message();
   
    if(run_clock){
    w_handler.sendMessage(m);
    
    Log.d("tag""true");
    }else{
    w_handler.removeCallbacks(this);
    Log.d("tag""false");
    }
    }while(true);

    }catch (Exception e) {
    Log.d("tag""error");
    // TODO: handle exception
    }//cttch
    }//run
    }//thread

還是不對,上面是出現error而產生的thread異常終止,不是我想要的,似乎是app widget不能讓thread改變,若是把thread在Activity則正常工作,看下面程式,當對clock_Thread使用set_start(false)是可讓時間停止,set_start(true)是可讓時間再次顯示的





package test.clock1;
import java.util.Calendar;

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.widget.TextView;
public class Clock1Activity extends Activity {
private TextView mTextView;
public Calendar c_Calendar; //日曆
public int c_Minutes; //分
public int c_Hour; //時
public int c_Second; //秒
private boolean run_clock;
public Handler c_Handler;
private clock_Thread c_Thread;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mTextView=(TextView)findViewById(R.id.textView1);
mTextView.setTextSize(30);

//hander指派工作
c_Handler =new Handler(){
public void handleMessage(Message msg){
long time=System.currentTimeMillis();
final Calendar c_Calendar = Calendar.getInstance();
c_Calendar.setTimeInMillis(time);
c_Hour=c_Calendar.get(Calendar.HOUR);
c_Minutes=c_Calendar.get(Calendar.MINUTE);
c_Second=c_Calendar.get(Calendar.SECOND);
mTextView.setText(c_Hour+":"+c_Minutes+":"+c_Second);
}
};
//run_clock=true;
run_clock=false;
//產執行續物件
c_Thread = new clock_Thread();
//啟動執行續
c_Thread.start();
}
//button事件,啟動時間
public void clock_start(View cv) {
c_Thread.set_start(true);

}//start_clock
//button事件,停止時間
public void clock_stop(View cv) {
c_Thread.set_start(false);
}//stop_clock
//執行續
class clock_Thread extends Thread{
boolean START=true;
public void run(){
super.run();
try{
do{
Thread.sleep(1000);
if(START){
Message m=new Message();

// 將Message物件送入MessageQueue裡
Log.d("TAG", "OK");
c_Handler.sendMessage(m);
}else{
Log.d("TAG", "NO");
}
}while(true);

}catch (Exception e) {
// TODO: handle exception
}//cttch
}//run
public void set_start(boolean b){
this.START=b;
}

}//thread
}





沒有留言: