如何在Android中编辑和删除闹钟时间?

时间:2016-12-15 18:37:12

标签: android android-alarms

我已经制作了一个可以正常工作的闹钟:

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);
    }

现在,如何编辑闹钟时间并删除闹钟?

1 个答案:

答案 0 :(得分:0)

要取消闹钟,您可以使用PendingIntent调用AlarmManager.cancel。

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

要编辑闹钟,您可以先取消闹钟并重新添加,也可以在安排新闹钟时添加PendingIntent.FLAG_UPDATE_CURRENT作为最后一个选项。

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

可能重复Cancel an AlarmManager pendingIntent in another pendingintent