Using Handler and RunnableThe Handler class can send and process thread-related messages and Runnable objects. The postDelayed method can delay the execution of a Runnable object for a period of time. Using Thread and sleepYou can use the sleep method in a new thread to achieve the delay effect. Be careful not to use this method in the UI thread, otherwise it will cause the interface to freeze. If you need to perform operations in the UI thread, you can use a Handler to send the results back to the main thread. Using Timer and TimerTaskThe Timer class can schedule one-time or recurring tasks to execute after a specified delay. Timer is not designed for concurrency and does not provide thread safety for multiple tasks. Using ScheduledExecutorServiceScheduledExecutorService is an interface in the Java concurrency package that is used to execute commands after a given delay or periodically. Use ObjectAnimator or ValueAnimator (animation related)If you are working with an animation and want to perform some action after the animation ends, you can use the listeners of the Animator class to achieve an effect similar to a delay. Although this method is related to animation, it is also a method for performing an action after a certain amount of time. Using RxJava and Kotlin FlowRxJava provides a timer operator to delay the entire operation. If you use Kotlin and have introduced coroutine support, you can use the delay function to implement delays. In practical applications, you should avoid starting coroutines in GlobalScope and start them in appropriate lifecycle scopes (such as ViewModel's scope). |
>>: Android animation framework, making panning animation more attractive
Last April, Bob Delamar and Jeremy Johnson became ...
[[341436]] This article is based on my personal e...
If you had called me an egg-stealing dragon befor...
Recently, as Hunan Radio and Television Station p...
In literary and artistic works, panda blood is a ...
If a mature brand still has no representative mar...
[[167050]] Beijing time, June 1 morning news, AMD...
This article was first published by Hunzhi Educat...
Editor: Guru...
It is widely circulated both among the public and...
This article was reviewed by Dr. Tao Ning, Associ...
In holiday marketing, we should take into account...
Here’s some fun fact for you: All vertebrates sle...
In 2016, the Internet of Things was written into ...
Tongcheng Travel spent only 9 yuan to buy the rea...