在 Android 警报管理器中,我们如何安排多个不重复且没有固定间隔重复的警报?我不能使用“setRepeating”功能,因为警报没有任何重复模式。
我将警报时间存储在 Sqlite 数据库表中,活动应该从该表中选择日期和时间并设置警报。
如果我们在一个循环中设置不同的警报,那么它只保留最后一个。我从帖子中读到:如何创建多个警报?
它告诉将唯一 ID 附加到意图,然后设置单独的警报事件。但这对我不起作用。
我们需要在 Manifest 文件中添加一些东西来处理这个唯一的 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>
重复警报:
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);