Android线程---UI线程和非UI线程之间通信

当主线程sendMessage后,子线程便会调用handleMessage来获取你所发送的Message。我的主线程向子线程发送消息时携带了数据,子线程根据主线程发送来的数据进行数据库查询,并将查询后的结果返回给该主线程:

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//创建一个子线程对象

13UpdataThread updataThread ;

14

15//定义一个全局变量,该Handler在主线程中重写HandleMessage。

16//若不定义成为全局变量,则在子线程中无发用到该Handler

17private Handler mainHandler = null;

18

19//创建一个非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所携带的数据

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//创建并连接数据库,若该数据库已经存在,则打开该数据库

44CreateDatabaseHelpercdh = new CreateDatabaseHelper(getApplication(), "myPeople.db3", 1);

45//使用游标查询数据库,并返回结果集

46Cursor cursor =cdh.getReadableDatabase().rawQuery("select * from people where name = ?and phone = ?", new String[]{name, phone});

47//创建一个Bundle存储查询出来的结果

48Bundle dataAll = newBundle();

49//遍历cursor,并将结果赋值给Bundle

50while(cursor.moveToNext()) {

51dataAll.putString("name", cursor.getString(1));

52dataAll.putString("phone", cursor.getString(2));

53dataAll.putString("address",cursor.getString(3));

54}

55//↓↓↓↓↓↓↓这一块便是子线程将查询的结果返回给主线程↓↓↓↓↓↓↓

56//创建Message

57Message msg_main = newMessage();

58msg_main.what = 0x456;

59//为Message添加数据

60msg_main.setData(dataAll);

61//向主线程发送消息

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所携带的数据包

89Bundle datas = intent.getExtras();

90//取出包中所携带的各种数据

91if (datas != null) {

92name =datas.getString("name");

93phone =datas.getString("phone");

94} else {

95name = "空";

96phone = "空";

97}

98 //↓↓↓↓↓↓↓这一块便是主线程向子线程发送消息↓↓↓↓↓↓↓↓

99//创建消息

100Message msg = new Message();

101//为msg标记一下(类似与--key--)

102msg.what = 0x123;

103//创建一个Bundle,并存放数据

104Bundle bundle = new Bundle();

105bundle.putString("name", name);

106bundle.putString("phone", phone);

107//将数据添加到msg

108msg.setData(bundle);

109//向新线程发送消息

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开发完可以做一个全面的检测:www.ineice.com


你可能感兴趣的:(Android线程---UI线程和非UI线程之间通信)