在Android Alarm Manager中,如何调度多个不重复、无固定间隔重复的告警?我不能使用'setRepeating‘函数,因为警报没有任何重复模式。
我将警报时间存储在Sqlite数据库表中,活动应该从该表中选择日期和时间并设置警报。
如果我们在一个循环中设置不同的警报,那么它只保留最后一个。我从帖子中读到:How can create more than one alarm?
它告诉将唯一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>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);发布于 2011-07-13 17:02:37
这就是对我有效的方法。我正在分享这个解决方案,以便其他人能够受益,并找到这个问题的快速解决方案。
我欢迎任何其他的投入,让我们更多地了解解决方案的技术性,以及为什么某些东西可以工作,而其他东西不能工作:)
(1)首先,清单文件:确保你的类有BroadcastReceiver的接收器。
<receiver android:name=".RepeatingAlarm" android:process=":remote">
<intent-filter>
<data android:scheme="timer:" />
</intent-filter>
</receiver>请注意,该类是主包的一部分。如果它在某个子包中,请移动到主包。主包是您在“manifest”标签中定义的包。
‘’intent filter‘用于定义'action’和'data‘。您可以将Activity类放在这里,它将从您的待定意图中调用。但是我发现如果你在清单中定义'action‘,它不会在活动上显示动态值。它只显示静态值。很奇怪。如果你遇到了同样的问题,不要把'action‘放在清单中,而是把它放在BroadcastReceiver类中,作为待定意图的一部分。
‘'data’标签是在使用AlarmManager调度不同的警报时,您将放置唯一意图的动态URI的标签。有关更多详细信息,请参阅后续步骤。
(2) Activity类,您将在其中使用AlarmManager来调度警报:我将使用数据库来存储我的警报时间值,然后使用这些值进行调度。我的游标从表中获取唯一的_ID和报警时间(自1970年1月1日以来的秒数)。请注意,此处输入的URI与清单文件中的URI相同。
Calendar cal = Calendar.getInstance();
int notifIterator = 0;
if (notifCursor.getCount() > 0) {
while (notifCursor.moveToNext()) {
Intent intent = new Intent(MySchedule.this,
RepeatingAlarm.class);
// As the same intent cancels the previously set alarm having
// same intent
// changing the intent for every alarm event so that every alarm
// gets
// scheduled properly.
intent.setData(Uri.parse("timer:" + notifCursor.getInt(0)));
PendingIntent sender = PendingIntent.getBroadcast(
MySchedule.this, 0, intent,
Intent.FLAG_GRANT_READ_URI_PERMISSION);
cal.setTimeInMillis(notifCursor.getLong(1) * 1000);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
notifIterator++;
Toast mToast = Toast.makeText(
RegularSchedule.this,
"Reminders added to the calendar successfully for "
+ android.text.format.DateFormat.format(
"MM/dd/yy h:mmaa",
cal.getTimeInMillis()),
Toast.LENGTH_LONG);
mToast.show();
}
}如果在执行此操作后仍未看到警报,请检查仿真器采用的时区。有时,我们计划为本地时区,但模拟器计划为GMT时区。如果您查看toast消息,它将帮助您解决此问题。
(3)最后一个是BroadcastReceiver类。请注意,要打开数据库,您需要使用'context':
public void onReceive(Context context, Intent intent) {
// Update the status in the notification database table
int notificationId = Integer.parseInt(intent.getData().getSchemeSpecificPart());
db = context.openOrCreateDatabase(DATABASE_NAME,
SQLiteDatabase.CREATE_IF_NECESSARY, null);
<<<< Do DB stuff like fetching or updating something>>>>
// Raise the notification so that user can check the details
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.icon;
CharSequence tickerText = "your text";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
// Count of number of notifications
notification.number = notifCount;
CharSequence contentTitle = "your title ";
CharSequence contentText = "your notification text";
// 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;
// Instead of 1234 or any other number, use below expression to have unique notifications
// Integer.parseInt(intent.getData().getSchemeSpecificPart())
mNotificationManager.notify(1234, notification);
}请注意,如果您想创建单独的通知,则可以在调用notify()时将请求id作为唯一进行传递。
最后,您可以创建当用户单击通知时要调用的DisplayReminder类。
发布于 2015-02-07 05:50:38
正如@Jonathon Horsman所建议的,确保您创建的意图是唯一的。
如果您想设置10个告警,例如:
for(int i=; i<10; i++) {
Intent intent = new Intent(YourActivity.this,
YourAlarm.class);
intent.setData(Uri.parse("timer:" + i);
PendingIntent sender = PendingIntent.getBroadcast(
YourActivity.this, 0, intent,
Intent.FLAG_GRANT_READ_URI_PERMISSION);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, yourTimeInMillis, sender);
}对我来说工作得很好。
https://stackoverflow.com/questions/6649402
复制相似问题