Android thread --- communication between the UI thread and the non-UI thread, android --- ui

Source: Internet
Author: User

Android thread --- communication between the UI thread and the non-UI thread, android --- ui
Recently, I learned about the thread and used a morning to finally develop the communication between the main and sub-threads. After the main thread sendMessage, The subthread calls handleMessage to obtain the Message you sent. My main thread carries data when sending messages to the subthread. The subthread queries the database based on the data sent by the main thread and returns the query results to the main thread:

1 public class UpdataPeople extends Activity {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; 10 String phone; 11 12 // create a subthread object 13 UpdataThread updataThread; 14 15 // define a global variable. The Handler overwrites HandleMessage in the main thread. 16 // if it is not defined as a global variable, the Handler 17 private Handler mainHandler = null is not used in the child thread; 18 19 // create a non-UI Thread 20 class UpdataThread extends Thread {21 22 public Handler mhandler; 23 24 public void run () {25 Looper. prepare (); 26 mhandler = new Handler () {27 28 // defines the Message Processing Method 29 @ Override 30 public void handleMessage (Message msg) {31 // --- some time-consuming operations 32 if (msg. what = 0x123) {33 // obtain the data carried by msg 34 Bundle bundle = msg. GetData (); 35 if (bundle! = Null) {36 String name = bundle. getString ("name"); 37 String phone = bundle. getString ("phone"); 38 Toast. makeText (getApplication (), "successfully passed the value" + name + phone, Toast. LENGTH_LONG ). show (); 39} else {40 name = ""; 41 phone = ""; 42} 43 // create and connect to the database. If the database already exists, then open the database 44 CreateDatabaseHelper cdh = new CreateDatabaseHelper (getApplication (), "myPeople. db3 ", 1); 45 // use a Cursor to query the database and return the result set 46 cursor Cursor = cdh. GetReadableDatabase (). rawQuery ("select * from people where name =? And phone =? ", New String [] {name, phone}); 47 // create a Bundle storage query result 48 Bundle dataAll = new Bundle (); 49 // traverse cursor, and assign the result to Bundle 50 while (cursor. moveToNext () {51 dataAll. putString ("name", cursor. getString (1); 52 dataAll. putString ("phone", cursor. getString (2); 53 dataAll. putString ("address", cursor. getString (3 )); 54} 55 // The subthread returns the query result to the main thread when there are too many concurrent threads running 56 // creates Message 57 Message msg_main = new Message (); 58 msg_main.what = 0x456; 59 // Add data for Message 60 msg_main.setData (dataAll); 61 // send Message 62 mainHandler to main thread. sendMessage (msg_main); 63 64} 65} 66}; 67 logoff. loop (); 68} 69} 70 71 @ Override 72 protected void onCreate (Bundle savedInstanceState) {73 super. onCreate (savedInstanceState); 74 // instantiate Thread 75 updataThread = new UpdataThread (); 76 // start the new Thread 77 updataThread. start (); 78 setContentView (R. lay Out. updatapeople); 79 // get the control 80 updata_name = (EditText) findViewById (R. id. updata_name); 81 updata_phone = (EditText) findViewById (R. id. updata_phone); 82 updata_address = (EditText) findViewById (R. id. updata_address); 83 updata_quxiao = (Button) findViewById (R. id. updata_quxiao); 84 updata_baocun = (Button) findViewById (R. id. updata_baocun); 85 86 // get the Intent 87 Intent intent = getIn that starts the Activity Tent (); 88 // retrieve the data packet 89 Bundle datas = Intent. getExtras (); 90 // retrieve all data contained in the packet 91 if (datas! = Null) {92 name = datas. getString ("name"); 93 phone = datas. getString ("phone"); 94} else {95 name = "null"; 96 phone = "null "; 97} 98 // The worker 99 // create a Message 100 Message msg = new message (); 101 // mark (similar to -- key --) 102 msg for msg. what = 0x123; 103 // create a Bundle and store data 104 Bundle bundle = new Bundle (); 105 bundle. putString ("name", name); 106 bundle. putString ("phone", phone); 107 // Add data to msg108 Msg. setData (bundle); 109 // send the message 110 updataThread to the new thread. mhandler. sendMessage (msg); 111 112 // The Message returned by the subthread is used the same as that returned by the subthread. 113 mainHandler = new Handler () {114 @ Override115 public void handleMessage (Message msg_main) {116 if (msg_main.what = 0x456) {117 // update the UI (because UI updates can be performed in the UI thread ...) 118 updata_name.setText (msg_main.getData (). getString ("name"); 119} 120} 121 };

 

Related Article

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.