how to cancel Alarmmanager

0

Hello, I have created a notification system which every so often I will check if there are notes to show them and that does it through an Alarmmanager that stays in the background cache running. That makes it perfect, but the problem is that when I want it to stop working I am not able to cancel the Alarmmanager. I tried it for active and passive but I do not know how to cancel it.

Here I leave the code.

MAIN CODE.

boolean alarmRunning = (PendingIntent.getBroadcast(this.context, 0, alarm, PendingIntent.FLAG_NO_CREATE) != null);
    if(alarmRunning == false) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.context, 0, alarm, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 59000, pendingIntent);
    }

CLOSURE CODE FROM ANOTHER ACTIVITY.

Intent intent = new Intent(this, Main.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.cancel(pendingIntent);

I hope you can help me out and thanks in advance.

    
asked by user3737118 14.01.2018 в 12:01
source

1 answer

0

Add this (where a is the activity to be deleted), if you have several create several methods

Intent intent = new Intent(this, AlarmReceive.class);

PendingIntent sender = PendingIntent.getBroadcast(this, a, intent, 0);

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(sender);
    
answered by 15.04.2018 в 03:05