The alarm does not stop

I try to stop the alarm and check if it stopped or not, but it always returns true, it means that the alarm is working. I tried to stop the alarm based on the response from the link https://stackoverflow.com/a/4129605/2129 , but it does not work for me.

Please refer to the code below

  • Start alarm

    public static void startSchedulerAlaram(Context ctx) {
    
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 0); 
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);
    
    if (calendar.compareTo(Calendar.getInstance()) <= 0)
        calendar.add(Calendar.DATE, 1);
    
    AlarmManager alarmManager = (AlarmManager) ctx
            .getSystemService(Context.ALARM_SERVICE);
    
    Intent intent = new Intent(ctx, Alaram_Receiver.class);
    intent.setAction(Utility.SCHEDULE_ACTION);
    
    PendingIntent pi = PendingIntent.getBroadcast(ctx, 1, intent,0);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
    calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi);
    
    log("Scheduler Alarm", "Started");
    
    }
    
  • Stop signal

    public static void stopSchedulerAlaram(Context ctx) {
    
    Intent intent = new Intent(ctx, Alaram_Receiver.class);
    intent.setAction(Utility.SCHEDULE_ACTION);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 1,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) ctx
            .getSystemService(Context.ALARM_SERVICE);
    alarmManager.cancel(pendingIntent);
    
    log("Scheduler Alarm", "Stopped");
    }
    
  • Check alarm

    public static boolean checkSchedulerAlaram(Context ctx) {
    
    boolean alarmUp = (PendingIntent.getBroadcast(ctx, 1, new Intent(ctx,Alaram_Receiver.class).setAction(Utility.SCHEDULE_ACTION),
            PendingIntent.FLAG_NO_CREATE) != null);
    
    return alarmUp;
    }
    
+1
source share
3 answers

You need to go different id for set alarm

PendingIntent.getBroadcast(ctx, id, intent,PendingIntent.FLAG_UPDATE_CURRENT);

For cancel alarm you have pass sameid that you used for set.

PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, id,
        intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) ctx
        .getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
+1
source

This code is used to determine if the alarm is set or not:

boolean alarmUp = (PendingIntent.getBroadcast(ctx, 1, new Intent(ctx,Alaram_Receiver.class).setAction(Utility.SCHEDULE_ACTION),
    PendingIntent.FLAG_NO_CREATE) != null);

PendingIntent, , .

, :

PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, 1,
    intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) ctx
    .getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);

, PendingIntent . / PendingIntent, , , PendingIntent, . ( ):

pendingIntent.cancel();
+1

,

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

All Articles