即使应用程序被销毁,如何保存倒数计时器的状态?

时间:2018-01-25 17:16:51

标签: android sharedpreferences countdown countdowntimer

我正在创建一个应用程序,其中有一个计时器按钮单击计时器启动,我想继续计时器,即使我销毁我的应用程序,我使用倒计时计时器方法如何保存它的状态? 我是菜鸟

请参阅代码并指导我

公共类Main2Activity扩展了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 个答案:

答案 0 :(得分:0)

如果销毁你的意思是你专注于其他应用程序,你可以只想要保存的数据上的静态关键字。如果你的意思是通过强制关闭应用程序进行销毁,你可以将其保存到onStop()方法中的文件中。