关闭应用程序后,CountDownTimer在计时器结束之前停止工作

时间:2018-12-18 20:38:03

标签: android countdowntimer

我将CountDownTimer设置为在应用关闭后6个小时后更改首选项,但有时计时器停止工作,但是如果我将计时器设置为10秒,首选项的确会更改。任何想法都可能导致CountDownTimer在6个小时内停止工作吗?这是我的代码。

public class TimeService extends Service {

CountDownTimer cdt = null;
SharedPreferences pref;
Intent timeService;

@Override
public void onCreate() {
    timeService = new Intent(this, TimeService.class);
    startService(timeService);
    pref = this.getSharedPreferences("log", MODE_PRIVATE);
    pref = this.getSharedPreferences("url", MODE_PRIVATE);
    cdt = new CountDownTimer(21600000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {

        }

        @Override
        public void onFinish() {
            savePreferences("url", WebPages.page);
            savePreferences("log", "false");
        }
    };
    cdt.start();
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

private void savePreferences(String key, String value) {
    SharedPreferences sharedPreferences = getSharedPreferences("log", MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.apply();
}

}

我会尽力回答任何问题。

0 个答案:

没有答案