Android handler class usage

Source: Internet
Author: User

<Linearlayout
Android: Orientation = "horizontal"
Android: layout_width = "fill_parent">
<Textview
Android: Id = "@ + ID/instruction"
Android: layout_width = "fill_parent"
Android: layout_weight = "0.8"
/>
<Textview
Android: Id = "@ + ID/lend"
Android: layout_width = "fill_parent"
Android: shadowradius = "0.3"/>
</Linearlayout>

Handler usage thread
The run function implemented by the runable interface does not start a new thread, but is executed in the current thread.
Runnable r = new runable (){
Public void run (){
// Program Execution Code }}
Handler and the activity using the thread are in the same thread.
Private handler = new handler ();
Thread. currentthread (). GETID ();
Thread. currentthread (). getname ();

This is different from the standard Java implementation thread.
Thread t = new thread (R );
T. Start (); in this way, the thread and the main thread are different ......
Handler. Post (r); the start method of the thread is not called.
Bundle object: using string as the key is a special table map and a data storage tool.

Loop class: provides a loop to take a rest from the queue.
The handlerthread class can process the waiting threads cyclically (Message Queue)
Handlerthread. getlooper
Handler. handlemassge
Handler. obtainmessage
Message. sendtotarget (handler)
 
Message. OBJ; transmits a small amount of data
Message. setdata (bundle)
Bundle B = new bundle ();
B. putint ("Age", 20 );
B. putstring ("name", Joy) O (character _ blank) O Haha ~ Nice
Int age = B. getdata ("name ");

Before using getlooper of handlerthread, you must call handthread of this class. Start ();

Asynchronous Message Processing: handlemessage in the new thread
Modify the mainfest file before execution

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.