如何在Android中设置可取消的超时

奈林昂

在我的应用程序中,我想在用户打开3G时设置超时...在经过一定时间后,我将关闭3G。.我的问题是取消计划的计时器..每次我调用timer.cancel( )..程序抛出错误

我调用clearTimeout()方法时出现的问题原因。

Timer timer;

class RemindTask extends TimerTask {
           public void run() {
               //do something when time's up 
                log("timer","running the timertask..");//my custom log method
                timer.cancel(); //Terminate the timer thread
            }

}

public void setTimeout(int seconds) {
    timer = new Timer();
    timer.schedule(new RemindTask(), seconds*1000);

}

public void clearTimeout(){
    log("timer", "cancelling the timer task");//my custom log method
    timer.cancel();
    timer.purge();
}

请帮助我..我是一个android初学者..

亚历山大·库里亚赫汀(Alexander Kulyakhtin)

Android有一个CountdownTimer类,该类具有start()cancel()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章