【问题标题】:Countdown timer bug after midnight (Related to Timezone and Clock )午夜后的倒数计时器错误(与时区和时钟有关)
【发布时间】:2019-01-01 21:34:15
【问题描述】:

我的应用中有几个用户报告了时间重置/错误的奇怪行为,我对此感到困惑,在测试和收集日志后 - 我认为我的计时器如果在午夜之前启动,会重置为瞬间 20 多个小时,我无法弄清楚为什么或如何防止这种情况。

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

GitHub

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

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

My Timer Class ( Full )

时间/日期相关的sn-p

 public class Timer_Service extends Service {

public static final long NOTIFY_INTERVAL = 1000;
public static String str_receiver = "com.nephi.getoffyourphone.receiver";
public String str_testing;
Calendar calendar;
SimpleDateFormat simpleDateFormat;
String strDate;
Date date_current, date_diff;
//Root Detector
RootBeer rootbeer;
//Con Manager
WifiManager wifiManager;
//DH Helper
DB_Helper db;
Intent intent;
Intent lockIntent;
Intent Shame;
//Shame Int Counter
private Handler mHandler = new Handler();
private Timer mTimer = null;

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

@Override
public void onCreate() {
    super.onCreate();
    //Lock Screen launch
    lockIntent = new Intent(this, locked.class);
    lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

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

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

    //Root Detector
    rootbeer = new RootBeer(this);
    //Wifi Manager
    wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

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

public String twoDatesBetweenTime() {


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

    }

    try {
        // Gets the first timestamp when the lockdown started
        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);

        } else {
            stopService(new Intent(getApplicationContext(), Timer_Service.class));

            mTimer.cancel();
        }

有什么建议吗?


编辑:

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

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

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

【问题讨论】:

  • 这里的代码太多了,我们无法回答这个问题。请edit 您的问题包含一个minimal reproducible example(关注“最小”和“可验证”)来证明您的问题。
  • 当我们不知道 strDatedb.get_Data(1) 的值是和/或应该是什么时,我们应该如何知道发生了什么?
  • @JoeC 我删除了与问题无关的代码。这样更好吗?
  • @Andreas 我刚刚添加了声明的变量,够了吗?还是我应该添加更多解释?
  • 基本上,我们需要的是足够的代码(仅此而已),我们可以复制并粘贴到我们选择的 IDE 中,单击“运行”,然后在一两分钟内查看问题。

标签: java android github timer open-source


【解决方案1】:

出现问题是因为您将时间存储为“H:m:ss”格式,但您忽略了日期方面。使用 System.currentTimeMillis() 可以为您提供 1970 年 1 月 1 日之后的毫秒数。所以这包括日期方面。取这两个值的差值将为您提供过期的毫秒数,您可以检查已用时间的任何时间间隔。


改为这样做:

当为计时器节省时间时,应该开始使用毫秒而不是 H:mm:ss :

long millisNow = System.currentTimeMillis();

将millisNow保存到数据库中。

并节省应该过去的时间:

int minutes = 30;
long millisElapse = 30 * 60 * 1000;

将此保存到数据库(或您需要的任何时间间隔)

现在来看看区别

long diff = timeNow - timeDatabase;

if(diff > millisElapse){
    //end session
}


顺便说一句:您发布的格式为“H:M:ss”。大写的“M”代表月(!)而不是分钟。
查看关于“日期和时间模式”SimpleDateFormat的文档

【讨论】:

  • 哦,好吧,所以我实际上取了两个日期的错误差异,我确实将我的日期保存在 db 中为 h:m:ss .. 我明天试试这个并调试它。非常感谢您的信息!
  • 即使您将整个日期保存为 yyyyMMddHHmmss,使用 new SimpleDateFormat("H:mm:ss"); 进行的转换仍会返回类似:23:55:22 的内容,它与日期无关。
  • 我已经实现了代码并对其进行了测试——它可以在我的设备上运行,但我正在等待几个用户也对其进行测试并进行报告。非常感谢您的帮助!
  • 很高兴它对你有用!您的个人资料显示您位于德国。我在亚琛及其周边生活了 20 年。我只是厌倦了下雨!搬回美国。
  • 哦,真的吗?我几乎每个月都去亚琛,那里有人!哈哈,是的,下雨太可怕了>.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-13
  • 1970-01-01
  • 1970-01-01
  • 2019-06-10
  • 1970-01-01
相关资源
最近更新 更多