Faisal Shaikh
Faisal Shaikh

Reputation: 4147

How to edit and delete alarm time in Android?

I have built an alarm which works properly:

public static void setAlarm(int hours, int minutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, hours);
        calendar.set(Calendar.MINUTE, minutes);
        Intent myIntent = new Intent(PreferenceHelper.getContext(), TimeManagementAdapter.AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(PreferenceHelper.getContext(), 0, myIntent, 0);
        alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
    }

Now, how can I edit the alarm time and delete the alarm?

Upvotes: 1

Views: 350

Answers (1)

Mattias Cederlund
Mattias Cederlund

Reputation: 11

For cancelling the alarm you may call AlarmManager.cancel with your PendingIntent.

Intent myIntent = new Intent(PreferenceHelper.getContext(), TimeManagementAdapter.AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(PreferenceHelper.getContext(), 0, myIntent, 0);
alarmManager.cancel(pendingIntent);

For editing the alarm, you have the option to first cancel it and re-add it or you may add PendingIntent.FLAG_UPDATE_CURRENT as the last option when scheduling a new alarm.

PendingIntent pendingIntent = PendingIntent.getBroadcast(PreferenceHelper.getContext(), 0, myIntent, 0, PendingIntent.FLAG_UPDATE_CURRENT);

Possible duplicate Cancel an AlarmManager pendingIntent in another pendingintent

Upvotes: 1

Related Questions