您的位置:首页 > 产品设计 > UI/UE

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"));
}
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: