Chapter 4 three cornerstones of Android development: Activity, Service, Handler (7), and androidhandler
4.3.2detailed Analysis of message mechanism in Android
Next, we will analyze the message mechanism in Android in detail.
Friends familiar with Windows Programming know that Windows programs are message-driven and have a global message loop system. Google has referenced the message Loop Mechanism in Windows and implemented the message Loop Mechanism in Android. Android uses logoff and Handler to implement the message loop mechanism. The message loop of Android is for threads. Each thread can have its own message queue and message loop.
Logoff In the Android system is responsible for managing the MessageQueue and logoff of threads ). Obtain the logoff object of the current thread through logoff. mylogoff (), and obtain the logoff object of the main thread of the current process through loaning. getmainlogoff.
As mentioned above, the message queues and message loops of Android are for specific threads. A thread can have a message queue and a message loop. messages of a specific thread can only be distributed to this thread, cross-thread and cross-process communication is not allowed. However, the created worker thread does not have a message queue or a message loop by default. To enable a worker thread to have a message queue or a message loop, you must first call logoff in the thread. prepare () to create a message queue, and then call logoff. loop () enters the message loop. For example:
// Import omitted Public class WorkThread extends Thread { Public Handler mHandler; Public void run (){ Logoff. prepare (); MHandler = new Handler (){ Public void handleMessage (Message msg ){ // Process received messages } }; Logoff. loop (); } } |
------------------------------------------ Try to put an advertisement. Now there is no job to survive. There is no way. The annual ROI of the p2p platform affiliated to Ping An group is 7%-9%. This is the preferred personal experience to replace bank financial management. the Rainbow Project should not invest in security. That is almost impossible to transfer. If you want to withdraw cash in advance, it is very difficult to link to http://affiliate.lufax.com/action/36xbu's first investment of Yuan, that is, a few hundred yuan in the quota --------------------------------------------
In this way, the created worker thread has a message processing mechanism.
So, why did we not see the call of lorule. prepare () and lorule. loop () in the previous example? The reason is that our Activity is a UI thread. When running in the main thread, the Android system creates a message queue and a message loop for the Activity at startup.
The most mentioned above are message queues and message loops. But we can see that Handler exists in each message processing place. What does Handler do? Handler is used to add a message to the Message Queue managed by a specific logoff, and distribute and process the messages in the message queue. When constructing a Handler, you can specify a logoff object. If this parameter is not specified, The logoff object of the current thread is used to create a logoff object.
Multiple worker threads can be created in an Activity. If these threads put their messages into the message queue of the Activity main thread, the messages will be processed in the main thread. The main thread is generally responsible for updating view components. For view components that are not thread-safe, this method can effectively Update views.
So how does the subthread put messages into the message queue of the main thread?
First, we create a Handler object in the Logoff of the main thread. When the sendMessage method of the Handler is called, the system will call the message queue of the main thread, the handleMessage method is used to process messages in the main thread message queue.
The following is a simple example. In this example, we implement an automatic counting function:
Create a CountActivity that inherits from the Activity. The Code is as follows:
// Import omitted Public class CountActivity extends Activity { Private TextView myText; Private static final int START = 1; Private int count = 0; Private Handler handler = new Handler (){ @ Override Public void handleMessage (Message msg ){ If (msg. what = START ){ MyText. setText (String. valueOf (count )); Count ++; Handler. sendMessageDelayed (handler. obtainMessage (START), 1000 ); } } }; @ Override Public void onCreate (Bundle savedInstanceState ){ Super. onCreate (savedInstanceState ); SetContentView (R. layout. count ); MyText = (TextView) findViewById (R. id. count ); Handler. sendMessage (handler. obtainMessage (START )); } } |
The layout file is very simple, with only one TextView displayed in the center.
Next, let's take a look at the effect, as shown in 4-9:
Figure 4-9 message mechanism
After running the task, you can see that the counter is automatically added to 1 every second.