Public class FirActivity extends Activity {static final int LOOPER_QUIT =-1; static final int LOOPER_BACK = 1; public Handler handler_fat = new Handler () // main thread handler, it is used to receive messages from sub-threads, and the main thread has logoff by default; {public void handleMessage (Message msg) {if (msg. what = LOOPER_BACK) // receives the message from the subthread {Log. d ("father", "father-> LOOPER_BACK") ;}}; public Handler handler_son;/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); Button button = (Button) findViewById (R. id. button1); final Thread thread = new Thread (new Runnable () {public void run () {Looper. prepare (); // The Sub-thread does not have a looper by default. You must enable looper; handler_son = new Handler () // The Sub-thread Handler, which is used to receive messages sent by other threads; {public void handleMessage (Message msg) {if (msg. what = LOOPER_QUIT) {Log. d ("thread", "prepare to go to death"); this. getLooper (). quit (); // end the loose loop. The subthread stops preparing because the code has been executed.} else if (msg. what = LOOPER_BACK) {Log. d ("thread", "LOOPER_BACK"); handler_fat.sendEmptyMessage (LOOPER_BACK); // send the message to the main thread;} else {Log. d ("thread", msg. what + "") ;}}}; logoff. loop (); // The logoff function starts to run. The Code Execution starts from here and will not be executed until it is finished. Log. d ("thread", "going to death") ;}}); thread. start (); // The thread starts to start the button. setOnClickListener (new View. onClickListener () {public void onClick (View v) {// TODO Auto-generated method stub Log. d ("main", thread. isAlive () + ""); // returns whether the thread is alive. Here it is true; then (44); handler_son.sendEmptyMessage (LOOPER_BACK); handler_son.sendEmptyMessage (LOOPER_QUIT); Log. d ("main", thread. isAlive () + ""); // here it is false ;}});}}
This article from the "wenjian minor notes" blog, please be sure to keep this http://wenjianboy.blog.51cto.com/7389753/1297205