我不明白出了什么问题,但我的闹钟每天都会响起,即使我硬编码了日期,我也不知道发生了什么......
Intent notificationIntent = new Intent(AddTask.this,CustomBroadcastReceiver.class);
notificationIntent.putExtra(Intent.EXTRA_UID,newTaskId);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AddTask.this, newTaskId, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(getApplicationContext().ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.DAY_OF_WEEK,2);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY * 7, pendingIntent);
今天是星期天,所以天数是 1,我写了 calendar.set(Calendar.DAY_OF_WEEK,2);自从我使用了这一刻的时间和分钟后,警报就被触发了……我将日期设置为 1 2 3 4 5 或 6 或即使我制作了多个具有相同 ID 且每个都包含其他警报管理器都没有关系day 它每天触发
最佳答案
为什么需要这条线?
calendar.setTimeInMillis(System.currentTimeMillis());
另外,android开发documentation推荐使用
setInexactRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation);
编辑: 我怀疑没有为创建警报设置 Intent ,所以它没有正确创建警报。 也许这会起作用:
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
//set repeating alarm here
我希望这能回答问题。
关于Android - 警报管理器每天运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39945018/