Detailed explanation of five methods of realizing timer in Android

1、 Timer

Timer is a class for Android to directly start timers. TimerTask is a sub thread, which is convenient to handle some complex and time-consuming functional logic. It is often used in combination with handler.

Compared with the timer implemented by the handler itself, the timer can do some complex processing. For example, it needs to sort the list with a large number of objects. The execution in TimerTask will not block the sub threads. It is often used in combination with the handler. After handling complex and time-consuming operations, the UI interface is updated through the handler.

Task: an object of TimerTask type. The run () method that implements TimerTask is a task to be executed periodically;

Delay: the delay time since the timer is initialized successfully.

Period: interval of timer.

The third parameter is the execution cycle, which is of type long.

Here are several ways to schedule tasks:

//Time is date type: execute timer.schedule (task, time) at the specified time// Firsttime is of type date and period is long, which means that it is executed every period milliseconds from the time of firsttime. timer.schedule(task,firstTime,period); // Delay is of long type: it will be executed once in milliseconds from now on. timer.schedule(task,delay); // Delay is long and period is long: it will be executed every period milliseconds after delay milliseconds from now on. timer.schedule(task,period); // Time is date type: execute once at the specified time. timer.schedule(task,period);

Note: 1. Cancel in taskondestory(), otherwise crash may occur

2. For apps that use TimerTask to perform certain operations regularly, even if they exit, TimerTask will still run for a while, but it cannot run for a long time

3. For some mobile phones, if you directly update the UI thread in TimerTask, you will not report an error and run normally, but be sure to note that the UI update must be executed in the main thread, otherwise you know when troubleshooting errors. And this thing consumes a lot of power, a lot of power, a lot of power. Say important things three times and turn it off when it's not in use

2、 Countdowntimer

Methods in ontick once

Until 10000 / 100 times, onfinish() will be executed finally

3、 AlarmManager

The above is the basic usage of the timer. First get the manager, and then define the flag of the alarm clock, the cycle time, and the pendingintent issued at the specified time.

Generally, the pendingintent is broadcast. We can customize a broadcast receiver to process our own functional logic by receiving the broadcast.

Here, you need to pay attention to the configuration in an independent process, which is defined by Android

1. The alarm timing does not require the program itself to maintain, but the system to maintain, so that the program can better avoid error prone problems and occupy system resources and CPU occupancy.

2. Even after the program exits, the program itself will not have any troublesome problems. The system will automatically call the corresponding components to execute the defined logic at the appointed time

3. Diversity of timing, including one-time timing and cycle timing (implemented on x-month-x, XX, from Monday to Friday, at what time every day...)

4、 Handler

Handler can help us operate the UI thread in the sub thread. For example, the sub thread parses data and notifies the UI to refresh the interface after parsing. It can also implement the timer itself.

As long as the handler. Sendemptymessage (0) is when the timer is started, the timer will start. The method of continuing the loop and stopping has been written in the comments.

Each cycle is operated in the main thread, avoiding the interspersed interaction between the sub thread and the main thread. Personally, I think it is easier to control than the timer, and the function implementation is also very simple.

Personally, I think it is more suitable for continuously updating the UI without complex and time-consuming processing. For example, in the player, we need to update the display of the current playback progress. Just update the text display, and using handler is a good choice.

5、 Thread

The timer implemented by thread is to create a sub thread in which the while loop can be used to update the UI through the handler. Personally, thread is no different from timer, but it looks different.

I think it's similar to timer. There are no special advantages

Similar to timer, multithreading often has problems if it is not considered carefully. Multiple threads with the same function often exist at the same time. Android itself has a limit on the number of sub threads, and running multiple threads at the same time in an app is a terrible thing. Therefore, like timer, it must be considered carefully when using it.

The above is a detailed explanation of the five methods of Android timer introduced by Xiaobian. I hope it will help you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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
分享
二维码
< <上一篇
下一篇>>