问题
I am creating an app in which there is a timer on button click the timer starts and I want to continue the timer even I destroy my app, I am using countdown timer method how can I save it's state? I am a noob
Please see the code and guide me
public class Main2Activity extends AppCompatActivity {
private static final long START_TIME_IN_MILLIS = 60000;
private TextView mTextViewCountDown;
private Button button_claim;
private CountDownTimer mCountDownTimer;
CharSequence count;
private boolean mTimerRunning;
private long mTimeLeftInMillis = START_TIME_IN_MILLIS;
boolean clicked = false;
Calendar now = Calendar.getInstance();
public static final String PREFS_NAME = "MyTimer_Settings";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
mTextViewCountDown = findViewById(R.id.text_view_countdown);
button_claim = findViewById(R.id.btn_claim);
final int i1 = now.get(Calendar.MINUTE);
final int i = now.get(Calendar.SECOND);
count = mTextViewCountDown.getText();
button_claim.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clicked = true;
if (mTimerRunning) {
pauseTimer();
} else {
resetTimer();
startTimer();
}
}
});
updateCountDownText();
}
/*************************************************************************************************/
@Override
protected void onStop() {
super.onStop();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
// Writing data to SharedPreferences
SharedPreferences.Editor editor = settings.edit();
editor.putLong("Timer",Long.parseLong(mTextViewCountDown.getText().toString()));
editor.commit();
pauseTimer(); }
@Override
protected void onRestart() {
super.onRestart();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
// Reading from SharedPreferences
Long value = settings.getLong("Timer", 0);
mTextViewCountDown.setText(String.valueOf(value));
}
/**Timer only */
private void startTimer() {
mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 500) {
@Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
}
@Override
public void onFinish() {
mTimerRunning = false;
button_claim.setEnabled(true);
button_claim.setClickable(true);
button_claim.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
}
}.start();
mTimerRunning = true;
button_claim.setEnabled(false);
button_claim.setClickable(false);
button_claim.setBackgroundColor(getResources().getColor(R.color.colorGrey));
}
private void updateCountDownText() {
int minutes = (int) (mTimeLeftInMillis / 1000) / 60;
int seconds = (int) (mTimeLeftInMillis / 1000) % 60;
String timeLeftFormatted = String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds);
mTextViewCountDown.setText(timeLeftFormatted);
}
private void pauseTimer() {
mCountDownTimer.cancel();
mTimerRunning = false;
button_claim.setEnabled(true);
button_claim.setClickable(true);
}
private void resetTimer() {
mTimeLeftInMillis = START_TIME_IN_MILLIS;
updateCountDownText();
button_claim.setEnabled(true);
button_claim.setClickable(true);
}
}
回答1:
If by destroyed you mean that you focus on an other app, you can just just the static keyword on the data you want to save. If you mean destroy by forcefully closing the app completely you could save it to a file in the onStop() method.
来源:https://stackoverflow.com/questions/48448539/how-to-save-state-of-countdown-timer-even-if-the-app-is-destroyed