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
- mprogresshandler = new handler ()
- {
- Public void handlemessage (message msg)
- {
- super.handlemessage (msg);
- }
- };
Of course, you can also inherit from handler, as well as implement the Handlemessage (Message msg) method.
[Java]View PlainCopy
- Class MyHandler extends Handler {
- Public MyHandler () {
- }
- //Subclasses must override this method to accept data
- @Override
- public void Handlemessage (Message msg) {
- //TODO auto-generated method stub
- LOG.D ("MyHandler", "Handlemessage ...");
- super.handlemessage (msg);
- }
- }
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
- Message message = Message.obtain ();
- MESSAGE.ARG1 = 1;
- MESSAGE.ARG2 = 2;
- Message.obj = "Demo";
- Message.what = 3;
- Bundle Bundledata = new Bundle ();
- Bundledata.putstring ("Name", "Lucy");
- 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