1. instance a new HandlerThread , and start it .
2. instance a new Handler , set a looper from HandlerThread .
3. Two thread can communicate by Message (for exp , press a button to stop the download thread);
package com.android; import android.app.Activity; import android.os.*; public class HandlerTest03 extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); System.out.println("start thread:"+Thread.currentThread().getId()); HandlerThread handlerThread=new HandlerThread("handler_thread"); handlerThread.start(); MyHandler myHandler=new MyHandler(handlerThread.getLooper()); Message msg=myHandler.obtainMessage(); msg.setData(new Bundle()); msg.sendToTarget(); } private class MyHandler extends Handler{ private MyHandler(Looper looper) { // throw new UnsupportedOperationException("Not yet implemented"); super(looper); } @Override public void handleMessage(Message msg){ Bundle b=msg.getData(); System.out.println("handle thread:"+Thread.currentThread().getId()); } }; }
if use handler.post(Runable runable) and handler.removeCallbacks(Runable runable) . It is run in the same main thread :
package com.android; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; import java.util.logging.Level; import java.util.logging.Logger; public class HandlerTest02 extends Activity { private Button startButt; private ProgressBar bar; private int barCount=0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); startButt=(Button)this.findViewById(R.id.startButton); startButt.setOnClickListener(startListener); bar=(ProgressBar)this.findViewById(R.id.bar); } private OnClickListener startListener=new OnClickListener(){ public void onClick(View v) { // throw new UnsupportedOperationException("Not supported yet."); bar.setProgress(barCount); bar.setVisibility(View.VISIBLE); handler.post(barThread); } }; private Handler handler=new Handler(){ @Override public void handleMessage(Message msg){ bar.setProgress(msg.arg1); handler.post(barThread); } }; private Runnable barThread=new Runnable(){ public void run() { // throw new UnsupportedOperationException("Not supported yet."); if(barCount<100){ try { Thread.sleep(1000); } catch (InterruptedException ex) { System.out.println(ex.getMessage()); } barCount+=10; Message msg=handler.obtainMessage(); msg.arg1=barCount; handler.sendMessage(msg); } else{ handler.removeCallbacks(barThread); bar.setVisibility(View.GONE); barCount=0; } } }; }
Message is a weird value container . it has 2 arg(int) for quick store . and also can container a bundle.
Handler can obtain a new Message , so the message can .sendToTarget to send back to the handler.
Message msg=myHandler.obtainMessage(); msg.setData(new Bundle()); msg.sendToTarget();