2015-11-05 441 views
0

我確定在這裏的某處有類似的問題,但我似乎無法找到它。Android定時器延遲和重置

這是我正在嘗試做的。

假設我已連接到服務器,並且如果在過去5分鐘內沒有用戶撥打任何電話,我想斷開連接。但是,如果連一個單一的呼叫時,5分鐘計時器將復位,倒計時5將重新開始..

它似乎很簡單,但我是一種新的Android和試圖搞清楚這些事情..

在此先感謝!


=======編輯

所以這裏的什麼我想要做的代碼的例子。

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

所以基本上,每當有一個調用服務器時被調用。
我需要在if語句中實現什麼?


================= ANSWER @Saeed Mashhadi的答案編輯時,

請參閱下面的日誌輸出。所以起初,它運行良好。當disconnectCounter每秒增加一次時,我又打了一個電話。

呼叫後,disconnectCounter再次從1開始,但它開始每秒增加2。左邊有時間戳。你能告訴我爲什麼會發生這種情況嗎?

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 
...... 

謝謝你!!

+0

答案是編輯,檢查,並告訴我結果 – Seyyed

回答

1

你可以做這樣的事情:

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

謝謝你的一個很好的例子。我很抱歉沒有以前的代碼示例。我已經更新了我的問題。我會試一試你的例子。謝謝! –

+1

答案編輯@ th3pat3l。願它有幫助:) – Seyyed

+0

完美的作品!謝謝!! –

1

由於你沒有代碼,我會給你一個高層次的答案。您需要保留上次連接的時間戳和偏移量。您還需要不時檢查時間戳+時間戳是否低於實際時間戳。並且每次請求都會更新最後一次請求時間戳。

+0

感謝您的快速回復。我實際上不確定在我的問題中放置什麼樣的代碼示例,因爲我想要做的是在特定時間後調用斷開連接方法。至於你的回答,你的意思是我必須在5分鐘內一再檢查嗎? –

+0

另外,請看看我編輯的問題。我試圖舉一個例子。 –