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

當主線程sendMessage後,子線程便會調用handleMessage來獲取你所發送的Message。個人主線程向子線程發送消息時攜帶了數據,子線程根據主線程發送來的數據進行數據庫查詢,並將查詢後的結果返回給該主線程:數據庫

 

  1  public class UpdataPeople extends Activity {ide

  2  oop

  3     EditText updata_name;佈局

  4     EditText updata_phone;spa

  5     EditText updata_address;線程

  6     Button updata_quxiao;對象

  7     Button updata_baocun;開發

  8  get

  9     String name;it

 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 void handleMessage(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                         CreateDatabaseHelper cdh = 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 = new Bundle();

 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 = new Message();

 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(Bundle savedInstanceState) {

 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         //獲取啓動該ActivityIntent

 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         //將數據添加到msg

108         msg.setData(bundle);

109         //向新線程發送消息

110         updataThread.mhandler.sendMessage(msg);

111 

112         //接受子線程返回的消息和子線程那邊的用法同樣

113          mainHandler = new Handler() {

114             @Override

115             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         };

 

另外建議APP開發完能夠作一個全面的檢測:www.ineice.com

相關文章
相關標籤/搜索