Java/android timer (start, pause, resume, stop)

Source: Internet
Author: User

Because of the pause and restore, I didn't use Android Countdowntimer, but I used a Java timer. So, this method is definitely generic in Java. I also have an Android-specific service, and some timers need to be timed when the Activiy is off, returning to activity, showing the current timing status.

Timer this Java class, specific to the Java API description, but note that:

After cancel, the Timer needs to be re-new again.

First, you need to define three states for the timer: Prepare, start, pause.

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

1. Preparation: No start time, most initial state;

2. Start: Countdown has begun

3. Pause: Countdown has started, and the user clicked Pause


To start and restore the countdown, I defined this method:

    /**     * Start count down */    private void Starttimer () {        timer = new timer ();        TimerTask = new Mytimertask ();        Timer.scheduleatfixedrate (timertask, 0, timer_unit);    }

Timer_unit is the time to perform the timer task, which I defined as 1 seconds. Mytimertask is a countdown calculation method that I have defined, as follows:

    /**     * 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 is a variable that records how much time is left in the current countdown, and when the remaining time is 0 o'clock, the countdown ends, so cancel ends the countdown. Updates the UI Tip countdown information by sending the handler to the main thread each time it is inverted.

Define two buttons on the interface, one start, one stop, you can pause after the start, you can resume after pausing.

          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);    } 

For Android applications that need to be closed after activity is still in the timing, here we need to use service, define the service is similar to the above, but note that this time service is a singleton mode, to ensure that each countdown comes in the same countdown. There is also a need to define methods for the activity to give the service delivery operation commands, start, pause, resume, stop.

    /**     * 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 ();        }    }
Get this service in activity

Countdowntimerservice = countdowntimerservice.getinstance (New Mycountdownlisener ()                , Service_distination_ Total);

Project Source: Https://github.com/arjinmc/Android-CountDownTimer







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java/android timer (start, pause, resume, stop)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.