Introduction to the Android Bundle, handler, and message classes

Source: Internet
Author: User

    • Bundles are a carrier that can store basic data types, objects, and so on, which is equivalent to a car that can load a lot of things and then transport it to where it's needed, such as:

Bundle mbundle=New  bundle (); Mbundle.putstring ("name", "Zhaolinit"); Mbundle.putint (" Number ", 123456); Mbundle.putboolean (" flag ",false); // then, put it in the intent object Intent mintent=New  Intent (); Mintent.putextras (mbundle);

    • M essage: A message containing descriptions and arbitrary data objects for sending to handler

Its member variables are as follows:

 Public Final class Implements parcelable {    publicint  , what    ;  Public int arg1;       Public int arg2;      Public Object obj;    ...    }

What is used to identify the message , or to let the recipient know what the message is about. Arg1 and arg2 are used to send values of some integer types. obj is used to transfer values of any type.

    • Handler: The message processor, by overriding the Handler handlemessage() method, processes the different messages received in the method, for example:
Handler mhandler=New  Handler () {        @Override        publicvoid  Handlemessage (Message msg) {            switch  (msg.what) {                testhandler.test:
+ = msg.arg1; LOG.D ("Progressvalue-------------->", progressvalue+ "");
           Break
}
}
}

Elsewhere, a message is sent through the SendMessage () method for the Handlemessage () method to accept

classMyThreadImplementsRunnable { Public voidrun () { while(!Thread.CurrentThread (). isinterrupted ()) {Message Message=NewMessage (); Message.what=testhandler.test; Testhandler. This. Myhandler.sendmessage (message); Try{Thread.Sleep (100); } Catch(interruptedexception e) {thread.currentthread (). interrupt (); }                  }             }        }

The child threads process some time-consuming operations, and then sends the processed results through the SendMessage () method to the UI main thread. Let the handler of the main thread update the results of the UI component.

Introduction to the Android Bundle, handler, and message classes

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.