How to cancel the timer and renew the same timer?

前端 未结 3 1017
南笙
南笙 2021-01-25 16:31

I am creating an app that vibrate and beep every 30 sec and when I log out the vibrate and beep must be cancelled and when I log in the vibrate and beep should resume.

3条回答
  •  情深已故
    2021-01-25 16:55

    Why do you need a static TimerTask.You can give like this which works fine for me.

    timer.schedule(new TimerTask() {
            @Override
            public void run() {
            //your code
            }
            }, 0, 30000);
    

    While logout use, timer.cancel(). Here you can simply cancel the timer.No need to cancel the TimerTask.

提交回复
热议问题