重复警报不起作用 [英] Repeating Alarm not working

查看:107
本文介绍了重复警报不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这类问题被问了很多遍...但是请先阅读我的问题,然后再否决投票或将其标记为重复.

I know this type of questions are aksed so many times...but please first read my question before down voting or marking as duplicate.

我已经提到了很多这样的问题,例如用于管理警报,但找不到任何解决方案,所以我在这里.

I have referred so many SO questions like this for managing alarm but cant find any solution so i am here.

在我的应用中,我想在每天8:00触发一次重复警报,而其他触发则由用户指定.

In my app i want to trigger repeating alarm one at daily 8.00 am and others as user specified.

首先我累了要使用setRepeating重复报警...这是我的代码

First i tired to repeat alarm using setRepeating...this is my code

Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 8);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.AM_PM,Calendar.AM);
            //calendar.set(Calendar.MILLISECOND, 0);


            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            Intent pintent = new Intent(this, AlarmReceiver.class);

            pintent.putExtra("id", 0);
            pintent.putExtra("ontime", false);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
                    pintent, PendingIntent.FLAG_UPDATE_CURRENT);


            am.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);

实际上,问题是长时间睡眠后警报没有响起.如果它使用更改的日期和时间进行测试,则可以正常点火....但是一晚上后它将停止点火...

Actually the problem is alarm is not firing after long sleep. If it test using changing date and time it fires perfactly....but it stops firing after a night...

所以我切换到第二个选项,如给定链接中所述.我按照以下步骤使用setExact设置了警报,并在每次火灾时将其重新安排.

So i switched to second option as mentioned in given link. I set alarm using setExact as follow and reschedule it on each fire.

 Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, 1);
            calendar.set(Calendar.HOUR_OF_DAY, 8);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);
            calendar.set(Calendar.AM_PM,Calendar.AM);
            //calendar.set(Calendar.MILLISECOND, 0);


            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            Intent pintent = new Intent(this, AlarmReceiver.class);

            pintent.putExtra("id", 0);
            pintent.putExtra("ontime", false);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
                    pintent, PendingIntent.FLAG_UPDATE_CURRENT);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(calendar.getTimeInMillis(), pendingIntent);
                am.setAlarmClock(alarmClockInfo, pendingIntent);
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                am.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
            } else
                am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

但是钢铁我也有同样的问题.它不会每天重复.还请考虑这只是我的默认警报的示例.用户指定的警报总数为N.

But steel i am having same issue. It is not repeating daily. Please also consider this is only the example of my default alarm. There can be N no of exact and repeating alarms as user specified.

有人还建议我使用服务而不是警报管理器,但是我的服务可能会被杀死,因此我害怕使用它.另一种解决方案是检查到期日并每天设置所有警报,但是我认为这不是正确的方法.是吗?

Someone also suggest me to use service rather than alarm manager, but my service might be killed so i am afraid to use that. One other solution is to check due date and set all alarm daily, but i dont think this is the right way. Is it?

请向我建议其他任何方法,或帮助我改善代码.我非常需要这个来工作.谢谢大家.我的测试设备是HTC One X(4.3),Redmi Prime(6.0)和Mi4i(5.0.2)

Please suggest me any other method or help me to improve my code. I badly need this to work. Thanx all. My testing devices are HTC One X (4.3), Redmi Prime(6.0) and Mi4i (5.0.2)

推荐答案

设置重复警报时遇到很多问题,但最终这段代码在我所有的测试设备上都有效,也许有帮助:

I had many problems setting repeating alarms, but in the end this code was working on all my testing devices, maybe this helps:

    // set time
    Calendar c = Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());
    c.set(Calendar.HOUR_OF_DAY, 8);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);

    long startUpTime = c.getTimeInMillis();

    // startupTime + 24 hours if alarm is in past
    if (System.currentTimeMillis() > startUpTime) {
        startUpTime = startUpTime + 24 * 60 * 60 * 1000;
    }

    // initialize alarm
    AlarmIntent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, alarmIntent, 0);

    manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, startUpTime, 24 * 60 * 60 * 1000, pendingIntent);

这篇关于重复警报不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆