警报管理器不响

发布于 2024-12-07 17:26:02 字数 1004 浏览 1 评论 0原文

我正在构建一个警报应用程序。为此,我需要提供一个时间作为输入。我正在尝试遵循代码,但警报没有触发。我在网上搜索并修改了很多代码,但仍然没有成功。

有人可以帮我吗。提前致谢。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北陌 2024-12-14 17:26:02

我认为您应该使用 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.

暮色兮凉城 2024-12-14 17:26:02
<receiver android:name=".OnetimeAlarmReceiver" android:process=":remote" />

您是否在清单中声明了接收者?

还在 处的接收者名称之前加上完整的包裹声明。如果您的接收器位于另一个包中,我已使用“。”在上述声明中...

谨致,

Arpit

<receiver android:name=".OnetimeAlarmReceiver" android:process=":remote" />

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文