android中timer的应用.本示例中采用的是用timer来控制ProgressBar的响应.

android中Timer的使用


private Timer timer = new Timer(true);
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
mProgress = (ProgressBar) findViewById(R.id.progress_bar);
timer.schedule(task, 0,50); ? //50表示隔多少MS执行一次.而0是一直循环下去. ?当然我这用的是progressbar.

}

TimerTask task = new TimerTask(){

@Override
public void run() {
// TODO Auto-generated method stub
Message message = new Message();
message.what = 0;
progressvalue++;
handler.sendMessage(message);
}

};

Handler handler = new Handler(){
public void handleMessage(Message msg)
{
switch (msg.what)
{
case 0:
{
mProgress.setProgress(progressvalue);
}
break;
default:
break;
}
super.handleMessage(msg);
}
};