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

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

概述我正在尝试将存储在Sqlite中的时间列表(它有小时和分钟)检索到警报管理器中以通过通知执行提醒.我的方法是将存储在Sqlite中的所有预定时间循环到警报管理器中,以根据存储的时间列表执行通知,但通知不会发出蜂鸣声.但是,当我指定一次(小时和分钟)时,它可以工作.下面是有效的代码示

我正在尝试将存储在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);
总结

以上是内存溢出为你收集整理的java – 如何在报警管理器中处理预定时间列表以推送通知全部内容,希望文章能够帮你解决java – 如何在报警管理器中处理预定时间列表以推送通知所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1117446.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存