Example analysis of message mechanism in Android programming

This paper describes the message mechanism of Android programming. Share with you for your reference, as follows:

1、 Role description

1. Looper: a thread can generate a looper object to manage the message queue in this thread.

2. Handler: you can construct a handler object to communicate with looper to push new messages into the message queue; Or receive the message sent by the looper (get it from the message queue).

3. Message queue: used to store messages put in by threads.

4. Thread: UI thread is usually main thread, and Android will create a message queue for it when it starts the program.

Each thread can contain a looper object and a messagequeue data structure. In your application, you can define a subclass of handler to receive messages sent by looper. In your Android program, when a new thread is born or executed, its message loop will not be automatically established.

There is no global message queue data structure in Android. For example, objects in different apks cannot exchange messages through the mass queue.

For example, the handler object of thread a can deliver messages to other threads, so that other threads B or C can send messages to thread a (stored in a's message queue).

Messages in the message queue of thread a can only be processed by the object to which thread a belongs.

Use looper.myloop to get the looper object of the current thread.

Use mhandler = new eevnthandler (looper. Myloop()); The handler object that can be used to construct the current thread; Where eevnthandler is a subcategory of self implemented handler.

Use mhandler = new eevnthandler (looper. Getmainlooper()); The handler object that can be used to handle the main thread; Where eevnthandler is a subcategory of self implemented handler. This description may be too abstract. Here are some practical examples to illustrate:

2、 Examples

1. Message passing between different components in the same thread

The looper class is used to manage message exchange between objects in a specific thread. Your application can generate many threads. A thread can have many components, and these components often need to exchange messages with each other. If necessary, you can construct a looper object for the thread to manage the message exchange. The looper object will establish a messagequeue data structure to store messages (including UI events or system events) from each object.

Each thread can contain a looper object and a messagequeue data structure. In your application, you can define a subclass of handler to receive messages sent by looper.

Message passing between different components of the same thread:

explain:

When this program is started, the current thread (i.e. main thread) has given birth to a looper object and has a messagequeue data structure.

At this time, when the looper object sees the message M in the messagequeue, it broadcasts it. When the mhandler object receives this message, it will call its handlemessage() function to process it, so it outputs "this my message!" on the screen. Role overview (review):

(1) UI thread is usually main thread, and Android will create a messagequeue for it when it starts the program. (2) Of course, you need a looper object to manage the messagequeue. (3) We can construct a handler object to push new messages into the message queue; Or receive the message sent by the looper (get it from the message queue). (4) The handler object of thread a can be passed to other threads so that other threads B or C can send messages to thread a (stored in a's message queue). (5) The messages in the message queue of thread a can only be processed by the object to which thread a belongs. The child thread passes messages to the main thread

explain:

Android will automatically create a message queue for the main thread. Message queue is not created in this sub thread. Therefore, the myloop value is null, and the mainloop points to the looper in the main thread. Then, execute to:

I hope this article will help you in Android programming.

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