Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Handler to realize timer and countdown timer function in Android

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "Android how to use Handler to achieve timer and countdown function", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Android how to use Handler to achieve timer and countdown function" bar!

What is Handler?

Handler is the upper interface of the Android message mechanism, which encapsulates many low-level details for us, so that we can easily use the underlying message mechanism. One of the most common application scenarios of Handler is to update UI indirectly in child threads through Handler. Handler has two main functions: one is to send messages; the other is to process messages, and its operation needs the support of the underlying Looper and MessageQueue. MessageQueue is a message queue, whose underlying layer is implemented with a single linked list; Looper is responsible for constantly fetching messages from MessageQueue in a loop, leaving it to Handler to process if it gets it, otherwise it will wait all the time. One thing to note about Looper is that Looper does not exist by default in threads other than the main thread. The main thread exists because the initialization of the ActivityThread is completed when the Looper is created.

Characteristics

Support operations:

Start

Pause

Resume

Cancel

Use

Allprojects {repositories {... Maven {url 'https://jitpack.io'}} dependencies {... Compile 'com.github.xesam:AndroidTimer:v0.1'}

TODO

Add ticker sequence

Use

CountTimer

New CountTimer (100) {@ Override public void onTick (long millisFly) {/ / millisFly is the Elapsed time at * Running State* vCountSwitcher.setText ((millisFly) + "); Log.d (" onTick ", millisFly +");}}

CountDownTimer

New CountDownTimer (10000,100) {@ Override public void onTick (long millisUntilFinished) {/ / millisUntilFinished is the left time at * Running State* Log.d ("onTick", millisFly + ");} @ Override public void onCancel (long millisUntilFinished) {} @ Override public void onPause (long millisUntilFinished) {} @ Override public void onResume (long millisUntilFinished) {} @ Override public void onFinish () {}}

Manage multiple scheduled tasks simultaneously using a single Handler

Create multiple tasks:

MultiCountTimer multiCountTimer = new MultiCountTimer; multiCountTimer.add (new CounterTimerTask (1) {@ Override public void onTick (long millisFly) {vMulti1.setText ("multi_1:" + millisFly);}}) .registerTask (new CounterTimerTask (2,100) {@ Override public void onTick (long millisFly) {vMulti2.setText ("multi_2:" + millisFly) ) .registerTask (new CounterTimerTask (3, 1000) {@ Override public void onTick (long millisFly) {vMulti3.setText ("multi_3:" + millisFly);}}); multiCountTimer.startAll ()

Cancel the task:

MultiCountTimer.cancel (2); or multiCountTimer.cancelAll (); thank you for reading, this is the content of "how Android uses Handler to achieve timer and countdown function". After the study of this article, I believe you have a deeper understanding of how Android uses Handler to achieve timer and countdown function, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report