简体   繁体   English

Android:定时后启动监听器

[英]Android: start listener after timing

i've an activity with a ToggleButton that must start (if checked on) or stop (if checked off) a listener. 我有一个带有ToggleButton的活动,该活动必须启动(如果选中)或停止(如果选中)侦听器。 The problem is that i want to start the listener after a specific time (10 sec for example) but keeping the ToggleButton active and if the user click on it before the timing ending, abort the timing and listener activation. 问题是我想在特定时间(例如10秒)后启动侦听器,但要保持ToggleButton处于活动状态,如果用户在计时结束之前单击它,则中止计时和侦听器激活。 I'm in confusion with the correct way to do that... do you have any idea? 我对执行此操作的正确方法感到困惑...您有任何想法吗?

thanks in advance 提前致谢

Use a Timer and TimerTask. 使用Timer和TimerTask。 Put the timer on 10 seconds delay. 将计时器延迟10秒。

If the user clicks the button again on these 10 seconds, cancel the Timer. 如果用户在这10秒钟内再次单击该按钮,请取消计时器。 If not, start the listener. 如果不是,请启动监听器。

Note that in order to change UI elements through the TimerTask thread, you will have to use an handler. 请注意,为了通过TimerTask线程更改UI元素,您将必须使用处理程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM