Вопрос

I don't know what is the problem,I saw many solution here I've already done it,but didn't help. Here is my code. I'm passing cancel id from intent while creating, and passing back, to cancel it. I've debugged it and passed and received id is same. Please help me, here is my code.

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);
}
Это было полезно?

Решение

Your intents don't match, so your alarm won't be canceled. In CancelAlarm, change

intent.setAction("BeforeAction");

to

intent.setAction("RepeatAction");

This will explain why.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top