package com.example.zjday18_handler_03;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
/**
* 主线程向子线程发送数据
* @author Administrator
*
*/
public class MainActivity extends Activity {
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new connectThread().start();
}
public void sendMessage(View v){
if(handler!=null){
Message msg =Message.obtain();
msg.obj ="向子线程中发送数据!";
handler.sendMessage(msg);
}
}
class connectThread extends Thread{
@Override
public void run() {
Looper.prepare();
handler =new Handler(){
public void handleMessage(Message msg) {
super.handleMessage(msg);
String info =(String) msg.obj;
Log.i("---", info);
};
};
Looper.loop();
}
}
}