private Handler mHandler_2;
/**
* 寒天星之线程基础篇 */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //实例化线程,在handler中规定什么情况触发什么 MyThread_2 thread_2 = new MyThread_2(); thread_2.start();//这里的newMsg就像一个发消息的入口将情况发给handler让handler进行处理
Message newMsg = Message.obtain();
newMsg.what = 1; mHandler_2.sendMessage(newMsg); } public class MyThread_2 extends Thread {@Override
public void run() { Looper.prepare();mHandler_2 = new Handler() {
public void handleMessage(Message msg) { switch (msg.what) { case 1: Toast.makeText(MainActivity.this, "出来了", 1).show(); break; case 2:break;
default:
break; } } };Looper.loop();
} }