How to start and stop the timer arbitrarily

We can add a preset timer in REGISTER_ACTIVITY_TIMER_TAB , but this method is not flexible enough to start/stop arbitrarily. Here is another way to add a timer. There are three methods about timers in the Activity class. The following describes how to use them.

/**
 * Register timer
 */
void registerUserTimer(int id, int time);
/**
 * Cancel timer
 */
void unregisterUserTimer(int id);
/**
 * Reset the timer
 */
void resetUserTimer(int id, int time);
  1. In logic.cc, add a variable to identify whether the timer has been registered.

      /**
       * Whether the timer is registered
       */
      static bool isRegistered = false;
      #define TIMER_HANDLE   2
    
  2. We add two more buttons. In the click event of the button, we add codes for registering and canceling the timer respectively.

    
     static bool onButtonClick_ButtonTimerOn(ZKButton *pButton) {
         //If not registered, register the timer
         if (!isRegistered) {
             mActivityPtr->registerUserTimer(TIMER_HANDLE, 500);
             isRegistered = true;
         }
         return false;
     }
    
     static bool onButtonClick_ButtonTimerOff(ZKButton *pButton) {
         //If the timer is already registered, cancel the registration
         if (isRegistered) {
             mActivityPtr->unregisterUserTimer(TIMER_HANDLE);
             isRegistered = false;
         }
         return false;
     }
    

[!Warning] The above-mentioned three functions registerUserTimer,unregisterUserTimer,andresetUserTimer cannot be called in the onUI_Timer function, which will cause deadlock.

Sample code

Refer to the TimerDemo project in Sample Code .
Preview effect picture:

Effects

powered by Gitbooklast modified: 2021-05-28 12:00:31

results matching ""

    No results matching ""