Search code examples
javaandroidsharedpreferencescountdowntimer

Countdowntimer saved in sharedpreference shows wrong timer


I have a countdown timer that takes a long as parameter and then it shows a countdown of that time. When i click the backbutton and exit the app, I want the remaining time ofthe countdown timer to be saved in a sharepreference in onStop() method. And when i reopen the app I want to take the remaining time as the parameter and start the timer again, until it hits 0.

I have troubles because if I my parameter is 2, then countdown timer counts down from 1 minutes and 59 secs. And when I exit the app and the reopen the app it still shows 1 minutes and 59secs. The countdown timer doesnt take the subtract of what time is left. It just shows the same time as entered.

private void startTimer() {
    countDownTimer = new CountDownTimer(timee*60000 , 1000) {
        @Override
        public void onTick(long l) {

            tv.setText(simpleDateFormat.format(l));

            timesLeft = l;

        }

        @Override
        public void onFinish() {
            tv.setText("00:00:00");}
}.start();

Here is onStop() and onStart();

protected void onStart() {
    super.onStart();
    SharedPreferences sPrefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);


    if(1>0){

    long s = sPrefs.getLong("sysstoptime", 0);
    long tt = sPrefs.getLong("timeleft", 0);
    long currenttime = System.currentTimeMillis();
    long k = sPrefs.getLong("timeset", 0);


    s = s+tt;

    timee = (s-currenttime)/60000+1;


    startTimer();}

    else {
        Toast t5 = Toast.makeText(this, "less than or equal 0", Toast.LENGTH_LONG);
        t5.show();
    }

}

@Override
protected void onStop() {
    super.onStop();


    SharedPreferences sPrefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
    SharedPreferences.Editor editor = sPrefs.edit();

    editor.putLong("sysstoptime", System.currentTimeMillis());
    editor.putLong("timeleft", timesLeft);
    editor.putLong("timeset", timeset);




    editor.apply();


}

What im trying to do is that when the countdown timer is running and I am leaving the app, i am taking the System.currenttimeinmillis + the remaining time of countdown timer. That is variable s+tt. Then I equal timee to the remaining time of s+tt - System.currenttimeinmillis, to have countdown timer, countdown the remaining time left including the time when app was closed/stopped. Then I call the method startTimer(); with my new parameter of timee. But it doesn't show the remaining time, instead it shows the same time as i enter when i exit and enter app.

timee is long variable, and the user choses what timee is, its in minutes. so if timee = 10, then it means 10 minutes and countdown timer will count from 09:59


Solution

  • First, when you get value to countdowning, multiply it by 60000 after press start button [or whatever you use to call startTimer()].

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           timee = YOUR_VALUE_TO_COUNTDOWN * 60000;
           startTimer();
        }
    });
    

    change startTimer() like that:

        private void startTimer() {
        countDownTimer = new CountDownTimer(timee, 1000) {
            @Override
            public void onTick(long l) {
                tv.setText(simpleDateFormat.format(l));
                timesLeft = l;
            }
    
            @Override
            public void onFinish() {
                tv.setText("00:00:00");
            }
        }.start();
    }
    

    change onStop()

    @Override
    protected void onStop() {
        super.onStop();
    
        SharedPreferences sPrefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
        SharedPreferences.Editor editor = sPrefs.edit();
        editor.putLong("timeleft", timesLeft);
        editor.putLong("sysstoptime", System.currentTimeMillis());
        editor.apply();
    }
    

    and finally onStart()

    @Override
    protected void onStart() {
        super.onStart();
        SharedPreferences sPrefs = getSharedPreferences("sharedPreferences", MODE_PRIVATE);
    
        if(1>0){            
            timesLeft = sPrefs.getLong("timeleft", 0);
            long stopTime = sPrefs.getLong("sysstoptime", 0);
            long currentTime = System.currentTimeMillis();
            timee = timesLeft - (currentTime - stopTime);
            startTimer();
        }
        else {
            Toast t5 = Toast.makeText(this, "less than or equal 0", Toast.LENGTH_LONG);
            t5.show();
        }
    }