Android线程---UI线程和非UI线程之间通信
2015-08-31 16:13
253 查看
近期自学到了线程这一块,用了一上午的时间终于搞出来了主、子线程间的相互通信。当主线程sendMessage后,子线程便会调用handleMessage来获取你所发送的Message。我的主线程向子线程发送消息时携带了数据,子线程根据主线程发送来的数据进行数据库查询,并将查询后的结果返回给该主线程:
public class UpdataPeople extends Activity { EditText updata_name; EditText updata_phone; EditText updata_address; Button updata_quxiao; Button updata_baocun; String name; String phone; //创建一个子线程对象 UpdataThread updataThread ; //定义一个全局变量,该Handler在主线程中重写HandleMessage。 //若不定义成为全局变量,则在子线程中无发用到该Handler private Handler mainHandler = null; //创建一个非UI线程 class UpdataThread extends Thread { public Handler mhandler; public void run() { Looper.prepare(); mhandler = new Handler() { //定义处理消息的方法 @Override public void handleMessage(Message msg) { //---这里面做一些耗时操作 if (msg.what == 0x123) { //获取msg所携带的数据 Bundle bundle = msg.getData(); if (bundle != null) { String name = bundle.getString("name"); String phone = bundle.getString("phone"); Toast.makeText(getApplication(), "传值成功" + name + phone, Toast.LENGTH_LONG).show(); } else { name = " "; phone = " "; } //创建并连接数据库,若该数据库已经存在,则打开该数据库 CreateDatabaseHelper cdh = new CreateDatabaseHelper(getApplication(), "myPeople.db3", 1); //使用游标查询数据库,并返回结果集 Cursor cursor = cdh.getReadableDatabase().rawQuery("select * from people where name = ? and phone = ?", new String[]{name, phone}); //创建一个Bundle存储查询出来的结果 Bundle dataAll = new Bundle(); //遍历cursor,并将结果赋值给Bundle while (cursor.moveToNext()) { dataAll.putString("name", cursor.getString(1)); dataAll.putString("phone", cursor.getString(2)); dataAll.putString("address", cursor.getString(3)); } //↓↓↓↓↓↓↓这一块便是子线程将查询的结果返回给主线程↓↓↓↓↓↓↓ //创建Message Message msg_main = new Message(); msg_main.what = 0x456; //为Message添加数据 msg_main.setData(dataAll); //向主线程发送消息 mainHandler.sendMessage(msg_main); } } }; Looper.loop(); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //实例化Thread updataThread = new UpdataThread(); //启动新线程 updataThread.start(); setContentView(R.layout.updatapeople); //获取布局文件里的控件 updata_name = (EditText) findViewById(R.id.updata_name); updata_phone = (EditText) findViewById(R.id.updata_phone); updata_address = (EditText) findViewById(R.id.updata_address); updata_quxiao = (Button) findViewById(R.id.updata_quxiao); updata_baocun = (Button) findViewById(R.id.updata_baocun); //获取启动该Activity的Intent Intent intent = getIntent(); //取出Intent所携带的数据包 Bundle datas = intent.getExtras(); //取出包中所携带的各种数据 if (datas != null) { name = datas.getString("name"); phone = datas.getString("phone"); } else { name = "空"; phone = "空"; } //↓↓↓↓↓↓↓这一块便是主线程向子线程发送消息↓↓↓↓↓↓↓↓ //创建消息 Message msg = new Message(); //为msg标记一下(类似与--key--) msg.what = 0x123; //创建一个Bundle,并存放数据 Bundle bundle = new Bundle(); bundle.putString("name", name); bundle.putString("phone", phone); //将数据添加到msg msg.setData(bundle); //向新线程发送消息 updataThread.mhandler.sendMessage(msg); //接受子线程返回的消息和子线程那边的用法一样 mainHandler = new Handler() { @Override public void handleMessage(Message msg_main) { if (msg_main.what == 0x456){ //更新UI(因为在UI 线程中可以进行UI的更新。。。) updata_name.setText(msg_main.getData().getString("name")); } } };
相关文章推荐
- UIEvent - 1
- 77 NSMutableURLRequest常用设置 GET和POST比较 发送JSON给服务器
- 阿里DruidDataSource配置(druid)
- 疯狂ios讲义之选择器(UIPickerView)
- 一个实用的android框架(二)—— UI
- iOS_UI_5_user-defined(自定义)View
- UINavigationController官方文档学习笔记
- poj 1679 The Unique MST
- DataGridView Index -1 does not have a value 错误
- SoapUI 引用第三方jar包和引用Groovy脚本
- break 退出循环 continue 提前进入下一轮循环 while的特殊用法
- UINavigationController详解
- Xamarin iOS UIAlertView
- Ehcache配置时出错- Another unnamed CacheManager already exists in the same VM. Please provide unique name
- ExtJs 集成UEditor and KindEditor
- 解决报错:A potentially dangerous Request.Form value was detected from the client
- Enable Debug log for Requests
- How to save cookie in file for Requests
- 2015第18本:从0到1,ZERO to ONE, Notes on startups, or how to build the future
- Android组件及UI框架大全