Cancel specific alarm

0

I have this alarm:

This in my acticity ScheduleActivity

 PendingIntent alarmIntent;

           Intent intent = new Intent(HorariosActivity.this, AlarmReceiver.class);
            intent.putExtra("agoa", "teste");
            alarmIntent = PendingIntent.getBroadcast(HorariosActivity.this, 1, intent, 0);

            Calendar calendar = Calendar.getInstance();
         long agora =  calendar.getTimeInMillis();




                calendar.set(Calendar.HOUR_OF_DAY, 12);
                calendar.set(Calendar.MINUTE, 5);
                calendar.set(Calendar.SECOND, 22);
               firstTime = calendar.getTimeInMillis();
         if (agora>firstTime) {
             calendar.add(Calendar.DAY_OF_MONTH, 1 );
             firstTime = calendar.getTimeInMillis();
         }
            AlarmManager am = (AlarmManager)HorariosActivity.this.getSystemService(Context.ALARM_SERVICE);


            am.setRepeating(AlarmManager.RTC_WAKEUP, firstTime, 86400000, alarmIntent);
    }

In another activity I tried to cancel with this code:

The name of this activity is ScheduleActivity2

PendingIntent alarmIntent;

Intent intent = new Intent(HorariosActivity2.this , AlarmReceiver.class);
HorariosActivity.context = getApplicationContext();
alarmIntent = PendingIntent.getBroadcast(HorariosActivity.context, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager am = (AlarmManager)HorariosActivity2.this.getSystemService(Context.ALARM_SERVICE);


am.cancel(alarmIntent);

I'm trying to cancel the alarm with id 1 that was done in another activity but it triggers it.

    
asked by anonymous 21.06.2016 / 14:11

2 answers

0

Try using

PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, alert.idAlert, intent, PendingIntent.FLAG_UPDATE_CURRENT)  

instead of

PendingIntent.FLAG_CANCEL_CURRENT 

Make sure you're using the same alarm object and context.

    
21.06.2016 / 14:35
0

I was passing the wrong context resolved.

PendingIntent alarmIntent;             SchedulesActivity.context = getApplicationContext ();

        Intent intent = new Intent(HorariosActivity.context , AlarmReceiver.class);

        alarmIntent = PendingIntent.getBroadcast(HorariosActivity.context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager am = (AlarmManager)HorariosActivity.context.getSystemService(Context.ALARM_SERVICE);


        am.cancel(alarmIntent);
    
21.06.2016 / 15:51