警报管理器不响
我正在构建一个警报应用程序。为此,我需要提供一个时间作为输入。我正在尝试遵循代码,但警报没有触发。我在网上搜索并修改了很多代码,但仍然没有成功。
有人可以帮我吗。提前致谢。
Intent intent = new Intent(NapAppActivity.this, OnetimeAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(NapAppActivity.this, 1, intent, 0);
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 17);
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
I'm building an Alarm Application. for it, I'm need to supply a time as input. I'm trying following code but alarm doesnt' trigger. I searched online and modified the code much alot but still no luck.
Can someone help me out. Thanks in advance.
Intent intent = new Intent(NapAppActivity.this, OnetimeAlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(NapAppActivity.this, 1, intent, 0);
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, 12);
cal.set(Calendar.MINUTE, 17);
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您应该使用 cal=Calendar.getInstance() 来获取当前日期/时间,而不是使用 GregorianCalendar 构造函数。
然后,只需设置 HourOfDay 和 Minutes,不要触及其他字段。
I think that you should use
cal=Calendar.getInstance()
to get the current date/time instead of using GregorianCalendar constructor.Then, just set HourOfDay and Minutes, do not touch the other fields.
您是否在清单中声明了接收者?
还在 处的接收者名称之前加上完整的包裹声明。如果您的接收器位于另一个包中,我已使用“。”在上述声明中...
谨致,
Arpit
have you declared receiver in your manifest.
Also preceed Receiver name with full package declaration at place of . If your Receiver is in another package I have used "." in above declaration ...
With Regards,
Arpit