【发布时间】:2019-01-01 21:34:15
【问题描述】:
我的应用中有几个用户报告了时间重置/错误的奇怪行为,我对此感到困惑,在测试和收集日志后 - 我认为我的计时器如果在午夜之前启动,会重置为瞬间 20 多个小时,我无法弄清楚为什么或如何防止这种情况。
该项目是开源的,欢迎任何人帮助我,这里是链接:
关于应用程序/项目的信息:
- 选择应用,选择所需的锁定时间,锁定应用。
时间/日期相关的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(关注“最小”和“可验证”)来证明您的问题。
-
当我们不知道
strDate和db.get_Data(1)的值是和/或应该是什么时,我们应该如何知道发生了什么? -
@JoeC 我删除了与问题无关的代码。这样更好吗?
-
@Andreas 我刚刚添加了声明的变量,够了吗?还是我应该添加更多解释?
-
基本上,我们需要的是足够的代码(仅此而已),我们可以复制并粘贴到我们选择的 IDE 中,单击“运行”,然后在一两分钟内查看问题。
标签: java android github timer open-source