How to create a Looper thread, then send it a message immediately?

后端 未结 4 1723
离开以前
离开以前 2020-11-30 17:47

I have a worker thread that sits in the background, processing messages. Something like this:

class Worker extends Thread {

    public volatile Handler hand         


        
4条回答
  •  温柔的废话
    2020-11-30 18:03

    take a look at the source code of HandlerThread

    @Override
         public void run() {
             mTid = Process.myTid();
             Looper.prepare();
             synchronized (this) {
                 mLooper = Looper.myLooper();
                 notifyAll();
             }
             Process.setThreadPriority(mPriority);
             onLooperPrepared();
             Looper.loop();
             mTid = -1;
         }
    

    Basically, if you are extending Thread in worker and implementing your own Looper, then your main thread class should extend worker and set your handler there.

提交回复
热议问题