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 Timer timer in Android

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

Share

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

Most people do not understand the knowledge points of this article "how to use the Timer timer in Android", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use the Timer timer in Android" article.

Android, considering thread safety, does not allow UI threads to be executed in threads. In Android, there is an interesting class: android.os.Handler, which implements message delivery between threads everywhere. First look at a piece of code, this instantiated a Handler,Handler can communicate through multiple threads through Message, what I do here is a regular recLen plus 1, and then in a certain format, displayed on the recTime (operations on UI threads).

Final Handler handler = new Handler () {public void handleMessage (Message msg) {switch (msg.what) {case 1: recLen++; recTime.setText (GetRecTime (recLen)); break;} super.handleMessage (msg);}}

Let's instantiate a TimerTask, which provides a timed execution for the Timer. What I added to this method is to give Handler a function to send messages, because in the thread of Timer, the UI thread cannot be manipulated directly.

TimerTask task = new TimerTask () {public void run () {Message message = new Message (); message.what = 1; handler.sendMessage (message);}}

The rest of the work is much easier. Add a Timer to make the program run.

Timer = new Timer (true); timer.schedule (task,1000, 1000); / / execute after delaying 1000ms, 1000ms executes once / / timer.cancel (); / / exit timer

At this point, maybe some people still feel that I haven't written for a long time, and I don't know how to make the problem more clear. if I paste the wrong writing method that the author started, compared with the wrong writing method, maybe we can understand it more quickly.

TimerTask task = new TimerTask () {public void run () {recLen++; recTime.setText (GetRecTime (recLen)); / / operate UI thread directly in TimerTask. When debugging in a single step, it is found that the program has not been executed here}}; timer = new Timer (true); timer.schedule (task,1000, 1000) The above is about the content of this article on "how to use the Timer timer in Android". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

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