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

Nay*_*ung 1 android timeout timer 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();
}
Run Code Online (Sandbox Code Playgroud)

请帮帮我..我是一个Android初学者..

Ale*_*tin 5

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