2014-05-23 30 views
0

我在我的一項活動中有一個計時計時器,我想在第二次活動中顯示計時器已經過去的時間。我試圖使用片段,但計時器保持在第二次活動的啓動。在多個活動上計數定時器

我的定時器的代碼是:

startTime = SystemClock.uptimeMillis(); 
    customHandler.postDelayed(updateTimerThread, 0); 

    private Runnable updateTimerThread = new Runnable() { 

    public void run() { 

     timeInMilliseconds = SystemClock.uptimeMillis() - startTime; 

     updatedTime = timeSwapBuff + timeInMilliseconds; 

     int secs = (int) (updatedTime/1000); 
     int mins = secs/60; 
     secs = secs % 60; 
     int milliseconds = (int) (updatedTime % 1000); 
    /* Timer.setText("" + mins + ":" 
       + String.format("%02d", secs) + ":" 
       + String.format("%03d", milliseconds));*/ 
     customHandler.postDelayed(this, 0); 
    } 

}; 
+3

創建服務並將計時器放入其中並顯示來自服務的任何活動的更新結果。 – Hulk

+0

我有這個想法,但我不知道如何創建一個後臺服務。你可以分享一些代碼嗎?這將是一個很大的幫助。 – user3626957

+0

define - 「顯示計時器」。 – TheLostMind

回答

1

所述第一活動傳遞startTime到第二在Intent。在第二項活動中,請不要將startTime設置爲SystemClock.uptimeMillis(),而是將其從第一項活動發送的Intent中的附加項中讀取。

+0

還是無法讓它運行...... – user3626957

相關問題