Cancel specific alarm

Asked

Viewed 387 times

1

I got this alarm :

This in my acticity Horariosactivity

 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 Horariosactivity2

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 Alarm with id 1 that was done in another activity but it fires the same.

2 answers

1

Try to use

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

instead of

PendingIntent.FLAG_CANCEL_CURRENT 

Make sure you are using the same alarm object and context.

1

I was passing the wrong context solved .

Pendingintent alarmIntent; Horariosactivity.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);
  • then my answer was correct? It would not be enough to work and comment on my reply?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.