Why use HandlerThread in Android

前端 未结 6 1614
名媛妹妹
名媛妹妹 2020-12-14 09:43

In android , Handler can be used to post / handle message, if I don\'t use a HandlerThread (pass its Looper to Handler), does that mean in this case Handler use MainThread (

6条回答
  •  眼角桃花
    2020-12-14 10:38

    Normal way to use HandlerThread like this:

    HandlerThread thread = new HandlerThread("A Handler Thread");
    thread.start();
    Handler handler = new Handler(thread.getLooper()){
        @Override
        public void handleMessage(Message msg) 
        {
        //....
        }
    };
    

    Because HandlerThread can create a Looper for Handler, it is a kind of convenient way.

    When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- see official docs...

提交回复
热议问题