當(dāng)主線程sendMessage后,子線程便會調(diào)用handleMessage來獲取你所發(fā)送的Message。我的主線程向子線程發(fā)送消息時攜帶了數(shù)據(jù),子線程根據(jù)主線程發(fā)送來的數(shù)據(jù)進行數(shù)據(jù)庫查詢,并將查詢后的結(jié)果返回給該主線程:
1public class UpdataPeople extends Activity {
2
3EditText updata_name;
4EditText updata_phone;
5EditText updata_address;
6Button updata_quxiao;
7Button updata_baocun;
8
9String name;
10String phone;
11
12//創(chuàng)建一個子線程對象
13UpdataThread updataThread ;
14
15//定義一個全局變量,該Handler在主線程中重寫HandleMessage酒繁。
16//若不定義成為全局變量,則在子線程中無發(fā)用到該Handler
17private Handler mainHandler = null;
18
19//創(chuàng)建一個非UI線程
20class UpdataThread extends Thread {
21
22public Handler mhandler;
23
24public void run() {
25Looper.prepare();
26mhandler = new Handler() {
27
28//定義處理消息的方法
29@Override
30public voidhandleMessage(Message msg) {
31//---這里面做一些耗時操作
32if (msg.what == 0x123) {
33//獲取msg所攜帶的數(shù)據(jù)
34Bundle bundle =msg.getData();
35if (bundle != null) {
36String name =bundle.getString("name");
37String phone =bundle.getString("phone");
38Toast.makeText(getApplication(), "傳值成功" +name + phone, Toast.LENGTH_LONG).show();
39} else {
40name = "";
41phone = "";
42}
43//創(chuàng)建并連接數(shù)據(jù)庫控妻,若該數(shù)據(jù)庫已經(jīng)存在州袒,則打開該數(shù)據(jù)庫
44CreateDatabaseHelpercdh = new CreateDatabaseHelper(getApplication(), "myPeople.db3", 1);
45//使用游標(biāo)查詢數(shù)據(jù)庫,并返回結(jié)果集
46Cursor cursor =cdh.getReadableDatabase().rawQuery("select * from people where name = ?and phone = ?", new String[]{name, phone});
47//創(chuàng)建一個Bundle存儲查詢出來的結(jié)果
48Bundle dataAll = newBundle();
49//遍歷cursor饼暑,并將結(jié)果賦值給Bundle
50while(cursor.moveToNext()) {
51dataAll.putString("name", cursor.getString(1));
52dataAll.putString("phone", cursor.getString(2));
53dataAll.putString("address",cursor.getString(3));
54}
55//↓↓↓↓↓↓↓這一塊便是子線程將查詢的結(jié)果返回給主線程↓↓↓↓↓↓↓
56//創(chuàng)建Message
57Message msg_main = newMessage();
58msg_main.what = 0x456;
59//為Message添加數(shù)據(jù)
60msg_main.setData(dataAll);
61//向主線程發(fā)送消息
62mainHandler.sendMessage(msg_main);
63
64}
65}
66};
67Looper.loop();
68}
69}
70
71@Override
72protected void onCreate(BundlesavedInstanceState) {
73super.onCreate(savedInstanceState);
74//實例化Thread
75updataThread = new UpdataThread();
76//啟動新線程
77updataThread.start();
78setContentView(R.layout.updatapeople);
79//獲取布局文件里的控件
80updata_name = (EditText)findViewById(R.id.updata_name);
81updata_phone = (EditText)findViewById(R.id.updata_phone);
82updata_address = (EditText)findViewById(R.id.updata_address);
83updata_quxiao = (Button)findViewById(R.id.updata_quxiao);
84updata_baocun = (Button)findViewById(R.id.updata_baocun);
85
86//獲取啟動該Activity的Intent
87Intent intent = getIntent();
88//取出Intent所攜帶的數(shù)據(jù)包
89Bundle datas = intent.getExtras();
90//取出包中所攜帶的各種數(shù)據(jù)
91if (datas != null) {
92name =datas.getString("name");
93phone =datas.getString("phone");
94} else {
95name = "空";
96phone = "空";
97}
98 //↓↓↓↓↓↓↓這一塊便是主線程向子線程發(fā)送消息↓↓↓↓↓↓↓↓
99//創(chuàng)建消息
100Message msg = new Message();
101//為msg標(biāo)記一下(類似與--key--)
102msg.what = 0x123;
103//創(chuàng)建一個Bundle,并存放數(shù)據(jù)
104Bundle bundle = new Bundle();
105bundle.putString("name", name);
106bundle.putString("phone", phone);
107//將數(shù)據(jù)添加到msg
108msg.setData(bundle);
109//向新線程發(fā)送消息
110updataThread.mhandler.sendMessage(msg);
111
112//接受子線程返回的消息和子線程那邊的用法一樣
113mainHandler = new Handler() {
114@Override
115public void handleMessage(Message msg_main) {
116if (msg_main.what == 0x456){
117//更新UI(因為在UI線程中可以進行UI的更新稳析。。弓叛。)
118updata_name.setText(msg_main.getData().getString("name"));
119}
120}
121};
另外建議APP開發(fā)完可以做一個全面的檢測:www.ineice.com