Android重复警报未取消

时间:2013-09-30 13:10:44

标签: android repeatingalarm

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

1 个答案:

答案 0 :(得分:1)

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

intent.setAction("BeforeAction");

intent.setAction("RepeatAction");

This将解释原因。