【问题标题】:Repeating Alarm not working重复警报不起作用
【发布时间】:2017-03-08 11:20:07
【问题描述】:

我知道这类问题被问了很多次......但请在否决或标记为重复之前先阅读我的问题。

我已经参考了很多 SO 问题,例如 this 来管理警报,但找不到任何解决方案,所以我在这里。

在我的应用程序中,我想在每天早上 8 点触发重复警报一个,而其他用户指定的警报。

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

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);

实际上问题是长时间睡眠后警报没有触发。如果它使用更改日期和时间进行测试,它会完全触发......但它会在一夜后停止触发......

所以我切换到给定链接中提到的第二个选项。我如下使用 setExact 设置警报,并在每次火灾时重新安排时间。

 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 个。

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

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

【问题讨论】:

    标签: android repeatingalarm


    【解决方案1】:

    我在设置重复警报时遇到了很多问题,但最后这段代码可以在我所有的测试设备上运行,也许这会有所帮助:

        // 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);
    

    【讨论】:

    • 好的...所以 INTERVAL_DAY 有问题对吧?但是如果我想每周或每月重复一次闹钟呢
    • INTERVAL_DAY 不是我和您的代码之间的唯一区别,例如,您可以尝试使用 .setInexactRepeating 而不是 .setRepeating。如果你想每周做一次,你可以将间隔乘以 7(24 * 60 * 60 * 1000 = 1 天,7 * 24 * 60 * 60 * 1000 = 1 周)
    • 但我希望我的闹钟在准确的时间触发,所以我不能使用 setInexactRepeating
    • 您真的需要警报在精确到秒的时间触发吗?因为如果没有,setInexactRepeating 会更好。由于 KitKat,setRepeating 和 setInexactRepeating 是一样的,见here。
    • 一旦你设置了闹钟它应该工作并不断重复,直到重新启动设备。一旦重新启动设备警报管理器会丢失所有警报,因此我们应该在系统启动时再次设置警报(boot_completed_receiver)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-18
    • 2015-10-19
    • 1970-01-01
    相关资源
    最近更新 更多