带有标志 AlarmManager.INTERVAL_DAY 的 AlarmManager 在当天执行,而不是在第二天执行
AlarmManager with flag AlarmManager.INTERVAL_DAY is executing in the current day, not in the next day
我需要在我的 Android 应用程序中添加推送通知。通知必须每天在特定时间显示(例如下午 1 点)。为此,我正在使用 AlarmManager。当应用程序第一次启动时,我正在注册一个闹钟。但是我遇到了问题。
如果我正在将我的应用程序安装到设备上,例如在下午 1 点 10 分,那么警报会在我的应用程序启动后立即 运行ning。但这是错误的,因为我需要这个闹钟在第二天 运行,而不是当天。任何人都可以帮助我并告诉我如何设置 daily 闹钟,该闹钟必须从当天开始工作。
这是我的代码,目前正在使用
private void registerAMAlarmManger(){
mAMAlarmIntent = new Intent(this, AMAlarmReceiver.class);
mAMPendingIntent = PendingIntent.getBroadcast(this, 0, mAMAlarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
SharedPreferences sPrefs = getSharedPreferences(Constants.PREFERENCES_NAME, Context.MODE_PRIVATE);
int amTime = sPrefs.getInt(Constants.MORNING_TIME, 9);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, amTime);
calendar.set(Calendar.MINUTE, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, mAMPendingIntent);
}
如果当前日期的时间已经过去,它会执行执行警报的代码。这需要处理。我是按照以下方式做的
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, mytesthourofday);
calendar.set(Calendar.MINUTE,
Integer.parseInt(min_am_pm[0]));
// calendar.set(Calendar.AM_PM, am_pm_integer);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 1000 * 24 * 60 * 60,
pendingIntent);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, mytesthourofday);
calendar.set(Calendar.MINUTE,
Integer.parseInt(min_am_pm[0]));
// calendar.set(Calendar.AM_PM, am_pm_integer);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 1000 * 24 * 60 * 60,
pendingIntent);
我需要在我的 Android 应用程序中添加推送通知。通知必须每天在特定时间显示(例如下午 1 点)。为此,我正在使用 AlarmManager。当应用程序第一次启动时,我正在注册一个闹钟。但是我遇到了问题。 如果我正在将我的应用程序安装到设备上,例如在下午 1 点 10 分,那么警报会在我的应用程序启动后立即 运行ning。但这是错误的,因为我需要这个闹钟在第二天 运行,而不是当天。任何人都可以帮助我并告诉我如何设置 daily 闹钟,该闹钟必须从当天开始工作。
这是我的代码,目前正在使用
private void registerAMAlarmManger(){
mAMAlarmIntent = new Intent(this, AMAlarmReceiver.class);
mAMPendingIntent = PendingIntent.getBroadcast(this, 0, mAMAlarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
SharedPreferences sPrefs = getSharedPreferences(Constants.PREFERENCES_NAME, Context.MODE_PRIVATE);
int amTime = sPrefs.getInt(Constants.MORNING_TIME, 9);
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, amTime);
calendar.set(Calendar.MINUTE, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, mAMPendingIntent);
}
如果当前日期的时间已经过去,它会执行执行警报的代码。这需要处理。我是按照以下方式做的
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, mytesthourofday);
calendar.set(Calendar.MINUTE,
Integer.parseInt(min_am_pm[0]));
// calendar.set(Calendar.AM_PM, am_pm_integer);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 1000 * 24 * 60 * 60,
pendingIntent);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, mytesthourofday);
calendar.set(Calendar.MINUTE,
Integer.parseInt(min_am_pm[0]));
// calendar.set(Calendar.AM_PM, am_pm_integer);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
calendar.add(Calendar.DAY_OF_MONTH, 1);
}
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 1000 * 24 * 60 * 60,
pendingIntent);