Domanda

In my application i want to set a timeout when the user turn on 3G... after a certain amount of time elapsed , i will turn off 3G.. my problem is cancelling the scheduled timer.. every time i call timer.cancel() .. the program throws errors

the problem cause when i call clearTimeout() method..

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();
}

please help me .. i am an android beginner..

È stato utile?

Soluzione

Android has a class CountdownTimer which has start() and cancel().

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top