Android:如何在设置独特警报后获取alarmID?

时间:2016-01-05 14:46:13

标签: android android-alarms

我正尝试通过以下代码插入新的独特闹钟:

public static Long setUniqueAlarm(Reminder reminder, Integer year, int month, Integer day,
                                      Integer hour, Integer minute, Context context) {
        Log.d("ALARMHELPER", "Year: " + year + ", month: " + month + ", day: " + day +
                ", hour: " + hour + ", minute: " + minute);
        Long alarmId = null;
        try {
            DateTime dt = new DateTime(year, month, day, hour, minute, 0, 0);
//            DateTime dt = new DateTime(2015, 11, 27, 18, 11, 0, 0);
            AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
            Intent alarmIntent = new Intent(context, AlarmBroadcastReceiver.class);
            // Pass the intent type and other additional values
            alarmIntent.putExtra(Constants.Global.ALARM_TYPE, Constants.Global.ALARM_TYPE_UNIQUE);
            alarmIntent.putExtra(Constants.Global.ALARM_CATEGORY, reminder.getConditionDescription());
            alarmIntent.putExtra(Constants.Global.ALARM_DESCRIPTION, reminder.getConditionDescription());
            int id = (int)DatabaseHelper.getHighestAlarmId();
            PendingIntent pendingAlarmIntent = PendingIntent.getBroadcast(context, id, alarmIntent, 0);
            alarmManager.set(AlarmManager.RTC_WAKEUP, dt.getMillis(), pendingAlarmIntent);
            // Save alarm item to DB too.
            // TODO: Remove alarms (table, model) before the production version
            //saveAlarmToDb(reminder, dt.toString(), id, context);
        } catch(Exception e) {
            Logger.e(e.getMessage());
        }

        return alarmId;
    }

因为需要以编程方式取消警报,所以我需要在添加新警报后获取它们的值。

是否可以通过此ID获取警报的唯一ID并取消警报?

或者我应该生成唯一ID并将ID保存到额外? (这意味着)

alarmIntent.putExtra("uniqueID", 1234556);

非常感谢任何建议。

0 个答案:

没有答案