Android timer implementation

Source: Internet
Author: User

Experiment 1: use Java. util. Timer.
Create a timer in onstart (), update the counter every 5 seconds, and start. Java code?

12345678 mTimer = new
Timer();        mTimer.schedule(new
TimerTask() {                        @Override            public
void run() {                 ++mCount;
                mHandler.sendEmptyMessage(0);               
            }
        },
5*1000,
5*1000);

When you connect to the USB cable for debugging, you will find that everything works normally and the interface is updated every 5 seconds. Even if you press the power key, it will still be triggered once every 5 seconds.
When the USB cable is unplugged, press the power button to close the screen. After a period of time, it is found that the timer does not continue counting, and the number remains when the power button is disabled.

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.