我有一些问题将Android AlarmManager设置为每天执行。 用户可以设置他的特定时间,并且应该每天运行。 这是代码:
@Override
protected void onResume() {
super.onResume();
Intent intent = new Intent(MainActivity.this,
NotificationBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getService(
MainActivity.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Make sure, there is no active Alarm
alarmManager.cancel(pendingIntent);
if(notificationEnabled) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.AM_PM, Calendar.PM);
cal.set(Calendar.HOUR_OF_DAY, notificationHour);
cal.set(Calendar.MINUTE, notificationMinute);
cal.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
pendingIntent);
}
}
现在我无法弄清楚,为什么这不会起作用。我曾经将警报设置为10:05,警报在晚上开启。警报应该在特定时间每天开启(如果它当然只启用)。从当天开始。因此,如果我将其设置为几分钟或几小时后,它应该在今天和每隔一天运行,直到我取消警报。#
提前致谢
答案 0 :(得分:0)
我认为你从日历中获得了一个错误的Unix时间戳。试试这个
// Set the alarm to start at approximately 2:00 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);