我不知道是什么问题,我在这里看到了很多解决方案,我已经完成了,但没有帮助。这是我的代码。我在创建时从意图传递取消 id,然后传回以取消它。我已经调试过它并传递和接收到的 id 是相同的。请帮助我,这是我的代码。
public void SetRepeatingAlarm(Context context,MyDateClass myDateClass,int reapitInterval){
final int _id = (int) System.currentTimeMillis();
final int minInMiliseconds = 60000;
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
Bundle extras = new Bundle();
extras.putParcelable("DateObj", myDateClass);
extras.putBoolean("IsRepeat", true);
extras.putInt("CancelId", _id);
intent.putExtras(extras);
intent.setAction("RepeatAction");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(myDateClass.getYear(), myDateClass.getMonth(), myDateClass.getDay(), myDateClass.getHour(), myDateClass.getMin(), 0);
PendingIntent pi = PendingIntent.getBroadcast(context, _id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), minInMiliseconds * reapitInterval , pi);
}
public void CancelAlarm(Context context, int id){
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
Bundle extras = new Bundle();
extras.putParcelable("DateObj", null);
extras.putBoolean("IsRepeat", true);
extras.putInt("CancelId", id);
intent.putExtras(extras);
intent.setAction("BeforeAction");
PendingIntent sender = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
sender.cancel();
alarmManager.cancel(sender);
}