2017-08-03 80 views
0

我有一個textview它的行爲stepuptimer。我使用了一個處理程序,並且每秒更新它,並且它的工作完美。但是,當我關閉應用程序並打開時,計時器從「0」開始。我想要時間來運行背景,當我打開活動時,應該顯示經過的時間,而不是從0開始。顯示時間格式爲00:00:00Android - 即使在關閉應用時計算背景時間

我的代碼是

public class GoOnlinePage extends Activity { 

private TextView Tv_timer; 
private Handler onlineTimeHandler = new Handler(); 
private long startTime = 0L; 
private long timeInMilliseconds = 0L; 
private long timeSwapBuff = 0L; 
private long updatedTime = 0L; 
private int mins; 
private int secs; 
private int hours; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.go_online_page); 

    Tv_timer = (TextView) findViewById(R.id.txt_timer); 

    startTime = SystemClock.uptimeMillis(); 

    onlineTimeHandler.post(updateTimerThread); 


    } 



    private Runnable updateTimerThread = new Runnable() { 

    public void run() { 

     timeInMilliseconds = SystemClock.uptimeMillis() - startTime; 

     updatedTime = timeSwapBuff + timeInMilliseconds; 

     secs = (int) (updatedTime/1000); 
     hours = secs/(60 * 60); 
     mins = secs/60; 
     secs = secs % 60; 
     if (mins >= 60) { 
      mins = 0; 
     } 


     Tv_timer.setText(String.format("%02d", hours) + ":" + 
     String.format("%02d", mins) + ":" 
       + String.format("%02d", secs)); 



     onlineTimeHandler.postDelayed(this, 1 * 1000); 

    } 

}; 

} 

我嘗試使用會話和更新的時間每一秒內updateTimerThread。當我打開應用程序時,我得到當前時間並找到兩次之間的差異並更新計時器。但沒有任何工作。

請幫幫我。提前致謝。

編輯

解決

我解決它通過使用服務,並通過使用的BroadcastReceiver更新TextView的。我已附上以下代碼供您參考。

import android.app.Service; 
import android.content.Intent; 
import android.os.Binder; 
import android.os.Handler; 
import android.os.IBinder; 
import android.os.SystemClock; 
import android.util.Log; 



public class TimerService extends Service { 
    private static String LOG_TAG = "TimerService"; 
    private IBinder mBinder = new MyBinder(); 

    Handler onlineTimeHandler = new Handler(); 
    long startTime = 0L, timeInMilliseconds = 0L, timeSwapBuff = 0L, updatedTime = 0L; 
    int mins, secs, hours; 
    String time = ""; 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     return super.onStartCommand(intent, flags, startId); 
    } 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     Log.v(LOG_TAG, "in onCreate"); 
     startTime = SystemClock.uptimeMillis(); 
     onlineTimeHandler.post(updateTimerThread); 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     Log.v(LOG_TAG, "in onBind"); 
     return mBinder; 
    } 

    @Override 
    public void onRebind(Intent intent) { 
     Log.v(LOG_TAG, "in onRebind"); 
     super.onRebind(intent); 
    } 

    @Override 
    public boolean onUnbind(Intent intent) { 
     Log.v(LOG_TAG, "in onUnbind"); 
     return true; 
    } 

    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.v(LOG_TAG, "in onDestroy"); 
     if(onlineTimeHandler!=null){ 
      onlineTimeHandler.removeCallbacks(updateTimerThread); 
     } 
    } 


    public class MyBinder extends Binder { 
     TimerService getService() { 
      return TimerService.this; 
     } 
    } 

    private Runnable updateTimerThread = new Runnable() { 

     public void run() { 

      timeInMilliseconds = SystemClock.uptimeMillis() - startTime; 

      updatedTime = timeSwapBuff + timeInMilliseconds; 

      secs = (int) (updatedTime/1000); 
      hours = secs/(60 * 60); 
      mins = secs/60; 
      secs = secs % 60; 
      if (mins >= 60) { 
       mins = 0; 
      } 

      time = String.format("%02d", hours) + ":" + String.format("%02d", mins) + ":" 
        + String.format("%02d", secs); 


      Intent intent = new Intent(); 
      intent.setAction("com.app.Timer.UpdateTime"); 
      intent.putExtra("time", time); 
      sendBroadcast(intent); 

      onlineTimeHandler.postDelayed(this, 1 * 1000); 

     } 

    }; 
} 
+0

這可能會幫助你:https://stackoverflow.com/questions/13401205/android-timer-that-c​​ounts-when-app-is-closed –

+0

爲什麼你不保存上次更新的時間在[SharedPreferences ](https://developer.android.com/reference/android/content/SharedPreferences.html)什麼的? – shadygoneinsane

+0

@shadygoneinsane - 我試着將它保存在共享首選項中並獲取它。但它只保存應用程序打開時的最後一次。即使應用程序關閉,我也需要計時器。 –

回答

0

當您的活動被破壞Tv_timer對象也將被破壞,一個哪個線程是updating.When活動再次啓動創建Tv_timer的新對象。

同樣的時間可以存儲在某個地方,當應用程序啓動

後來用
0

有可能實現這一目標有兩種方式:

  1. 可以使用Service定時器,它會保持甚至跑步如果應用程序在後臺或被殺害。
  2. 使用SharedPreference和存儲計時器值的app被殺害,當,當應用程序重新啓動,就取這個值,當你的計時器在一些持久的地方開始與持續時間
1

商店startTime值和它(如SharedPreferences等等),並刪除當你的計時器結束。

在您的Activity#onCreate方法中,檢查持久值,如果存在,請使用該值初始化您的值startTime而不是當前系統時間。

的問題關係到你的代碼的一些技巧:

  • 使用new Date().getTime()而不是SystemClock.uptimeMillis()。它使您的代碼在重新啓動時更加健壯。
  • 當您撥打Activity#onPause時,停止您的計時器代碼,並在Activity#onResume調用時啓動。由於您不需要在不可見的情況下更新您的用戶界面,因此無需進行更新。
1

很簡單,使用一些靜態變量。 這個例子繼續,即使該活動是在後臺或毀壞計數,沒有SharedPreferences或服務需要

public class MainActivity extends AppCompatActivity { 

    private static TextView txtCounter; 
    private static Handler handler; 
    private static Runnable runnable ; 
    private static int count; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     txtCounter = (TextView) findViewById(R.id.txtCounter); 
     if (handler == null) { 
      handler = new Handler(); 
      runnable = new Runnable() { 
       @Override 
       public void run() { 
        txtCounter.setText("Count = " + count); 
        count++; 
        handler.postDelayed(runnable, 1000); 
       } 
      }; 
      handler.postDelayed(runnable, 1000); 
     } 
    } 

} 

UPDATE:

要暫停:handler.removeCallbacks(runnable);

要恢復: handler.postDelayed(runnable, 1000);

重置計數器:count = 0;

+0

你如何開始/停止/暫停計數器? – Hansie

+0

看看更新的答案。 –

相關問題