在Android的Alarm Manager中,如果要安排多个不重复且没有固定间隔重复的闹钟,我们该如何做呢?由于这些闹钟没有任何重复模式,因此无法使用“setRepeating”函数。
我将闹钟时间存储在SQLite数据库表中,活动应该从这个表中获取日期和时间并设置闹钟。
如果我们在循环中设置不同的闹钟,那么它只保留最后一个。我从这篇帖子中读到:如何创建多个闹钟? 它告诉我们将唯一ID附加到意图中,然后设置单独的闹钟事件。但是对我来说没有起作用。
在清单文件中需要添加什么内容以处理这个唯一ID吗?
下面是“RegularSchedule”活动中的代码,它只创建一个闹钟事件:
我将闹钟时间存储在SQLite数据库表中,活动应该从这个表中获取日期和时间并设置闹钟。
如果我们在循环中设置不同的闹钟,那么它只保留最后一个。我从这篇帖子中读到:如何创建多个闹钟? 它告诉我们将唯一ID附加到意图中,然后设置单独的闹钟事件。但是对我来说没有起作用。
在清单文件中需要添加什么内容以处理这个唯一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);