Android線程---UI線程和非UI線程之間通訊

來源:互聯網
上載者:User

Android線程---UI線程和非UI線程之間通訊
當主線程sendMessage後,子線程便會調用handleMessage來擷取你所發送的Message。我的主線程向子線程發送訊息時攜帶了資料,子線程根據主線程發送來的資料進行資料庫查詢,並將查詢後的結果返回給該主線程: 

  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     //建立一個子線程對象 13     UpdataThread updataThread ; 14  15     //定義一個全域變數,該Handler在主線程中重寫HandleMessage。 16     //若不定義成為全域變數,則在子線程中無發用到該Handler 17     private Handler mainHandler = null; 18  19     //建立一個非UI線程 20     class UpdataThread extends Thread { 21  22         public Handler mhandler; 23  24         public void run() { 25             Looper.prepare(); 26             mhandler = new Handler() { 27  28                 //定義處理訊息的方法 29                 @Override 30                 public voidhandleMessage(Message msg) { 31                     //---這裡面做一些耗時操作 32                     if (msg.what == 0x123) { 33                         //擷取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(), "傳值成功" +name + phone, Toast.LENGTH_LONG).show(); 39                         } else { 40                             name = ""; 41                             phone = ""; 42                         } 43                         //建立並串連資料庫,若該資料庫已經存在,則開啟該資料庫 44                         CreateDatabaseHelpercdh = new CreateDatabaseHelper(getApplication(), "myPeople.db3", 1); 45                         //使用遊標查詢資料庫,並返回結果集 46                         Cursor cursor =cdh.getReadableDatabase().rawQuery("select * from people where name = ?and phone = ?", new String[]{name, phone}); 47                         //建立一個Bundle儲存查詢出來的結果 48                         Bundle dataAll = newBundle(); 49                         //遍曆cursor,並將結果賦值給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     //↓↓↓↓↓↓↓這一塊便是子線程將查詢的結果返回給主線程↓↓↓↓↓↓↓ 56                         //建立Message 57                         Message msg_main = newMessage(); 58                         msg_main.what = 0x456; 59                         //為Message添加資料 60                        msg_main.setData(dataAll); 61                         //向主線程發送訊息 62                        mainHandler.sendMessage(msg_main); 63  64                     } 65                 } 66             }; 67             Looper.loop(); 68         } 69     } 70  71     @Override 72     protected void onCreate(BundlesavedInstanceState) { 73         super.onCreate(savedInstanceState); 74         //執行個體化Thread 75         updataThread = new UpdataThread(); 76         //啟動新線程 77         updataThread.start(); 78         setContentView(R.layout.updatapeople); 79         //擷取布局檔案裡的控制項 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         //擷取啟動該Activity的Intent 87         Intent intent = getIntent(); 88         //取出Intent所攜帶的資料包 89         Bundle datas = intent.getExtras(); 90         //取出包中所攜帶的各種資料 91         if (datas != null) { 92             name =datas.getString("name"); 93             phone =datas.getString("phone"); 94         } else { 95             name = "空"; 96             phone = "空"; 97         } 98 //↓↓↓↓↓↓↓這一塊便是主線程向子線程發送訊息↓↓↓↓↓↓↓↓ 99         //建立訊息100        Message msg = new Message();101        //為msg標記一下(類似與--key--)102        msg.what = 0x123;103        //建立一個Bundle,並存放資料104        Bundle bundle = new Bundle();105        bundle.putString("name", name);106        bundle.putString("phone", phone);107        //將資料添加到msg108        msg.setData(bundle);109        //向新線程發送訊息110        updataThread.mhandler.sendMessage(msg);111 112        //接受子線程返回的訊息和子線程那邊的用法一樣113         mainHandler = new Handler() {114            @Override115            public void handleMessage(Message msg_main) {116                 if (msg_main.what == 0x456){117                     //更新UI(因為在UI 線程中可以進行UI的更新。。。)118                    updata_name.setText(msg_main.getData().getString("name"));119                 }120            }121        };

 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.