Search code examples
androidkotlinalarmmanageralarm

Unable to Cancel the Alarm defined in kotlin


I was creating the Alarm by defined below method

Intent intent = new Intent(context, AlarmClockReceiver.class);
    intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
    Bundle bundle = new Bundle();
    bundle.putSerializable(ALARM_CLOCK, dosesReminder);
    intent.putExtra(ALARM_CLOCK_BUNDLE, bundle);
    intent.putExtra("MedicineName", dosesReminder.getMedicine_name());
    String MedicineTime = "";
    if (dosesReminder.getMorning_dosed().equals("1")) {
        MedicineTime = "1 Morning ";
    }
    if (dosesReminder.getAfter_noon_dosed().equals("1")) {
        MedicineTime = MedicineTime + "1 Afternoon ";
    }
    if (dosesReminder.getEvening_dosed().equals("1")) {
        MedicineTime = MedicineTime + "1 Evening ";
    }
    intent.putExtra("MedicineTime", MedicineTime);
    Log.e("Alarm Id---", "" + alarm_id);
    PendingIntent pi = PendingIntent.getBroadcast(context, Integer.parseInt(alarm_id), intent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    long nextTime = calculateNextTime(alarm.hour,
            alarm.minute, getWeeks(alarm));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, nextTime, pi);
    else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, nextTime, pi);
    } else {
        alarmManager.set(AlarmManager.RTC_WAKEUP, nextTime, pi);
    }

Alarm works well and also able to fire Notification with above code.

i used below code to cancel the Alarm

try {
        Log.e("Cancel Alarm Id---", "" + alarmClockCode);
        Intent intent = new Intent(context, AlarmClockReceiver.class);
        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        Bundle bundle = new Bundle();
        bundle.putSerializable(ALARM_CLOCK, dosesReminder);
        intent.putExtra(ALARM_CLOCK_BUNDLE, bundle);
        intent.putExtra("MedicineName", dosesReminder.getMedicine_name());
        String MedicineTime = "";
        if (dosesReminder.getMorning_dosed().equals("1")) {
            MedicineTime = "1 Morning ";
        }
        if (dosesReminder.getAfter_noon_dosed().equals("1")) {
            MedicineTime = MedicineTime + "1 Afternoon ";
        }
        if (dosesReminder.getEvening_dosed().equals("1")) {
            MedicineTime = MedicineTime + "1 Evening ";
        }
        intent.putExtra("MedicineTime", MedicineTime);

        PendingIntent pi = PendingIntent.getActivity(context, alarmClockCode,
                intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager am = (AlarmManager) context
                .getSystemService(Activity.ALARM_SERVICE);
        am.cancel(pi);
    } catch (Exception e) {
        e.printStackTrace();
    }

i also checked the id are same when set and cancel the Alarm but i am unable to cancel the Alarm.


Solution

  • As i reviewed my code there was a single mistake while canceling the Alarm, that i made as below:

     PendingIntent pi = PendingIntent.getActivity(context, alarmClockCode,
                intent, PendingIntent.FLAG_CANCEL_CURRENT);
    

    I used the getActivity method to fetch the Pending Intent object but while creating the Pending Intent Object i used the getBroadcast method.

    Now Issue resolved