即使应用程序被破坏,如何保存倒数计时器的状态? [英] How to save state of countdown timer even if the app is destroyed?

查看:79
本文介绍了即使应用程序被破坏,如何保存倒数计时器的状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

请参阅代码并指导我

public类Main2Activity扩展了AppCompatActivity {

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();
}

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);
}

}

推荐答案

如果销毁是指您专注于其他应用,则仅可以在要保存的数据上使用static关键字。如果您要强制完全关闭应用程序进行销毁,则可以将其保存到onStop()方法中的文件中。

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.

这篇关于即使应用程序被破坏,如何保存倒数计时器的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆