Android中幾種常用的定時器和延時方法


通過實際項目的練習,掌握了幾種android基本定時器和延時的用法,這里我想總結一下作為自己的收獲,下面列出的是比較簡潔的模式,方便簡單地在程序中直接調用。 
一、三種常用的定時器 
1.Handler類的postDelayed方法:

 1 Handler mHandler = new Handler();
 2                  Runnable r = new Runnable() {
 3 
 4                     @Override
 5                     public void run() {
 6                             //do something
 7                             //每隔1s循環執行run方法
 8                             mHandler.postDelayed(this, 1000);
 9                     }
10                 };
11 
12 
13 主線程中調用:  mHandler.postDelayed(r, 100);//延時100毫秒

2.用handler+timer+timeTask方法:

 1 Handler handler = new Handler() {
 2         @Override
 3         public void handleMessage(Message msg) {
 4             if (msg.what == 1){
 5                 //do something
 6             }
 7             super.handleMessage(msg);
 8         }
 9     };
10 
11     Timer timer = new Timer();
12     TimerTask timerTask = new TimerTask() {
13         @Override
14         public void run() {
15             Message message = new Message();
16             message.what = 1;
17             handler.sendMessage(message);
18         }
19     };
20  主線程中調用:timer.schedule(timerTask,1000,500);//延時1s,每隔500毫秒執行一次run方法

3.Thread+handler方法:

 1 Handler handler = new Handler() {
 2         @Override
 3         public void handleMessage(Message msg) {
 4             if (msg.what == 1){
 5                 //do something
 6             }
 7             super.handleMessage(msg);
 8 
 9         }
10     };
11 
12     class MyThread extends Thread {//這里也可用Runnable接口實現
13         @Override
14         public void run() {
15             while (true){
16                 try {
17                     Thread.sleep(1000);//每隔1s執行一次
18                     Message msg = new Message();
19                     msg.what = 1;
20                     handler.sendMessage(msg);
21                 } catch (InterruptedException e) {
22                     e.printStackTrace();
23                 }
24 
25             }
26         }
27     }
28 主線程中調用:new Thread(new MyThread()).start();

二、三種延時的快捷方法: 
1.Handler的postDelayed方法:

1 new Handler().postDelayed(new Runnable() {
2 
3         @Override
4         public void run() {
5               //do something
6           }
7        }, 1000);    //延時1s執行

2.timer + TimerTask方法:

1   timer = new Timer();
2                  timer.schedule(new TimerTask() {                   
3                     @Override
4                     public void run() {
5                           //do something
6                     }
7                 },1000);//延時1s執行

3.Thread方法:

 1  new Thread(new MyThread()).start();
 2         new Thread(new Runnable() {
 3             @Override
 4             public void run() {
 5                 try {
 6                     Thread.sleep(1000);//延時1s
 7                     //do something
 8                 } catch (InterruptedException e) {
 9                     e.printStackTrace();
10                 }
11             }
12         }).start();

轉載地址:http://blog.csdn.net/u011710991/article/details/50133455


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com 联系我们: