如果日期添加到日历,警报管理器无法触发警报

发布于 2024-12-22 02:11:57 字数 1271 浏览 2 评论 0原文

我正在尝试使用 AlarmManager 在我的应用程序中创建警报。

我可以设置多个闹钟,但如果将 DATE 参数添加到日历中,则根本不会触发闹钟。以下是我的代码

        Intent intent = new Intent(this, OneShotAlarm.class);
        /*Pass the task row ID as the Unique ID for Pending Intent*/
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), (int) rowid , intent, PendingIntent.FLAG_ONE_SHOT);

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
        calendar.clear();
        calendar.setTimeInMillis(System.currentTimeMillis());

        calendar.set(Calendar.HOUR_OF_DAY, mHour);
        calendar.set(Calendar.MINUTE, mMinute);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        long timeSet = calendar.getTimeInMillis();
        alarmManager.set(AlarmManager.RTC_WAKEUP, timeSet, pendingIntent);

如果我将日期参数添加到日历,则

        calendar.add(Calendar.DAY_OF_MONTH, mDay);
        calendar.add(Calendar.MONTH, mMonth);
        calendar.add(Calendar.YEAR, mYear);

不会触发警报。我必须在未来的某个日期安排一个活动。请提出我所缺少的内容。感谢您的帮助!

PS 我正在从 Date & 中获取日期和时间。时间对话框选择器

I am trying to create the Alarms in my application using AlarmManager.

I am able to set multiple alarms, but if DATE parameter is added to Calender, the alarms are not at all triggered. Following is my code

        Intent intent = new Intent(this, OneShotAlarm.class);
        /*Pass the task row ID as the Unique ID for Pending Intent*/
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), (int) rowid , intent, PendingIntent.FLAG_ONE_SHOT);

        AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
        calendar.clear();
        calendar.setTimeInMillis(System.currentTimeMillis());

        calendar.set(Calendar.HOUR_OF_DAY, mHour);
        calendar.set(Calendar.MINUTE, mMinute);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        long timeSet = calendar.getTimeInMillis();
        alarmManager.set(AlarmManager.RTC_WAKEUP, timeSet, pendingIntent);

If I add the Date parameters to Calender as

        calendar.add(Calendar.DAY_OF_MONTH, mDay);
        calendar.add(Calendar.MONTH, mMonth);
        calendar.add(Calendar.YEAR, mYear);

The alarms are not triggered. I have to schedule a event at a future date. Please suggest what I am missing. Thanks for the help!!

P.S. I am taking the date and time from Date & Time dialog picker

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

冰魂雪魄 2024-12-29 02:11:57

我已经多次实现 AlarmManager,以下技术将对您有所帮助。

  1. 以毫秒为单位计算您的闹钟时间,例如您想在当前时间后 10 分钟然后 10*60*1000 毫秒后设置闹钟。

  2. 添加您计算的时间(以当前毫秒为单位)

示例

long currentTime = System.currentTimeMillis();
long fireTime = 10 * 60 * 1000;    
Intent ucintent = new Intent(getApplicationContext(),TimeAlarmReceiver.class);
ucintent.putExtra("isAlarm", true);

PendingIntent mTimeSlot = PendingIntent.getBroadcast(getApplicationContext(), (int)fireTime , ucintent, PendingIntent.FLAG_ONE_SHOT);
alarmManager.set(AlarmManager.RTC_WAKEUP,currentTime+ fireTime, mTimeSlot);

上面的示例非常完美。

谢谢你,

I have implement AlarmManager many times, Following technique will help you.

  1. calculate your alarm time in milliseconds for example you want to set alarm after 10 minutes then 10*60*1000 millisecond after current time.

  2. Add your calculated time in current millisecond

Example

long currentTime = System.currentTimeMillis();
long fireTime = 10 * 60 * 1000;    
Intent ucintent = new Intent(getApplicationContext(),TimeAlarmReceiver.class);
ucintent.putExtra("isAlarm", true);

PendingIntent mTimeSlot = PendingIntent.getBroadcast(getApplicationContext(), (int)fireTime , ucintent, PendingIntent.FLAG_ONE_SHOT);
alarmManager.set(AlarmManager.RTC_WAKEUP,currentTime+ fireTime, mTimeSlot);

Above example works perfect.

Thank You,

平安喜乐 2024-12-29 02:11:57

Ketan的回答很好,但有一个错误。

    long currentTime = System.currentTimeMillis();
    long fireTime = 10 * 60 * 1000;    
    Intent ucintent = new Intent(getApplicationContext(),TimeAlarmReceiver.class);
    ucintent.putExtra("isAlarm", true);

    PendingIntent mTimeSlot = PendingIntent.getBroadcast(getApplicationContext(), (int) requestCode, ucintent, PendingIntent.FLAG_ONE_SHOT);
    alarmManager.set(AlarmManager.RTC_WAKEUP,currentTime+ fireTime, mTimeSlot);

但是,您不希望 PendingIntent 中包含“fireTime”。您应该有一个请求代码。这是您创建的用于识别您的待处理意图的代码。它对 Ketan 的情况有效,因为他总是使用相同的时间。但如果你改变时间,你最终会得到两种不同的意图。

请参阅 https://developer.android.com/reference/android/app/PendingIntent .html

Ketan's answer is good but there is an error.

    long currentTime = System.currentTimeMillis();
    long fireTime = 10 * 60 * 1000;    
    Intent ucintent = new Intent(getApplicationContext(),TimeAlarmReceiver.class);
    ucintent.putExtra("isAlarm", true);

    PendingIntent mTimeSlot = PendingIntent.getBroadcast(getApplicationContext(), (int) requestCode, ucintent, PendingIntent.FLAG_ONE_SHOT);
    alarmManager.set(AlarmManager.RTC_WAKEUP,currentTime+ fireTime, mTimeSlot);

However you don't want "fireTime" in the PendingIntent. You should have a request code. Which is a code you create to identify your pending intents. It works in Ketan's case because he is always using the same time. But if you change the time, you will end up with two different intents.

see https://developer.android.com/reference/android/app/PendingIntent.html

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