В Android Alarm Manager, как мы можем планировать несколько аварийных сигналов, которые не повторяются, и не имеют фиксированных интервалов для повторения? Я не могу использовать функцию setRepeating, так как у будильников нет повторяющегося шаблона.
У меня есть время будильника, хранящееся в таблице базы данных Sqlite, и действие должно выбрать дату и время из этой таблицы и установить аварийные сигналы.
Если мы настраиваем различные сигналы тревоги в цикле, тогда он сохраняет только последний. Я прочитал из сообщения: Как создать несколько предупреждений?
Он сообщает, чтобы прикрепить уникальный идентификатор к намерению, а затем настроить отдельные события тревоги. Но это не сработало для меня.
Есть ли что-то, что нам нужно добавить в файл манифеста, чтобы позаботиться об этом уникальном id?
Код в действии "RegularSchedule" есть и он создает только одно событие тревоги:
while (notifCursor.moveToNext()) {
Intent intent = new Intent(RegularSchedule.this,
RepeatingAlarm.class);
// The cursor returns first column as unique ID
intent.setData(Uri.parse("timer:" + notifCursor.getInt(0)));
PendingIntent sender = PendingIntent.getBroadcast(
RegularSchedule.this, 0, intent, 0);
// Setting time in milliseconds taken from database table
cal.setTimeInMillis(notifCursor.getLong(1));
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
}
Пожалуйста, дайте мне знать, если требуются дополнительные сведения или фрагменты кода.
Файл манифеста (здесь RepeatingAlarm расширяет BroadcastReceiver):
<receiver android:name=".user_alerts.RepeatingAlarm" android:process=":remote" />
<activity android:name=".user_alerts.RegularSchedule"
android:label="@string/reg_schedule_title" android:theme="@android:style/Theme.Light">
</activity>
RepeatingAlarm:
public class RepeatingAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
.......
// The PendingIntent to launch our activity if the user selects this notification
Intent notificationIntent = new Intent (context, DisplayReminder.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
mNotificationManager.notify(2425, notification);