handler的写法:
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 0:
break;
case 1:
String json = (String) msg.obj;
content.setText(json);
break;
}
}
};
message的写法:
class MyThread extends Thread {
@Override
public void run() {
isDownloading = true;
Log.d(TAG,"MyThread start run");
//发送消息给mHander
mHandler.sendEmptyMessage(MSG_DOWN_START);
try { //让线程睡眠3s。
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message msg = new Message();
msg.what = MSG_DOWN_SUCCESS;
//msg.arg1 = 111; 可以设置arg1、arg2、obj等参数,传递这些数据
//msg.arg2 = 222; msg.obj = obj;
mHandler.sendMessage(msg);
isDownloading = false;
Log.d(TAG,"MyThread stop run");
}
}
这就是简单的演示过程。