我需要为我在PendingIntent内创建的每个警报添加一个唯一的ID。
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
目前,请求代码设置为0。
我希望能够列出listview
内的警报,然后允许用户从列表中取消警报时间。我的逻辑是使用每个警报的唯一ID来取消。
如何为pendingIntent中创建的每个警报设置唯一ID?
这是我用于setAlarm()
public void setAlarm(Context context, Long affirmationTime){
alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(), affirmationTime, alarmIntent);
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
这是我将用于cancelAlarm()
public void cancelAlarm(Context context){
// If the alarm has been set, cancel it.
if (alarmManager != null) {
alarmManager.cancel(alarmIntent);
}
// Disable {@code SampleBootReceiver} so that it doesn't automatically restart the
// alarm when the device is rebooted.
ComponentName receiver = new ComponentName(context, BootReceiver.class);
PackageManager pm = context.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}