Fourth three cornerstones of Android development-activity, service and handler (7)

Source: Internet
Author: User

Detailed analysis of message mechanism in 4.3.2Android

Let's look at the messaging mechanism in Android in detail.

A friend familiar with Windows programming knows that Windows programs are message-driven and have a global messaging system. Google has referenced the Windows message loop mechanism, and also implemented a message loop mechanism in the Android system. Android uses Looper, handler to implement the message loop mechanism. The message loop for Android is thread-specific, and each thread can have its own message queue and message loop.

The Looper in the Android system is responsible for managing thread Message Queuing (MessageQueue) and Message loops (Looper). The Looper object of the current thread is obtained through Looper.mylooper (), and the Looper object of the current process's main thread is obtained through Looper.getmainlooper ().

As mentioned earlier, both the message queue and message loop for Android are specific threads, one thread can have a message queue and a message loop, and a particular thread's message can only be distributed to this thread, and cannot be communicated across threads and across processes. However, the worker threads created by default do not have Message Queuing and message loops, and if you want the worker thread to have Message Queuing and message loops, you will need to call Looper.prepare () to create the message queue in the thread and then call Looper.loop () to enter the message loop. For example:

Import slightly

public class Workthread extends Thread {

Public Handler Mhandler;

public void Run () {

Looper.prepare ();

Mhandler = new Handler () {

public void Handlemessage (Message msg) {

Process the received message

}

};

Looper.loop ();

}

}

--------------------------------------------trying to put an ad, and now there's no work to survive.Ping An Lufax is affiliated to the Ping An group's peer platformannual ROI 7%-9% is the preferred alternative to bank bankingPersonal lessons recommend investing in anxin or guaranteed rainbow projectsdon't invest in an e that's almost impossible to transfer it's very difficult to withdraw in advancesite LinksHttp://affiliate.lufax.com/action/36XBUThe first investment of 1000 yuan to make an additional hundreds of Yuan--------------------------------------------

In this way, the worker threads we create have a message-handling mechanism.

So why do we not see Looper.prepare () and Looper.loop () calls in the previous example? The reason is that our activity is a UI thread that runs in the main thread, and the Android system creates a message queue and message loop for the activity when it starts.

The most mentioned is the message queue and the message loop, but we see that every message processing place has a handler existence, what is it doing? The role of handler is to add messages to a message queue that is managed by a particular looper and to distribute and process messages in that message queue. When constructing handler, you can specify a Looper object that is created with the Looper object of the current thread if not specified.

In one activity, multiple worker threads can be created, and if those threads put their messages in the message queue of the activity's main thread, the message is processed in the main thread. Because the main thread is generally responsible for updating the view component, this is a good way to implement the view update for a view component that is not thread-safe.

So how does a child thread put the message into the message queue of the main thread?

First, we create the handler object in the Looper of the main thread, so when the SendMessage method of handler is called, the system invokes the main thread's message queue and processes the message in the main thread message queue through the Handlemessage method.

Below, let's illustrate with a simple example. In this example, we implemented an auto-counting function:

We create a new countactivity to inherit from the activity, the code is as follows:

Import slightly

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

}

}

Layout file is very simple, there is only one TextView, centered display.

Below, let's look at the effect, shown in 4-9:

Figure 4-9 Message mechanism

After you start running, you can see that every second, the counter automatically adds 1.

Fourth three cornerstones of Android development-activity, service and handler (7)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.