Android四种定时器

1. 第一次种定时器
private void diyige() {
Timer timer =new Timer(); 
timer.schedule(task,2000,1000 );
}

final TimerTask task = new TimerTask() {    
   @Override  
  public void run() {     
   i++;   
   runOnUiThread(new Runnable() {   
      @Override     
       public void run() {     
       textView.setText(String.valueOf(i));    
       }  
    });
  }
};
2. 第二种定时器
 private void dierge() {
参数:  倒计时的时间     多少秒执行一次ontick方法
CountDownTimer count = new CountDownTimer(20000,1000) {    
@Override    
public void onTick(long millisUntilFinished) { 
       textView1.setText(String.valueOf(i));       
}
倒计时结束时调用
    @Override 
   public void onFinish() {    
     }   
  };   
       count.start();
}
3. 第三种定时器private void disanci() {
 final Handler han = new Handler();   
 han.post(new Runnable() {        
 @Override       
  public void run() {          
   textView2.setText(String.valueOf(i));   
     han.postDelayed(this,1000);  
     } 
  });
}
4. 第四种定时器
 private void disige() {   
 handler1.sendEmptyMessageDelayed(0, 1000);//启动handler,实    现1秒定时循环执行
 }private Handler handler1 = new Handler(){   
 @Override
  public void handleMessage(Message msg) {    
   super.handleMessage(msg);  
  textView3.setText(String.valueOf(i));  
一秒后调用自己
  handler1.sendEmptyMessageDelayed(0,1000);    
   }
};

推荐阅读更多精彩内容