Three implementation methods of Android timer (chrome, timer, handler)

This article shares three methods of Android timer for you. The specific contents are as follows

catalog:

1. With timer

2. Call handler.sendmessagedelay (message MSG, long delaymillis)

3. With layout Chrome

1. With timer

(1) Layout file

The layout file is very simple. It is a textview used to display the timing time. Let's take a look at the logical implementation in activity:

(2) Activity file

Open a new timer, obtain the startup time in the sub thread and convert it into string format, and use the handler to return the UI thread for display.

(3) Operation results:

2. Call handler.sendmessagedelay (message MSG, long delaymillis)

(1) The layout file is the same as method 1, and the running result is the same as method 1

(2) Activity file

Sendmessagedelayed (message MSG, long delaymillis): Send a message MSG after delaymillis / 1000 seconds.

The sendMessageDelayed method is invoked in the handleMessage () method of Handler, which cleverly implements the loop. It should be noted that starttimehandler. Sendmessagedelayed (message. Obtain (starttimehandler, 1000)) should be called once outside the handler as the entry of the loop.

3. With the help of layout Chrome

(1) Layout file

Layout chrome inherits from textview

(2) Activity file

The logic code is very simple. You can start timing by calling chronometer. Start().

Chronometer.setbase (long base): set the starting timing point. Here, you can get the startup time.

Chronometer. Start(): start timing with the time point set by setbase() above as the starting point. Just look at the source code of start():

Updaterunning() was called to follow the updaterunning() method:

Use the updatetext() method to set the time display. As for the timing loop mechanism, it is the same as method 2. It also calls the handmessagedelayed method of handler.

(3) Operation results:

Note: finally, let's talk about a common question about the chrome class. I see that many people ask how to format HH: mm: SS time with the chrome class. (if you have this problem, please continue to see. If there is no problem, please ignore it)

For children's shoes asking this question, first look at the description of the official document:

If the format string is null,or if you never call setFormat(),the Chronometer will simply display the timer value in "MM:SS" or "H:MM:SS" form.

In other words, by default, the format used is "mm: SS" or "H: mm: SS", and then children's shoes will ask: is it "mm: SS" or "H: mm: SS". Let's first look at the source code:

updateText():

Dateutils.formatelapsedtime is called. See what is in dateutils.formatelapsedtime:

There are many codes, so I'll pick the key points and intercept them. Take a closer look at the if () {} else{} statement above. You must have a sudden understanding?

For the correctness of our theory, slightly modify the code in method 3 activity:

chronometer.setBase(-18000000);

The operation results are as follows:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>