警报在今天凌晨 12 点触发,而不是在 48 小时后触发
alarm is getting fired today at 12 AM rather then after 48 hours
我每隔 48 小时(即两天)在午夜 12 点响起闹钟。
这是我的代码
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR, 12); // MIDNIGHT 12 AM
calendar.set(Calendar.MINUTE,00);
calendar.set(Calendar.SECOND, 00);
calendar.getTime().toString();
dayAlarmMgr.setRepeating(
AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY * 2, // EVERY TWO DAYS
dayAlarmIntent);
现在,当我 运行 我的应用程序在今天凌晨 12 点触发警报时,它应该会在 48 小时后根据要求触发。
所以我怎样才能让它在 12 点 48 小时后启动 a.m。只要 ?
您需要将闹钟第一次响起的时间增加 2 天。您还应该使用 Calendar.HOUR_OF_DAY
,因为它更直接。
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0); // MIDNIGHT 12 AM
calendar.set(Calendar.MINUTE,00);
calendar.set(Calendar.SECOND, 00);
calendar.add(Calendar.HOUR_OF_DAY, 48);
dayAlarmMgr.setRepeating(
AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY * 2, // EVERY TWO DAYS
dayAlarmIntent);
我每隔 48 小时(即两天)在午夜 12 点响起闹钟。
这是我的代码
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR, 12); // MIDNIGHT 12 AM
calendar.set(Calendar.MINUTE,00);
calendar.set(Calendar.SECOND, 00);
calendar.getTime().toString();
dayAlarmMgr.setRepeating(
AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY * 2, // EVERY TWO DAYS
dayAlarmIntent);
现在,当我 运行 我的应用程序在今天凌晨 12 点触发警报时,它应该会在 48 小时后根据要求触发。
所以我怎样才能让它在 12 点 48 小时后启动 a.m。只要 ?
您需要将闹钟第一次响起的时间增加 2 天。您还应该使用 Calendar.HOUR_OF_DAY
,因为它更直接。
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 0); // MIDNIGHT 12 AM
calendar.set(Calendar.MINUTE,00);
calendar.set(Calendar.SECOND, 00);
calendar.add(Calendar.HOUR_OF_DAY, 48);
dayAlarmMgr.setRepeating(
AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY * 2, // EVERY TWO DAYS
dayAlarmIntent);