0

I am confused on the function of Looper in the Handler.

I have some code like this:

snippet1:

mThread = new Thread(){
    @Override
    public void run() {
        Log.d(tag, "thread 1 current thread:" + Thread.currentThread());
        //if(null == Looper.myLooper()) {// This is redundant.
            //Looper.prepare();
        //}
        Log.d(tag, "thread 2 current thread:" + Thread.currentThread());
        mHandler = new Handler(Looper.getMainLooper()){
            @Override
            public void handleMessage(Message msg) {
                Log.d(tag, "thread 3 current thread:" + Thread.currentThread());
            }
        };
        mHandler.sendEmptyMessage(0);
        //Looper.loop(); // This is redundant since mHandler has a mainLooper, and loop in main thread.
    }
};

snippet2:

mThread = new Thread(){
    @Override
    public void run() {
        Log.d(tag, "thread 1 current thread:" + Thread.currentThread());
        if(null == Looper.myLooper()) {
            Looper.prepare();
        }
        Log.d(tag, "thread 2 current thread:" + Thread.currentThread());
        mHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                Log.d(tag, "thread 3 current thread:" + Thread.currentThread());
            }
        };
        mHandler.sendEmptyMessage(0);
        Looper.loop();
    }
};

With different Looper passed to the Handler, I got different output from handleMessage, the snippet1 log "thread 3" output main thread, the snippet2 log "thread 3" output child thread, so I am confused on the function of Looper passed to Handler, can it switch the thread where to handle the method handlerMessage. I haven't found any clue in the Handler source.

Anyone can help me out, thanks in advance.

twlkyao
  • 14,302
  • 7
  • 27
  • 44
  • This is the best blog on this http://techtej.blogspot.in/2011/02/android-passing-data-between-main.html – Ezio Jan 31 '18 at 10:54
  • are you asking what a looper is? – Tim Jan 31 '18 at 10:54
  • @pskink The code used the passed looper and looper's messagequeue, but how it swith the thread? – twlkyao Jan 31 '18 at 10:55
  • @TimCastelijns No, I want to know how it switch the thread. – twlkyao Jan 31 '18 at 10:55
  • @pskink I just want to know how it switch the thread, but I sure will not use handler and looper like the snippet above. – twlkyao Jan 31 '18 at 11:01
  • @pskink because the output of Log.d(tag, "thread 3 current thread:" + Thread.currentThread()); is different by snippet1 and snippet2 – twlkyao Jan 31 '18 at 11:04
  • "I want to know how it switch the thread" - so you don't know what a looper is? – Tim Jan 31 '18 at 11:04
  • @TimCastelijns All I know is that looper is a loop that get message from messagequeue and dispatch to handler. – twlkyao Jan 31 '18 at 11:06
  • @pskink Yes, I know this, maybe my express is not clear, what I am confused is that, the `new Handler()` use the current `Thread`'s `Looper`, and the messeage is send to the `Looper`'s messagequeue, and the handler is attached to the thread, but the hanldeMessage is running on UI thread, I will check the `HandlerThread`. – twlkyao Jan 31 '18 at 11:21
  • @pskink My fault, the `Looper.prepare()` and `Looper.loop()` is redundant for snippet1 – twlkyao Feb 01 '18 at 03:11

0 Answers0