Asynchronous message processing mechanism handler

Source: Internet
Author: User

asynchronous Message Processing in Android consists of four parts, message, Handler, MessageQueue, and Looper.

1. Message

A message is an information that is passed between threads, and it can carry a small number of messages inside to exchange data between different threading. (Field has what arg1 arg2 obj)

2. Handler

The Handler is primarily used to send and process messages. Sending a message is typically using the handler SendMessage () method, and the emitted message is eventually passed to the handler Handlemessage () method after a series of tossing and processing.

3. MessageQueue

MessageQueue is a message queue that is primarily used to store all messages sent through handler. This part of the message will remain in the message queue and wait for it to be processed. There will only be one MessageQueue in each thread

Object.

4. Looper

Looper is the steward of MessageQueue in each thread, and after calling the Looper loop () method, it enters an infinite loop, and then whenever a message is found in MessageQueue, it is taken out and passed to handler In the Handlemessage () method. There will only be one Looper object in each thread.

Code implementation:

 Packagecom.example.androidthreadtest;ImportAndroid.os.AsyncTask;ImportAndroid.os.Bundle;ImportAndroid.os.Handler;ImportAndroid.os.Message;Importandroid.app.Activity;ImportAndroid.app.ProgressDialog;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.TextView; Public classMainactivityextendsActivityImplementsonclicklistener{PrivateTextView text; PrivateButton Changetext; //defines an integer constant Update_text that represents the update TextView action.    Private Final Static intUpdate_text=1; //Handler overrides the Handlemessage method of the parent class, where the specific message is processed    PrivateHandler handler=NewHandler () { Public voidhandlemessage (Message msg) {Switch(msg.what) { CaseUPDATE_TEXT:text.setText ("Nice to Meet your");  Break; default:                 Break;    }        }    }; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Changetext=(Button) Findviewbyid (R.id.change_text); Text=(TextView) Findviewbyid (R.id.text); Changetext.setonclicklistener ( This); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public voidOnClick (View v) {//TODO auto-generated Method Stub        Switch(V.getid ()) { CaseR.id.change_text:NewThread (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method StubMessage message=NewMessage (); Message.what=Update_text; //Send Messagehandler.sendmessage (message);        }}). Start (); }    }    }

Message.what

    • Message is a class
    • MSG is an instance of a class message
    • What is a member variable of instance MSG
    • Msg.what = 0 is a given member variable what assignment

Asynchronous message processing mechanism handler

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.