Search code examples
androidtimerpostdelayed

Android Timer delay and reset


I'm sure there is a similar question somewhere on here but I just can't seem to find it.

Here's what I'm trying to do.

let's say I am connected to a server and I want to disconnect from it if no calls are made by the user in last 5 minutes. BUT, if even a single call is made, the 5 minute timer would reset and countdown to 5 would start again..

It seems simple but I am kind of new to Android and trying to figure things out..

thanks in advance!


======= EDIT

So here's an example of code of what I want to do.

try {
    client.publish(topic, message);
    success = true;

    if(topic.equals("response")) {
        // need to reset my 5 min timer here
        // but if 5 mins go by and this try/catch isn't called again,
        // need to call the client.disconnect() method here
    } else {
        client.disconnect();
    }

} catch (Exception e) {
    success = false;
    e.printStackTrace();
}

so basically that gets called every time there is a call to the server.
What do i need to implement in the if statement?


================= EDIT AFTER ANSWER @Saeed Mashhadi's answer

Please see the following log output. So at first, it worked fine. and When the disconnectCounter was increasing by one every second, I made another call.

after that call, disconnectCounter started at 1 again but it started increasing by 2 every second. There are timestamps on the left. can you please tell me why this is happening?

11-05 15:50:59.395 13253-13521/ ~~ disconnectCounter - 1  
11-05 15:51:00.404 13253-13521/ ~~ disconnectCounter - 2  
11-05 15:51:01.401 13253-13521/ ~~ disconnectCounter - 3  
11-05 15:51:02.403 13253-13521/ ~~ disconnectCounter - 4  
11-05 15:51:03.394 13253-13521/ ~~ disconnectCounter - 5  
11-05 15:51:04.400 13253-13521/ ~~ disconnectCounter - 6  
11-05 15:51:05.396 13253-13521/ ~~ disconnectCounter - 7  
11-05 15:51:06.402 13253-13521/ ~~ disconnectCounter - 8  
11-05 15:51:07.408 13253-13521/ ~~ disconnectCounter - 9  
11-05 15:51:08.399 13253-13521/ ~~ disconnectCounter - 10  
11-05 15:51:09.407 13253-13521/ ~~ disconnectCounter - 11  
11-05 15:51:10.406 13253-13521/ ~~ disconnectCounter - 12  
11-05 15:51:11.401 13253-13521/ ~~ disconnectCounter - 13  
11-05 15:51:12.409 13253-13521/ ~~ disconnectCounter - 14  
......   
11-05 15:51:27.498 13253-13253/ ~~~~~~~~~~~ USER CALL ~~~~~~~~~~~  
11-05 15:51:28.399 13253-13521/ ~~ disconnectCounter - 1  
11-05 15:51:28.514 13253-13521/ ~~ disconnectCounter - 2  
11-05 15:51:29.398 13253-13521/ ~~ disconnectCounter - 3  
11-05 15:51:29.515 13253-13521/ ~~ disconnectCounter - 4  
11-05 15:51:30.403 13253-13521/ ~~ disconnectCounter - 5  
11-05 15:51:30.519 13253-13521/ ~~ disconnectCounter - 6  
11-05 15:51:31.401 13253-13521/ ~~ disconnectCounter - 7  
11-05 15:51:31.512 13253-13521/ ~~ disconnectCounter - 8  
11-05 15:51:32.398 13253-13521/ ~~ disconnectCounter - 9  
11-05 15:51:32.510 13253-13521/ ~~ disconnectCounter - 10  
11-05 15:51:33.398 13253-13521/ ~~ disconnectCounter - 11  
11-05 15:51:33.506 13253-13521/ ~~ disconnectCounter - 12  
11-05 15:51:34.400 13253-13521/ ~~ disconnectCounter - 13  
11-05 15:51:34.504 13253-13521/ ~~ disconnectCounter - 14  
......  

Thanks you!!


Solution

  • You can do something like this:

    Timer timer;
    

    ...

    try {
        client.publish(topic, message);
        success = true;
    
        if(topic.equals("response")) {
            // If try/catch is called, counter resets
            disconnectCounter=0;
            timer=new Timer();
    
            timer.scheduleAtFixedRate(new TimerTask() {
                @Override
                public void run() {
                    // Increase counter every second
                    disconnectCounter++;
                    Log.i("counter", disconnectCounter + "");
                    // If 5 mins go by and this try/catch isn't called
                    // again, disconnect
                    if(disconnectCounter==300){ // 300=5*60
                        client.disconnect(); 
                        timer.cancel();
                        disconnectCounter=0;
                    }
                }
            }, 1000, 1000);
    
        } else {
            client.disconnect();
            disconnectCounter=0;
        }
    
    } catch (Exception e) {
        success = false;
        timer.cancel();
        disconnectCounter=0;
        e.printStackTrace();
    }