我有一个保存在TimeInterval对象中的每周计划,它们工作正常。我构建了一个循环,所以我可以为每个循环设置一个警报。我的代码不起作用,我缺少什么?
此外,如何从另一个线程的BroadcastReceiver中取消这些警报?
for(ArrayList<TimeInterval> day : days){
for(TimeInterval ti : day){
numberofintents++;
Intent i = new Intent(getApplicationContext(),ChangeMode.class);
i.putExtra("ringermode", ti.getRingerMode());
PendingIntent pending = PendingIntent.getService(getApplicationContext(), id, i,PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MINUTE,ti.getMinute()%60);
cal.set(Calendar.HOUR_OF_DAY,ti.getMinute()/60);
cal.set(Calendar.DAY_OF_WEEK,dayofweek);
Log.d(TAG, cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE));
service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000*60*60*24*7, pending);
id++;
}
dayofweek++;
}
答案 0 :(得分:0)
首先制作待处理意图的数组列表,
public static ArrayList <PendingIntent> intentArray= new ArrayList <PendingIntent>();
然后将待处理的意图添加到数组列表
for(ArrayList<TimeInterval> day : days){
for(TimeInterval ti : day){
numberofintents++;
Intent i = new Intent(getApplicationContext(),ChangeMode.class);
i.putExtra("ringermode", ti.getRingerMode());
PendingIntent pending = PendingIntent.getService(getApplicationContext(), id, i,PendingIntent.FLAG_CANCEL_CURRENT);
intentarray.add(pending);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MINUTE,ti.getMinute()%60);
cal.set(Calendar.HOUR_OF_DAY,ti.getMinute()/60);
cal.set(Calendar.DAY_OF_WEEK,dayofweek);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
pendingIntent);
id++;
}
dayofweek++;
}
现在您的待处理意图存储在数组列表中。
如果您想取消闹钟,
alarmManager.cancel(intentArray.get(i));