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