Run Handler messages in a background thread

前端 未结 5 1953
失恋的感觉
失恋的感觉 2020-12-04 19:09

I want to run some Runnable in a background thread. I want to use Handler because it\'s convenient for delays. What I mean is

handler.post(runnable, delay);
         


        
5条回答
  •  無奈伤痛
    2020-12-04 19:56

    You can simply do this:

    private Handler mHandler;
    
    private HandlerThread mHandlerThread;
    
    public void startHandlerThread(){
        mHandlerThread = new HandlerThread("HandlerThread");
        mHandlerThread.start();
        mHandler = new Handler(mHandlerThread.getLooper());
    }
    

    Then invoke with:

    mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
              // Your task goes here
            }
        },1000);
    

提交回复
热议问题