zoukankan      html  css  js  c++  java
  • Android线程---UI线程和非UI线程之间通信

        近期自学到了线程这一块,用了一上午的时间终于搞出来了主、子线程间的相互通信。当主线程sendMessage后,子线程便会调用handleMessage来获取你所发送的Message。我的主线程向子线程发送消息时携带了数据,子线程根据主线程发送来的数据进行数据库查询,并将查询后的结果返回给该主线程:
     
      1  public class UpdataPeople extends Activity {
      2  
      3     EditText updata_name;
      4     EditText updata_phone;
      5     EditText updata_address;
      6     Button updata_quxiao;
      7     Button updata_baocun;
      8  
      9     String name;
     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         //获取启动该Activity的Intent
     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         };
  • 相关阅读:
    CGO入门和OCR文字识别(非第三方API,有源码,效果好)实战
    Golang中如何正确的使用sarama包操作Kafka?
    音量强度转分贝db
    ShowDialog()弹出的窗体,关闭后,主窗体会闪烁的BUG
    小鱼提问3 static方法中可以访问某个类的私有变量吗(不通过反射的其他非正常手段)?什么情况下可以?
    “-="的陷阱
    c++ 从一个BYTE[] *filePtr 追加二进制文件
    Android解决程序切换后台被干掉,恢复状态问题
    Cookie
    dede后台搜索标签
  • 原文地址:https://www.cnblogs.com/819158327fan/p/4773322.html
Copyright © 2011-2022 走看看