java – 如何在报警管理器中处理预定时间列表以推送通知

我正在尝试将存储在Sqlite中的时间列表(它有小时和分钟)检索到警报管理器中以通过通知执行提醒.我的方法是将存储在Sqlite中的所有预定时间循环到警报管理器中,以根据存储的时间列表执行通知,但通知不会发出蜂鸣声.

但是,当我指定一次(小时和分钟)时,它可以工作.下面是有效的代码示例,但我不想这样:

alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 45);
Intent myIntent = new Intent(ListRemainder.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(ListRemainder.this, 0, myIntent, 0);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);

但是当涉及循环几个时间表它不起作用,这是我想要的方法,下面是代码示例,我缺少什么?

//listOfTime is the one which contains the list of stored scheduled time in Sqlite.
for (int i = 0; i < listOfTime.size(); i++) {
    int hour = Integer.parseInt(listOfTime.get(i).toString().substring(0, 2));
    int minute = Integer.parseInt(listOfTime.get(i).toString().substring(3));
    System.out.print("Hour : " + hour + " Minute : " + minute);
    Log.d("MyActivity", "Alarm On");
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minute);
    Intent myIntent = new Intent(ListRemainder.this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(ListRemainder.this, 0, myIntent, 0);
    alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}

解决方法:

getBroadcast的原型如下:

getBroadcast(Context context, int requestCode, Intent intent, int flags)

为每个PendingIntent设置唯一的“requestCode”应该允许调度多个警报:

PendingIntent.getBroadcast(context, uniqueValue, intent, PendingIntent.FLAG_UPDATE_CURRENT);

为确保唤醒用户严重警报:

void setAlarmClock (AlarmManager.AlarmClockInfo info, 
            PendingIntent operation)

来自Docs:
“安排一个代表闹钟的闹钟,用于在用户关闭时通知用户.期望当此闹钟触发时,应用程序将进一步唤醒设备以告知用户有关闹钟的信息.在屏幕上,播放声音,振动等等.因此,系统通常还将使用此处提供的信息来告知用户这个即将发生的警报.

由于此类警报的性质,类似于setExactAndAllowWhileIdle(int,long,PendingIntent),即使系统处于低功耗空闲(a.k.a. doze)模式,也允许触发这些警报.

总结一下:

>使用setAlarmClock唤醒设备以向用户显示信息.
>使用setExactAndAllowWhileIdle在后台工作,必须在打盹期间完成.但是,这些警报的触发可能会大致延迟1分钟到15分钟.
>使用FCM从远程应用程序实时唤醒设备.实际上,如果可能的话,许多应用程序应该使用FCM而不是setExactAndAllowWhileIdle.
>如果在打盹期间完全同步并不重要,请使用setExact.另外,在API 21之前的设备上使用setExact而不是setAlarmClock或setExactAndAllowWhileIdle …

Lutaaya的最终解决方案:

AlarmManager.AlarmClockInfo(calendar.getTimeInMillis(),pendingIntent), pendingIntent);

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能