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

标签 android timer timeout 3g

在我的应用程序中,我想在用户打开 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有一个类CountdownTimer其中有 start()cancel()

关于android - 如何在android中设置可取消的超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18554637/

相关文章:

android - 使用处理程序自动滚动 WebView

java - SQLite 异常 : unrecognized token in UPDATE statement

c - C for Windows 32 控制台应用程序中的计时器事件

.net - Management Studio 和 TableAdapter 之间存储过程的执行时间差异很大

excel - 在 VBA WinHttpRequest 上捕获超时

python - 在 Python 中实现多处理进程的超时

android - 收藏的内容未在 webview 上正确显示

安卓:接收短信

python - 在 pygame 中暂停游戏和用户输入

java - 在 Android Studio 中设置移动按钮的计时器