午夜后重置计时器

时间:2018-09-10 16:45:47

标签: java android time service calendar

我的应用程序中有几个用户报告了一个奇怪的时间重置/错误行为,我对此感到困惑,并且在测试和收集日志之后-我发现我的计时器(如果在午夜之前启动)会重置为立即超过20个小时,我不知道为什么或如何防止这种情况。

该项目是开源的,欢迎任何人帮助我,这是链接:

GitHub

有关应用程序/项目的信息:

  • 选择应用,选择所需的锁定时间,然后锁定应用。

My Timer Class ( Full )

与时间/日期相关的摘要

calendar = Calendar.getInstance();
    simpleDateFormat = new SimpleDateFormat("H:M:ss");

    mTimer = new Timer();
    mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 5, NOTIFY_INTERVAL);
    intent = new Intent(str_receiver);


    //DB
    db = new DB_Helper(this);
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
    restartServiceIntent.setPackage(getPackageName());

    PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    assert alarmService != null;
    alarmService.set(
            AlarmManager.ELAPSED_REALTIME,
            SystemClock.elapsedRealtime() + 1000,
            restartServicePendingIntent);

    super.onTaskRemoved(rootIntent);
    Log.e("Service_Auto_Restart", "ON");
}

public String twoDatesBetweenTime() {


    try {
        date_current = simpleDateFormat.parse(strDate);
    } catch (Exception e) {

    }

    try {
        date_diff = simpleDateFormat.parse(db.get_Data(1));
    } catch (Exception e) {

    }

    try {


        long diff = date_current.getTime() - date_diff.getTime();
        int int_hours = Integer.valueOf(db.get_Hours(1));
        long int_timer;
        if (int_hours > 10) {
            int_timer = TimeUnit.MINUTES.toMillis(int_hours);
        } else {
            int_timer = TimeUnit.HOURS.toMillis(int_hours);
        }
        long long_hours = int_timer - diff;
        long diffSeconds2 = long_hours / 1000 % 60;
        long diffMinutes2 = long_hours / (60 * 1000) % 60;
        long diffHours2 = long_hours / (60 * 60 * 1000) % 24;


        if (long_hours >= 0) {
            str_testing = String.format("%d:%d:%02d", diffHours2, diffMinutes2, diffSeconds2);
            Log.e("TIME", str_testing);
            db.set_TimerFinish(0);
            //db.set_Running("Y");
            fn_update(str_testing);
        } else {
            stopService(new Intent(getApplicationContext(), Timer_Service.class));
            notification_update();
            db.set_TimerFinish(1);
            db.set_Running("N");
            //db.set_LockTime("");
            db.set_Hours("");
            db.set_Data("");
            //db.set_openCounter(0);
            db.set_openTimes(0);
            mTimer.cancel();
        }
    } catch (Exception e) {
        stopService(new Intent(getApplicationContext(), Timer_Service.class));
        db.set_TimerFinish(1);
        db.set_Running("N");
        // db.set_LockTime("");
        db.set_Hours("");
        db.set_Data("");
        //db.set_openCounter(0);
        db.set_openTimes(0);
        mTimer.cancel();
        mTimer.purge();
    }

    return "";

}

有什么建议吗?


编辑:

  • 计时器工作正常,如果有人设置锁定时间(例如30分钟),它将倒计时30分钟并在完成后解锁。

假设有人在21:30:00开始锁定,计时器将正常工作并在10:00:00结束。

但是,如果有人在23:55:00开始锁定30分钟,那么“解锁的剩余时间”将跳到21小时,而不是30分钟。这仅在新的一天开始/计时器在午夜之前开始时发生。

0 个答案:

没有答案
相关问题