The Android thread communicates between the UI thread and the non-UI thread---

Source: Internet
Author: User

Recently self-taught to the thread this piece, with a morning's time finally out of the main, sub-threading communication between each other. When the main thread SendMessage, the child thread calls Handlemessage to get the message you sent. My mainline Cheng thread sends the message with the data, the sub-thread Cheng The database query based on the data sent by the mainline, and returns the query result to the main thread:
1   Public classUpdatapeopleextendsActivity {2  3 EditText Updata_name;4 EditText Updata_phone;5 EditText updata_address;6 Button Updata_quxiao;7 Button Updata_baocun;8  9 String name;Ten String phone; One   A     //Create a child thread object - Updatathread Updatathread; -   the     //defines a global variable that overrides handlemessage in the main thread of the handler.  -     //if it is not defined as a global variable, it is not sent to the handler in the child thread -     PrivateHandler MainHandler =NULL; -   +     //create a non-UI thread -     classUpdatathreadextendsThread { +   A          PublicHandler Mhandler; at   -          Public voidrun () { - Looper.prepare (); -Mhandler =NewHandler () { -   -                 //define methods for handling messages in @Override -                  Public voidhandlemessage (Message msg) { to                     //---Do some time-consuming operations here. +                     if(Msg.what = = 0x123) { -                         //get the data that the MSG carries theBundle bundle =Msg.getdata (); *                         if(Bundle! =NULL) { $String name = bundle.getstring ("name");Panax NotoginsengString phone = bundle.getstring ("Phone"); -Toast.maketext (Getapplication (), "value succeeded" + Name +phone, Toast.length_long). Show (); the}Else { +Name = ""; APhone = ""; the                         } +                         //Create and connect to the database, if the database already exists, open the database -Createdatabasehelper CDH =NewCreatedatabasehelper (Getapplication (), "Mypeople.db3", 1); $                         //querying the database with cursors and returning result sets $cursor cursor = Cdh.getreadabledatabase (). Rawquery ("SELECT * from people where name =? and phone =? ",Newstring[]{name, phone}); -                         //Create a bundle to store the results of the query -Bundle Dataall =NewBundle (); the                         //traverse the cursor and assign the result to the bundle -                          while(Cursor.movetonext ()) {WuyiDataall.putstring ("Name", Cursor.getstring (1)); theDataall.putstring ("Phone", cursor.getstring (2)); -Dataall.putstring ("Address", cursor.getstring (3)); Wu                         } -     //↓↓↓↓↓↓↓ This piece is a child thread returns the result of the query to the main thread ↓↓↓↓↓↓↓ About                         //Create a message $Message Msg_main =NewMessage (); -Msg_main.what = 0x456; -                         //add data to a message - Msg_main.setdata (dataall); A                         //send a message to the main thread + mainhandler.sendmessage (msg_main); the   -                     } $                 } the             }; the Looper.loop (); the         } the     } -   in @Override the     protected voidonCreate (Bundle savedinstancestate) { the         Super. OnCreate (savedinstancestate); About         //Instantiate thread theUpdatathread =NewUpdatathread (); the         //start a new thread the Updatathread.start (); + Setcontentview (r.layout.updatapeople); -         //get the controls in the layout file theUpdata_name =(EditText) Findviewbyid (r.id.updata_name);BayiUpdata_phone =(EditText) Findviewbyid (r.id.updata_phone); theUpdata_address =(EditText) Findviewbyid (r.id.updata_address); theUpdata_quxiao =(Button) Findviewbyid (R.id.updata_quxiao); -Updata_baocun =(Button) Findviewbyid (r.id.updata_baocun); -   the         //gets the intent that initiated the activity theIntent Intent =getintent (); the         //Remove the packets that are carried by the intent theBundle datas =Intent.getextras (); -         //remove the various data that is carried in the package the         if(Datas! =NULL) { theName = datas.getstring ("name"); thePhone = datas.getstring ("Phone");94}Else { theName = "Empty"; thePhone = "Empty"; the         }98 //↓↓↓↓↓↓↓ This piece is the main thread Cheng thread sends a message ↓↓↓↓↓↓↓↓ About         //Create a message -Message msg =NewMessage ();101         //mark the MSG (similar to--key--)102Msg.what = 0x123;103         //Create a bundle and store the data104Bundle bundle =NewBundle (); theBundle.putstring ("name", name);106Bundle.putstring ("Phone", phone);107         //Add data to msg108 Msg.setdata (bundle);109         //send a message to a new thread the updataThread.mhandler.sendMessage (msg);111   the         //accept that the message returned by the child thread is the same as the usage of the child thread113MainHandler =NewHandler () { the @Override the              Public voidhandlemessage (Message msg_main) { the                 if(Msg_main.what = = 0x456){117                     //update UI (because UI updates can be made in the UI thread ...) )118Updata_name.settext (Msg_main.getdata (). getString ("name"));119                 } -             }121};

The Android thread communicates between the UI thread and the non-UI thread---

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.