The Android messaging mechanism is another form of "event handling", a mechanism designed to solve the multithreading problems of Android applications. The--android platform does not allow activity-initiated threads to access the interface components in the activity, which causes the newly-started thread to be unable to modify the property values of the interface components, which we need to implement through the hander message mechanism.
1.Handler Introduction
1) Handler effect:
Send a message in a newly started thread
Fetching in the main thread, processing messages
2) control the timing of message delivery and acceptance
By means of callbacks, developers need to rewrite the method of handling messages in the handler class when a newly-started thread sends a message
, the method that processes the message in the handler class is automatically called back.
3) The handler class contains the following methods for sending and processing messages
. void Handlemessage (Message msg): The method that processes the message. This method is typically used to override
. Final Boolean hasmessages (int what): Checks whether Message Queuing contains a message with a value specified by what property
. Final Boolean hasmessages (int what,object Object): Checks whether the message queue contains the What property is the specified value
Message and the object property is a message for the specified objects
. Multiple overloaded Message obtainmessage (): Getting messages
. Sendemptymessage (int): Send an empty message
. Final Boolean sendemptymessagedelayed (int what,long delaymills): Specifies the number of seconds after which an empty message is sent
. Final Boolean sendMessage (Message msg): Send Message now
2. Experience:
1) Send message
a new startup thread, such as:
New Timer (). Schedule (new Task () { publicvoid run () { new Message (); Msg.what=0x1223; Myhandler.sendmessage (msg); } });
In the above MyHandler is the overridden instantiation object:
New Handler () { publicvoid handlemessage (Message msg) { if 0x1223) { // Make sure this message is sent by this program ...}} ;
when a new thread sends a message, the rewritten handlermessage (message msg) is called back, thus reaching a new thread to modify the properties of the activity component.
Android Handler message delivery mechanism