【问题标题】:how to set a cancellable timeout in android如何在android中设置可取消的超时
【发布时间】:2013-09-01 02:24:50
【问题描述】:

在我的应用程序中,我想在用户打开 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初学者..

【问题讨论】:

    标签: android timer timeout 3g


    【解决方案1】:

    Android 有一个类CountdownTimer,它有start()cancel()

    【讨论】:

    • 我试过 CountdownTimer 但 cancel() 对我不起作用.. 当计时器启动时.. 我无法取消 sechdule 任务.. 我在服务中使用计时器..
    猜你喜欢
    • 2011-09-27
    • 2016-09-06
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 2014-01-06
    相关资源
    最近更新 更多