0

我不知道是什么问题,我在这里看到了很多解决方案,我已经完成了,但没有帮助。这是我的代码。我在创建时从意图传递取消 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);
}
4

1 回答 1

1

您的意图不匹配,因此您的闹钟不会被取消。在CancelAlarm, 改变

intent.setAction("BeforeAction");

intent.setAction("RepeatAction");

将解释原因。

于 2013-09-30T13:24:23.163 回答