我有一個textview它的行爲stepuptimer。我使用了一個處理程序,並且每秒更新它,並且它的工作完美。但是,當我關閉應用程序並打開時,計時器從「0」開始。我想要時間來運行背景,當我打開活動時,應該顯示經過的時間,而不是從0開始。顯示時間格式爲00:00:00。Android - 即使在關閉應用時計算背景時間
我的代碼是
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);
}
};
}
這可能會幫助你:https://stackoverflow.com/questions/13401205/android-timer-that-counts-when-app-is-closed –
爲什麼你不保存上次更新的時間在[SharedPreferences ](https://developer.android.com/reference/android/content/SharedPreferences.html)什麼的? – shadygoneinsane
@shadygoneinsane - 我試着將它保存在共享首選項中並獲取它。但它只保存應用程序打開時的最後一次。即使應用程序關閉,我也需要計時器。 –