Detailed explanation of how to use Android HandlerThread
HandlerThread inherits from Thread and encapsulates Looper internally.
First of all, the main difference between Handler and HandlerThread is: Handler and Activity are in the same thread, HandlerThread and Activity are not in the same thread, but in new threads other than the other (time-consuming operations cannot be performed in Handler).
usage:
import ; import ; import ; import ; import ; import ; public class MainActivity extends Activity { HandlerThread handlerThread = new HandlerThread("test"); Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); ("cur_=" + ()); //post(runnable), just run run() directly. ThreadId in run() is the same as UIThread in run().// handler = new Handler(); //post(runnable), run runnable in handlerThread, which is not UIThread handler = new Handler((), new Callback() { @Override public boolean handleMessage(Message msg) { ("receive =" + ); if ( == 1) { return true;//Not passed to the outer layer anymore } else { return false; //The outer layer handleMessage() continues to execute } } }) { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub (msg); ("receive =" + ); } }; (new Runnable() { @Override public void run() { ("handler_post_cur_codeview">01-23 07:12:31.590: I/(12386): cur_id=1 01-23 07:12:31.590: I/(12386): =1866 01-23 07:12:31.590: I/(12386): handler_post_cur_id=1866 01-23 07:12:31.600: I/(12386): receive =1 01-23 07:12:31.600: I/(12386): receive =2 01-23 07:12:31.600: I/(12386): receive =2
If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!