handler在主线程中定义,子线程中往主线程发消息,消息不空,但是主线程中无法获取到
代码如下:
主线程:
if (conn == null) {
conn = new ConnectServer(msg,LoginActivity.this);
Log.e(TAG, "成功" );
}
conn.start();
public final Handler mHandler = new Handler(){
public void handleMessage(Message msg) {
Log.e("系统消息", "handler传值回来");
String login=(String)msg.obj;
};
};
子线程:
public ConnectServer(String message,LoginActivity login){
sendmsg = message;
//this.handler = handler;
this.login = login;
}
public void run() {
String backmsg = "9999";
Message msg = Message.obtain();
msg.obj = backmsg;
login.mHandler.sendMessage(msg);}
代码如下:
主线程:
if (conn == null) {
conn = new ConnectServer(msg,LoginActivity.this);
Log.e(TAG, "成功" );
}
conn.start();
public final Handler mHandler = new Handler(){
public void handleMessage(Message msg) {
Log.e("系统消息", "handler传值回来");
String login=(String)msg.obj;
};
};
子线程:
public ConnectServer(String message,LoginActivity login){
sendmsg = message;
//this.handler = handler;
this.login = login;
}
public void run() {
String backmsg = "9999";
Message msg = Message.obtain();
msg.obj = backmsg;
login.mHandler.sendMessage(msg);}