我對Android開發很新穎。我有一個應用程序,記錄用戶在工作多久,並顯示他們賺了多少。它完美的工作,直到Android殺死該活動。有沒有辦法來防止這種情況?我已經讀了一些關於服務的內容,但我不確定自己是否需要甚至如何使用它們。任何幫助都是極好的。感謝您的時間。 (編輯)這是我的clockIn()方法。它在用戶按下按鈕中的時鐘時被調用。如何防止Android殺死我的活動。服務可能嗎?
public void clockIn(){
start = System.currentTimeMillis();
mTimer = new Timer();
mTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
now = System.currentTimeMillis();
seconds = date.getSeconds();
elapsedTime = now - start;
earnings = (wageHour/60/60/1000)*elapsedTime;
runOnUiThread(new Runnable() {
public void run() {
timeDisplay.setText("$" + String.format("%.4f", earnings));
}
});
}
}, 0, 100);
}
是的,只需使用一項服務! –