2015-05-03 58 views
0

我建立了一個計時器,即使當我從活動中出去時,計時器也會繼續,當我離開當前活動時該如何停止計時?當我離開我的活動時,如何停止計時器?

螺紋:

t = new Thread() { 

      @Override 
      public void run() { 
      try { 
       while (!isInterrupted()) { 
       Thread.sleep(5000); 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         try { 

         output = new teste().execute(getconversationid).get(); 
         setMyInt(output); 
         Log.e("", output); 
         } catch (InterruptedException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } catch (ExecutionException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
        } 
       }); 
       } 
      } catch (InterruptedException e) { 
      } 
      } 
     }; 

     t.start(); 

回答

-1

在的onDestroy()或的onStop()方法通過調用

t.kill停止定時器螺紋();

另一方面,我會建議使用一個簡單的cound down計時器類而不是自己建立一個。然後在onDestroy或onStop()方法中調用objectname.cancle()。

public class pauseTimer extends CountDownTimer { 

    public pauseTimer(long millisInFuture, long countDownInterval) { 
     super(millisInFuture, countDownInterval); 
    } 

    @Override 
    public void onTick(long millisUntilFinished) { 

    } 



    @Override 
    public void onFinish() { 

    } 
} 
+0

我接收到這樣做的方法殺滅()是未定義的類型 \t螺紋 –

+0

見我的編輯方法。這是我所知道的最好的。 –

相關問題