关闭

实现Android计时与倒计时方法

2353人阅读 评论(0) 收藏 举报

 

今天给大家介绍几个Android中实现计时和倒计时的几种方法,这几种方法也许今后会帮上大家,现贴出来如下:

方法一

Timer与TimerTask(Java实现)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class timerTask extendsActivity{
  
    privateintrecLen = 11;
    privateTextView txtView;
    Timer timer =newTimer();
  
    publicvoidonCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
          
        setContentView(R.layout.timertask);
        txtView = (TextView)findViewById(R.id.txttime);
          
        timer.schedule(task,1000,1000);      // timeTask
    }   
  
    TimerTask task =newTimerTask() {
        @Override
        publicvoidrun() {
  
            runOnUiThread(newRunnable() {     // UI thread
                @Override
                publicvoidrun() {
                    recLen--;
                    txtView.setText(""+recLen);
                    if(recLen <0){
                        timer.cancel();
                        txtView.setVisibility(View.GONE);
                    }
                }
            });
        }
    };
}

方法二

TimerTask与Handler(不用Timer的改进型)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class timerTask extendsActivity{
    privateintrecLen = 11;
    privateTextView txtView;
    Timer timer =newTimer();
  
    publicvoidonCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
  
        setContentView(R.layout.timertask);
        txtView = (TextView)findViewById(R.id.txttime);
  
        timer.schedule(task,1000,1000);      // timeTask
    }   
  
    finalHandler handler =newHandler(){
        @Override
        publicvoidhandleMessage(Message msg){
            switch(msg.what) {
            case1:
                txtView.setText(""+recLen);
                if(recLen <0){
                    timer.cancel();
                    txtView.setVisibility(View.GONE);
                }
            }
        }
    };
  
    TimerTask task =newTimerTask() {
        @Override
        publicvoidrun() {
            recLen--;
            Message message =newMessage();
            message.what =1;
            handler.sendMessage(message);
        }
    };
}

方法三

Handler与Message(不用TimerTask)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class timerTask extendsActivity{
    privateintrecLen = 11;
    privateTextView txtView;
  
    publicvoidonCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
  
        setContentView(R.layout.timertask); 
        txtView = (TextView)findViewById(R.id.txttime);
  
        Message message = handler.obtainMessage(1);    // Message
        handler.sendMessageDelayed(message,1000);
    }  
  
    finalHandler handler =newHandler(){
  
        publicvoidhandleMessage(Message msg){        // handle message
            switch(msg.what) {
            case1:
                recLen--;
                txtView.setText(""+ recLen);
  
                if(recLen >0){
                    Message message = handler.obtainMessage(1);
                    handler.sendMessageDelayed(message,1000);     // send message
                }else{
                    txtView.setVisibility(View.GONE);
                }
            }
  
            super.handleMessage(msg);
        }
    };
}

方法四

Handler与Thread(不占用UI线程)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
public class timerTask extendsActivity{
    privateintrecLen = 0;
    privateTextView txtView;
  
    publicvoidonCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
  
        setContentView(R.layout.timertask);
        txtView = (TextView)findViewById(R.id.txttime);
          
        newThread(newMyThread()).start();         // start thread
    }   
  
    finalHandler handler =newHandler(){         // handle
        publicvoidhandleMessage(Message msg){
            switch(msg.what) {
            case1:
                recLen++;
                txtView.setText(""+ recLen);
            }
            super.handleMessage(msg);
        }
    };
  
    publicclassMyThread implementsRunnable{     // thread
        @Override
        publicvoidrun(){
            while(true){
                try{
                    Thread.sleep(1000);    // sleep 1000ms
                    Message message =newMessage();
                    message.what =1;
                    handler.sendMessage(message);
                }catch(Exception e) {
                }
            }
        }
    }
}

方法五

Handler与Runnable(最简单型)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class timerTask extendsActivity{
    privateintrecLen = 0;
    privateTextView txtView;
  
    publicvoidonCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
  
        setContentView(R.layout.timertask);
        txtView = (TextView)findViewById(R.id.txttime);
          
        handler.postDelayed(runnable,1000);
    }   
  
    Handler handler =newHandler();
    Runnable runnable =newRunnable() {
        @Override
        publicvoidrun() {
            recLen++;
            txtView.setText(""+ recLen);
            handler.postDelayed(this,1000);
        }
    };
}

计时与倒计时

方法1,方法2和方法3,都是倒计时

方法4,方法5,都是计时

计时和倒计时,都可使用上述方法实现(代码稍加改动)

 

UI线程比较

方法1,方法2和方法3,都是在UI线程实现的计时;

方法4和方法5,是另开Runnable线程实现计时

 

实现方式比较

方法1,采用的是Java实现,即Timer和TimerTask方式;

其它四种方法,都采用了Handler消息处理

 

 

 

 

http://www.itivy.com/android/archive/2011/10/25/some-method-android-timer.html

 

 

 

0
1

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:409415次
    • 积分:4612
    • 等级:
    • 排名:第6567名
    • 原创:58篇
    • 转载:33篇
    • 译文:3篇
    • 评论:58条
    最新评论