无法停止CountDownTimer

时间:2017-02-02 09:21:17

标签: android countdowntimer

美好的一天!我尝试将倒数计时器推迟到日期,但我有下一个问题:当我设置日期和时间 - 它的工作没问题,但是当我尝试重置计时器然后重新加载应用程序时,我仍然看到前一个计时器倒计时。我已经尝试了所有我甚至可以成像的东西。

public class MainActivity extends AppCompatActivity  {


TextView tv_days;
TextView tv_hours;
TextView tv_minutes;
TextView tv_seconds;
TextView tv_msg;
ImageButton btnTimer;
ImageButton btnExit;

long total_millisec;
CountDownTimer cdt;
TimerPreference timerPreference;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv_days = (TextView) findViewById(R.id.days);
    tv_hours = (TextView) findViewById(R.id.hours);
    tv_minutes = (TextView) findViewById(R.id.minutes);
    tv_seconds = (TextView) findViewById(R.id.seconds);
    tv_msg = (TextView) findViewById(R.id.msg);
    btnTimer = (ImageButton) findViewById(R.id.setTimer);
    btnExit = (ImageButton) findViewById(R.id.resetTimer);

    final Intent setTimer = new Intent(this, ShowActivity.class);

    timerPreference = new TimerPreference(this);
    total_millisec = timerPreference.getTime();

    if(total_millisec != 0){
        setTime();
    }



    View.OnClickListener mainWin = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.setTimer:
                    startActivity(setTimer);
                    break;
                case R.id.resetTimer:
                    resetTimer();
                    break;
            }

        }
    };
    btnTimer.setOnClickListener(mainWin);
    btnExit.setOnClickListener(mainWin);

}


public void setTime() {

    cdt = new CountDownTimer(total_millisec, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {

            timerPreference.setTime(millisUntilFinished);

            long days = TimeUnit.MILLISECONDS.toDays(millisUntilFinished);
            millisUntilFinished -= TimeUnit.DAYS.toMillis(days);
            long hours = TimeUnit.MILLISECONDS.toHours(millisUntilFinished);
            millisUntilFinished -= TimeUnit.HOURS.toMillis(hours);
            long minutes = TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished);
            millisUntilFinished -= TimeUnit.MINUTES.toMillis(minutes);
            long seconds = TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished);



            setScreenData(String.valueOf(days),
                    String.valueOf(hours),
                    String.valueOf(minutes),
                    String.valueOf(seconds));
        }

        @Override
        public void onFinish() {
            Log.e("TIMER:"," FINISH!");
        }
    };
    cdt.start();
}

public void setScreenData(String setD, String setH,String setM, String setS){
    tv_days.setText(setD);
    tv_hours.setText(setH);
    tv_minutes.setText(setM);
    tv_seconds.setText(setS);
    tv_msg.setText("");

}

private void resetTimer(){

    AlertDialog.Builder quitDialog = new AlertDialog.Builder(this);
    quitDialog.setTitle("Reset Timer?");

    quitDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {

        }
    });

    quitDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            cancelTimer();
            recreate();
        }
    });

    quitDialog.show();
}

void cancelTimer() {
    if(cdt!=null) {
        cdt.cancel();
    }
    timerPreference.clearPreferences();

}

}

我记录了这段代码几个小时,我注意到即使我取消了计时器,清除首选项并重新加载应用程序,“total_millisec = timerPreference.getTime();” in onCreate还有时间信息...... 提前感谢您的回答。

UDP:

TimerPreference.java

class TimerPreference {

private SharedPreferences prefs;

TimerPreference(Context context){
    prefs = context.getSharedPreferences("Timer",Context.MODE_PRIVATE);
}

void setTime(Long time){
    prefs.edit().putLong("time", time).apply();
}

Long getTime(){
    return prefs.getLong("time", 0);
}

void clearPreferences(){
    SharedPreferences.Editor editor = prefs.edit();
    editor.clear();
    editor.apply();
}

}

UPD2: 我使用String.valueOf(timerPreference.getTime())记录了代码,当我按下reset时,我得到“TIME-IN-cancelTimer:0”,而不是onrecreate()重新加载的活动;然后我得到“TIME-in-onCreate:0”,但当我关闭应用程序并再次打开时 - 我得到“TIME-IN-onCreate:47025952”,所以现在我确定问题出在TimerPreference类中......但是我仍然找不到解决方案。

几小时后我发现了另一种模式:如果设置时间,重置计时器并重新启动应用程序 - 一切正常。但是如果要设置时间,重新启动它,然后重置计时器 - 在下一个应用程序启动后,它将再次开始倒计时。

2 个答案:

答案 0 :(得分:0)

timerPreference.setTime(0);方法中timerPreference.clearPreferences();之后添加cancelTimer()

答案 1 :(得分:0)

尝试在editor.commit();

clearPreferences()方法中使用TimerPreference