Android Handler Message Summary

Source: Internet
Author: User

http://blog.csdn.net/caesardadi/article/details/8473777

When the application starts, it opens a main thread (the UI thread), which manages the UI, listens for user clicks, responds to users, and distributes events. Therefore, generally in the main thread do not perform more time-consuming operations, such as network download data, or a ANR error. Therefore, these operations are placed in child threads, but because the androidui thread is unsafe, the UI can only be updated in the main thread. Handler is used to communicate between a child thread and a thread that creates a handler.

The use of handler is divided into two parts:

part of creating a handler instance, overloading the Handlemessage method to process the message.

[Java]View PlainCopy
    1. mprogresshandler = new handler ()   
    2.         {  
    3.              Public void handlemessage (message msg)   
    4.             {  
    5.                  super.handlemessage (msg);   
    6.             }  
    7.         };  


Of course, you can also inherit from handler, as well as implement the Handlemessage (Message msg) method.

[Java]View PlainCopy
  1. Class MyHandler extends Handler {
  2. Public MyHandler () {
  3. }
  4. //Subclasses must override this method to accept data
  5. @Override
  6. public void Handlemessage (Message msg) {
  7. //TODO auto-generated method stub
  8. LOG.D ("MyHandler", "Handlemessage ...");
  9. super.handlemessage (msg);
  10. }
  11. }


The other part is distributing the message or Runable object to the thread where handler is located, generally handler in the main threads.

Some ways to distribute messages in handler
Post (Runnable)
Postattime (Runnable,long)
Postdelayed (Runnable Long)
Sendemptymessage (int what)
SendMessage (Message)
Sendmessageattime (Message,long)
Sendmessagedelayed (Message,long)

Handler itself can not only send messages, but also by post to add an anonymous object to implement the Runnable interface to the message queue, the target receives the message can be recalled in its own thread to execute the method body of the run.

Message

[Java]View PlainCopy
    1. Message message = Message.obtain ();
    2. MESSAGE.ARG1 = 1;
    3. MESSAGE.ARG2 = 2;
    4. Message.obj = "Demo";
    5. Message.what = 3;
    6. Bundle Bundledata = new Bundle ();
    7. Bundledata.putstring ("Name", "Lucy");
    8. Message.setdata (Bundledata);



The parameters that a message can pass are:

1. arg1 arg2 integer type is a low-cost alternative to SetData. Passing Simple types

2. Object Type obj

3. What user-defined message code, so that the recipient can understand the message information. Each handler contains its own message code, so there's no need to worry about custom messages that conflict with other handlers.

4. Others can be passed through bundles

The message can be constructed with the new message to create a message, but this is not a good way to use. It is best to use Message.obtain () to get the message instance, which creates a pool of messages to handle.

Public constructors

Public Message ()

Constructor (but the best way to get the message object is to call Message.obtain ()).

The following is a message.obtain way to get a message instance, handler is passed in the parameter, and Handler.sendmessage is no longer used when the message is sent. Use Message.sendtotarget (), but in the final analysis, call Handler.sendmessage to send a message. The handler instance is saved in the message class.

public static Message obtain (Handler H, int. What, int arg1, int arg2, Object obj)

Same as obtain (), but sets the values of target, what, Arg1, arg2, and obj.

Parameters

The target value set by H

What sets the What value

Arg1 value set by Arg1

Arg2 value set by arg2

Obj value set by obj

return value

A message object that is assigned from the global pool.

public static Message obtain (Handler h, int, Object obj)

Same as obtain (), but sets the value of target, what and obj.

Parameters

The target value set by H

What sets the What value

Obj value set by obj

return value

A message object that is assigned from the global pool.

public static Messageobtain (Handler h, int)

Same as obtain (), but sets the value of target and what.

Parameters

The value of the H target

What's the value

return value

A message object that is assigned from the global pool.

public static Message obtain (Handler h)

Same as obtain (), but set the value of target

Parameters

The value of the target member of the H message object

return value

A message object that is assigned from the global pool.

public static Message obtain (Handler H, Runnable callback)

Same as obtain (Handler), but sets the callback function to be called when the message is returned.

Parameters

The value of the target member of the H message object

Callback callback function that is called when message processing is processed

return value

A message object that is assigned from the global pool.

public static Message obtain ()

Returns a new message instance from the global pool. In most cases, this avoids assigning new objects.

public static Message obtain (Handler H, int. What, int arg1, int arg2)

Same as obtain (), but set the values of target, what, Arg1 and arg2

Parameters

H Set the Targe value

What sets the What value

Arg1 value set by Arg1

Arg2 value set by arg2

return value

A message object that is assigned from the global pool.

public static message obtain (message obj)

With obtain (), but copies the value from an existing message (including its target).

Parameters

Orig the source message to be copied

return value

A message object that is assigned from the global pool.

Public Bundle Peekdata ()

Similar to GetData (), but does not delay the creation of bundles. Returns null if the bundle object does not exist. See GetData () for more information.

Reference

Android Handler Message Summary

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.