Update the interface in the child thread of Android development tutorial

Each handler object is associated with the thread that created it, and each handler object can only be associated with one thread. Handler is generally used for two purposes: 1) executing planned tasks. You can re schedule the implementation to execute some tasks and simulate timers. 2) Inter thread communication. When the Android application starts, a main thread will be created, and the main thread will create a message queue to process various messages. When you create a child thread, you can get the handler object created in the parent thread from your child thread, and you can send messages to the message queue of the parent thread through this object. Since Android requires updating the interface in the UI thread, you can update the interface in other threads through this method.

An example of updating the interface in a child thread through runnable

Create handler in oncreate

Create a sub thread. In the run function of the thread, we send a runnable to the message queue of the main thread to update the interface.

Use message to update the interface in the sub thread. The update interface with message is similar to the update interface with runnable, but it needs to be modified in several places. Implement your own handler to process messages

Send message in new thread

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