search

Home  >  Q&A  >  body text

android - Handler postDelayed 无效。

        mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mHandler.removeCallbacksAndMessages(null);
                    }
                }, 500);
                
                
                
                 mHandler.removeCallbacksAndMessages(null); 用来移除所有消息,
                 
                 但是用handler.postdelayed 无效,
                 是用TimerTask 或者 Thread  来控制时 是可以生效的。
                 
                请问因为什么原因导致无效的呢?
                
              
迷茫迷茫2771 days ago1363

reply all(3)I'll reply

  • ringa_lee

    ringa_lee2017-04-17 17:37:39

    What is the function of removeCallbacksAndMessages(null)?

    If the parameter is null, all Callbacks and Messages will be cleared.
    You are looking at the source code. The constructor of Message is empty and what is 0. It has not been executed yet and will be deleted.

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 17:37:39

    Excuse me, why is it invalid? Is mHandler.removeCallbacksAndMessages(null); not executed?

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 17:37:39

    It feels a bit strange to remove my own runnable after a delay of 500.
    You can try this

    
    mHandler = new Handler(){
       handleMessage(Message msg){
        switch(msg.what){
        case REMOVE:
           removeCallbacksAndMessages(null);
        }
       }
    }
    ....
    mHandler.sendEmptyMessageDelayed(REMOVE, 500);
    

    reply
    0
  • Cancelreply