Java/Android計時器(開始,暫停,恢複,停止)

來源:互聯網
上載者:User

標籤:arjinmc   android   java   countdown   timer   

由於要做暫停和恢複,這裡我就沒有使用Android的CountDownTimer,而是用了Java的Timer。所以,這個方法在java肯定是通用。我也外加了Android專屬的Service,有些計時器需要在Activiy關閉的情況下依然在計時,回到Activity時,顯示當前的計時狀態。

Timer 這個Java的類,具體看Java的API說明,但是要注意一點:

Timer 在cancel後,需要重新new 一次。

首先要給計時器定義三個狀態:準備,開始,暫停。

    public static final int PREPARE = 0;    public static final int START = 1;    public static final int PASUSE = 2;

1.準備:沒有開始計時,最初始狀態;

2.開始:已經開始倒計時

3.暫停:已經開始倒計時,而且使用者點擊了暫停


開始、恢複倒計時,我定義了這個方法:

    /**     * start count down     */    private void startTimer(){        timer = new Timer();        timerTask = new MyTimerTask();        timer.scheduleAtFixedRate(timerTask, 0, timer_unit);    }

timer_unit 就是執行timer 任務的時間,我定義為1秒。MyTimerTask是我定義的一個倒計時計算方法,如下:

    /**     * count down task     */    private class MyTimerTask extends TimerTask{        @Override        public void run() {            timer_couting -=timer_unit;            if(timer_couting==0){                cancel();                initTimerStatus();            }            mHandler.sendEmptyMessage(1);        }    }
timer_couting 是一個變數,記錄當前倒計時還剩餘多少時間,當剩餘時間為0時,倒計時結束,所以cancel結束倒計時。每次倒計一秒通過handler發送到主線程來更新ui提示倒計時資訊。

在介面上定義兩個按鈕,一個開始,一個停止,開始後可以暫停,暫停後可以恢複。

          case R.id.btn_start:                switch (timerStatus){                    case CountDownTimerUtil.PREPARE:                        startTimer();                        timerStatus = CountDownTimerUtil.START;                        btnStart.setText("PAUSE");                        break;                    case CountDownTimerUtil.START:                        timer.cancel();                        timerStatus = CountDownTimerUtil.PASUSE;                        btnStart.setText("RESUME");                        break;                    case CountDownTimerUtil.PASUSE:                        startTimer();                        timerStatus = CountDownTimerUtil.START;                        btnStart.setText("PAUSE");                        break;                }                break;            case R.id.btn_stop:                if(timer!=null){                    timer.cancel();                    initTimerStatus();                    mHandler.sendEmptyMessage(1);                }

對於Android應用中需要用到關閉了Activity後依然在計時,在這裡我們需要用Service,定義Service的情況跟上面類同,但是要注意的是這個計時Service是要單例模式,保證每次倒計時進來都是同一個倒計時。還需要定義一些方法來讓Activity給這個Service傳遞操作命令,開始,暫停,恢複,停止。

    /**     * start     */    public void startCountDown(){        startTimer();        timerStatus = CountDownTimerUtil.START;    }    /**     * paust     */    public void pauseCountDown(){        timer.cancel();        timerStatus = CountDownTimerUtil.PASUSE;    }    /**     * stop     */    public void stopCountDown(){        if(timer!=null){            timer.cancel();            initTimerStatus();            mCountDownTimerListener.onChange();        }    }
在Activity裡面擷取這個Service

countDownTimerService = CountDownTimerService.getInstance(new MyCountDownLisener()                ,service_distination_total);

項目源碼:https://github.com/arjinmc/Android-CountDownTimer







著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

Java/Android計時器(開始,暫停,恢複,停止)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.