我正在使用AlarmManager()
来解除通知。我将它设置为早上10:30开火,每24小时重复一次。
我的代码如下。我昨天进行了测试,问题是在接下来的2小时内重复了大约4-5次。我不明白这是什么问题。我只想在早上10:30开火,并且只在24小时重复。
请帮我解决问题。我在我的应用程序的启动画面onCreate()
上调用此代码
我的代码:
Intent myIntent = new Intent(Splash.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Splash.this,
0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar firingCal = Calendar.getInstance();
Calendar currentCal = Calendar.getInstance();
firingCal.set(Calendar.HOUR_OF_DAY, 10);
firingCal.set(Calendar.MINUTE, 30);
firingCal.set(Calendar.SECOND, 0);
long intendedTime = firingCal.getTimeInMillis();
long currentTime = currentCal.getTimeInMillis();
if (intendedTime >= currentTime) {
alarmManager.setRepeating(AlarmManager.RTC, intendedTime,
AlarmManager.INTERVAL_DAY, pendingIntent);
} else {
firingCal.add(Calendar.DAY_OF_MONTH, 1);
intendedTime = firingCal.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC, intendedTime,
AlarmManager.INTERVAL_DAY, pendingIntent);
}
答案 0 :(得分:3)
您可以使用CommonsWare cwac-wakeful库。它具有内置支持来设置警报。
答案 1 :(得分:0)
// Retrieve a PendingIntent that will perform a broadcast
Intent alarmIntent = new Intent(HomeContactActivity.this,
AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(
HomeContactActivity.this, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// Set the alarm to start at 10:00 AM
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
manager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 86400000,
pendingIntent); // for repeating in every 24 hours