如何在Android上暂停handler.postDelayed()计时器

时间:2021-04-03 17:28:21

How can i pause the handler.postDelayed() timer using a button. So when i click the same button again the handler.postDelayed() timer should resume.

如何使用按钮暂停handler.postdelay()计时器。因此,当我再次单击相同的按钮时,handler.postdelay()计时器将恢复。

handler.postDelayed(counterz, 60);

2 个解决方案

#1


16  

Handler does not have a timer to tweak. You are posting to event-queue of a thread, where a lot of other stuff is running as well.

处理程序没有计时器来调整。您正在向线程的事件队列发布消息,其中还有许多其他内容正在运行。

You can cancel posted Runnable's:

你可以取消发布的Runnable:

handler.removeCallbacks(counterz);

And post again, to resume.

然后再发一次,继续。

#2


3  

Handler does not have a pause method. You need to cancel and run again.

处理程序没有暂停方法。您需要取消并再次运行。

http://developer.android.com/reference/android/os/Handler.html#removeCallbacks(java.lang.Runnable)

http://developer.android.com/reference/android/os/Handler.html removeCallbacks(java.lang.Runnable)

public final void removeCallbacks (Runnable r)

公共最终真空远程遥控(可运行的r)

Remove any pending posts of Runnable r that are in the message queue.

删除消息队列中任何可运行r的挂起。

When not required you need to call m_handler.removeCallbacks(m_handlerTask) to cancel the run. If you need again you need to run the the task again.

当不需要时,您需要调用m_handler.removeCallbacks(m_handlerTask)来取消运行。如果需要,您需要再次运行该任务。

            Handler m_handler;
            Runnable m_handlerTask ;  
            m_handler = new Handler();  
            m_handlerTask = new Runnable()
           {
               @Override 
               public void run() {
                             // do something 

                    m_handler.postDelayed(m_handlerTask, 1000);    

               }
          };
          m_handlerTask.run(); // call run

Suppose you use a timer. Even timer does not have pause method.

假设您使用一个计时器。甚至计时器也没有暂停方法。

#1


16  

Handler does not have a timer to tweak. You are posting to event-queue of a thread, where a lot of other stuff is running as well.

处理程序没有计时器来调整。您正在向线程的事件队列发布消息,其中还有许多其他内容正在运行。

You can cancel posted Runnable's:

你可以取消发布的Runnable:

handler.removeCallbacks(counterz);

And post again, to resume.

然后再发一次,继续。

#2


3  

Handler does not have a pause method. You need to cancel and run again.

处理程序没有暂停方法。您需要取消并再次运行。

http://developer.android.com/reference/android/os/Handler.html#removeCallbacks(java.lang.Runnable)

http://developer.android.com/reference/android/os/Handler.html removeCallbacks(java.lang.Runnable)

public final void removeCallbacks (Runnable r)

公共最终真空远程遥控(可运行的r)

Remove any pending posts of Runnable r that are in the message queue.

删除消息队列中任何可运行r的挂起。

When not required you need to call m_handler.removeCallbacks(m_handlerTask) to cancel the run. If you need again you need to run the the task again.

当不需要时,您需要调用m_handler.removeCallbacks(m_handlerTask)来取消运行。如果需要,您需要再次运行该任务。

            Handler m_handler;
            Runnable m_handlerTask ;  
            m_handler = new Handler();  
            m_handlerTask = new Runnable()
           {
               @Override 
               public void run() {
                             // do something 

                    m_handler.postDelayed(m_handlerTask, 1000);    

               }
          };
          m_handlerTask.run(); // call run

Suppose you use a timer. Even timer does not have pause method.

假设您使用一个计时器。甚至计时器也没有暂停方法。