停止 handler.postDelayed()

Posted

技术标签:

【中文标题】停止 handler.postDelayed()【英文标题】:Stop handler.postDelayed() 【发布时间】:2014-05-08 06:37:38 【问题描述】:

我通过 new Handler().postDelayed(new Runnable() 调用多个 Handlers..... 单击返回时如何停止它?

public class MyActivity extends AppCompatActivity implements OnClickListener 

    private Button btn;
    private Handler handler;
    private Runnable myRunnable;

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState); 
        btn = (Button) findViewById(R.id.trainingsstart);
        btn.setOnClickListener(this);   
    

    @Override
    public void onClick(View v) 
        Handler handler =  new Handler();
        Runnable myRunnable = new Runnable() 
            public void run() 
                // Things to be done
            
        ;

        handler.postDelayed(myRunnable, 3000);
    

    @Override 
    public void onBackPressed()  
        super.onBackPressed();
        handler.removeCallbacks(myRunnable);
       

我的问题是我的处理程序没有像“myHandler”这样的名称。我该如何处理?

感谢您的帮助!

现在我得到了这些错误:

03-28 17:47:03.662: E/androidRuntime(1961): FATAL EXCEPTION: main
03-28 17:47:03.662: E/AndroidRuntime(1961): java.lang.NullPointerException
03-28 17:47:03.662: E/AndroidRuntime(1961):     at de.basti12354.tage.uebungen.Tag1.onBackPressed(Tag1.java:120)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.app.Activity.onKeyUp(Activity.java:2159)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.KeyEvent.dispatch(KeyEvent.java:2647)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.app.Activity.dispatchKeyEvent(Activity.java:2389)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1860)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3791)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3774)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3516)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3666)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:1982)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1698)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1689)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1959)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.os.MessageQueue.nativePollOnce(Native Method)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.os.MessageQueue.next(MessageQueue.java:132)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.os.Looper.loop(Looper.java:124)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at android.app.ActivityThread.main(ActivityThread.java:5103)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at java.lang.reflect.Method.invokeNative(Native Method)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at java.lang.reflect.Method.invoke(Method.java:525)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-28 17:47:03.662: E/AndroidRuntime(1961):     at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

【参考方案1】:

说明:

必须在“onCreate”之前声明特定的处理程序。如果它在“onCreate”中声明,则它们不共享相同的声明,因此无法识别。这就是作者面临的问题。他不小心在“onCreate”中声明了特定的处理程序。

解决方案:

public class MyActivity extends AppCompatActivity implements OnClickListener 

    private Button btn;
    private Handler handler;
    private Runnable myRunnable;

    @Override
    public void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState); 
        btn = (Button) findViewById(R.id.trainingsstart);
        btn.setOnClickListener(this);   
    

    @Override
    public void onClick(View v) 
        handler =  new Handler();
        myRunnable = new Runnable() 
            public void run() 
                // Things to be done
            
        ;

        handler.postDelayed(myRunnable, 3000);
    

    @Override 
    public void onBackPressed()  
        super.onBackPressed();
        handler.removeMessages(0);
       

【讨论】:

【参考方案2】:

你可以使用:

 Handler handler = new Handler()
 handler.postDelayed(new Runnable())

或者你可以使用:

 handler.removeCallbacksAndMessages(null);

文档

public final void removeCallbacksAndMessages(对象令牌)

在 API 级别 1 中添加 删除所有待处理的回调帖子并发送 obj 为令牌的消息。如果 token 为 null,则所有回调和 消息将被删除。

或者你也可以这样做:

Handler handler =  new Handler()
Runnable myRunnable = new Runnable() 
public void run() 
    // do something

;
handler.postDelayed(myRunnable,zeit_dauer2);

然后:

handler.removeCallbacks(myRunnable);

文档

public final void removeCallbacks (Runnable r)

在 API 级别 1 中添加删除 Runnable r 的所有待处理帖子 在消息队列中。

public final void removeCallbacks (Runnable r, Object token)

编辑:

改变这个:

@Override
public void onClick(View v) 
    Handler handler =  new Handler();
    Runnable myRunnable = new Runnable() 

收件人:

@Override
public void onClick(View v) 
    handler = new Handler();
    myRunnable = new Runnable()  /* ... */

因为你有以下。在 onCreate 之前声明,但您在 onClick 中重新声明并初始化它,导致 NPE。

Handler handler; // declared before onCreate
Runnable myRunnable;

【讨论】:

已添加 handler.removeCallbacks(myRunnable);到我的 onBackPressed() 对吗? @user3433232 使用它你想停止运行 你能看看我的代码吗,我尝试使用你的帮助´,但现在我得到了错误,你可以在我的帖子中看到。 @user3433232 检查编辑。您的处理程序为空。您在导致 NPE 的 onClick 中重新声明了它 我正在使用 API 16 并且 handler.removeCallbacks(runnable) 不起作用。但是 handler.removeCallbacksAndMessages(null);工作谢谢【参考方案3】:

这可能很旧,但对于那些正在寻找答案的人,您可以使用它...

public void stopHandler() 
   handler.removeMessages(0);

干杯

【讨论】:

【参考方案4】:
  Boolean condition=false;  //Instance variable declaration.

 //-----------------Inside oncreate--------------------------------------------------- 
  start =(Button)findViewById(R.id.id_start);
        start.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 

                starthandler();

                if(condition=true)
                
                    condition=false;
                


            
        );

        stop=(Button) findViewById(R.id.id_stoplocatingsmartplug);

        stop.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                stophandler();

            
        );


    

//-----------------Inside oncreate---------------------------------------------------

 public void starthandler()
    

        handler = new Handler();
        handler.postDelayed(new Runnable() 
            @Override
            public void run() 


                if(!condition)
                
                    //Do something after 100ms 


                

            
        , 5000);

    


    public void stophandler()
    
        condition=true;
    

【讨论】:

请解释为什么这段代码回答了这个问题。 假设您在 XML 启动和停止中有 2 个按钮。此代码演示了如何通过单击开始按钮启动处理程序并通过停止按钮停止处理程序。 它可以工作,但 runnable 仍然在后台运行。【参考方案5】:

当你想停止处理程序时,你可以定义一个布尔值并将其更改为 false。像这样..

boolean stop = false;

handler.postDelayed(new Runnable() 
    @Override
    public void run() 

        //do your work here..

        if (!stop) 
            handler.postDelayed(this, delay);
        
    
, delay);

【讨论】:

以上是关于停止 handler.postDelayed()的主要内容,如果未能解决你的问题,请参考以下文章

取消 handler.postdelayed 进程

Handler.postDelayed() 方法中的 delayMillis 参数实际上代表啥?

handler.postDelayed 在 IntentService 的 onHandleIntent 方法中不起作用

Handler的postDelayed(Runnable, long)

Android Handler.removeMessage移除所有postDelayed的问题

怎么在android里显示一个计时器