Several methods of realizing timer in Android are explained in detail

preface

I'm reading the official Android development documents these days. There are many development suggestions worth thinking about. Friends who have time can go and have a look (the official documents are in English. If you can't understand them, you can translate and compare them through the browser plug-in, which is still very convenient).

One of the courses mentioned the AlarmManager. This class was only used to understand that it is an alarm clock manager. If it is better than reminders, alarm clocks and other software, it needs to be used. The official example is used to realize the timer. I suddenly feel that this is a very magical thing. I collected some information and wrote down several methods I know to realize the timer, so as to deepen my memory and share it with you.

text

I use several classes to implement timers: handler, timer, thread and AlarmManager.

AlarmManager

AlarmManager is an open alarm clock function of the system, and its use mode is no different from that of ordinary manager.

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

Summary of advantages##

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...)

Applicable scenario##

Personally, I think it is more suitable for independent function logic. For example, if the app needs to regularly grab the latest data from the server, using an independent service will be separated from the main function and easy to maintain. The key is low power consumption and not easy to make mistakes.

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.

Summary of advantages##

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.

Applicable scenario##

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.

Timer

Timer is a class for Android to directly start the timer, and it is also the first tool class I came into contact with that can realize the function of timer.

His usage is generally known:

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

Period: interval of timer.

Summary of advantages##

Timer is easy to use. 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.

Applicable scenario

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.

Make complaints about mobile phone: for some phones, if you update UI threads directly in TimerTask, it will not be wrong, and run normally. But be careful, update UI must be executed in the main thread, otherwise you will know when checking errors. And this thing consumes a lot of power. It consumes a lot of power. Say important things three times. Be sure to turn it off when you don't use it. Use it with caution**

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.

Summary of advantages

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

Applicable scenario

It's similar to timer. If you don't think carefully about multithreading, there will often be problems. Multiple threads with the same function often exist at the same time. Android itself has a limit on the number of sub threads, and it's a terrible thing for an app to run multiple threads at the same time. Therefore, like timer, you must think carefully when using it.

ending

The above are several implementation methods of the timer I have personally used, but they are only a brief introduction. There are many relevant materials on the Internet for more detailed usage. If there are errors, please leave a message for criticism and correction. I hope it will be helpful to you after reading this article. I also 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
分享
二维码
< <上一篇
下一篇>>