简体   繁体   English

AlarmManager警报在错误的时间响起

[英]AlarmManager alarm goes off at the wrong time

I am attempting to create a sample alarm application using AlarmManager. 我正在尝试使用AlarmManager创建示例警报应用程序。 However, the alarm goes off at different times even though I have set a specific time for it to be triggered. 但是,即使我设置了触发时间,警报也会在不同的时间响起。

Setting the alarm: 设置闹钟:

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 18); // trigger at 6 PM

        Intent notifyIntent = new Intent(context, CheckupAlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast
                (context, 100, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        android.app.AlarmManager alarmManager = (android.app.AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (alarmManager != null) {
            alarmManager.setInexactRepeating(android.app.AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                    android.app.AlarmManager.INTERVAL_DAY, pendingIntent);
        }

Manifest: 表现:

<receiver
      android:name=".notifications.CheckupAlarmReceiver"
      android:enabled="true"
      android:exported="false" />

To re-iterate, the alarm works fine. 要再次重申,闹铃工作正常。 It just fires at the wrong time. 它只是在错误的时间触发。 Is the Calendar instance not taking the time zone in to consideration? 日历实例是否未考虑时区? What am I doing wrong here? 我在这里做错了什么?

Your are setting setInexactRepeating() which as its name says, it could not be fired at the exact time you specified. 您正在设置setInexactRepeating() ,顾名思义,它无法在您指定的确切时间触发。 This is an optimization so the Operating System tries to get more than one alarm which should fire at similar times to fire all of them at the same time. 这是一项优化,因此操作系统尝试获取多个警报,这些警报应在相似的时间触发,以同时触发所有警报。

Instead, you can use void setExactAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation) . 相反,您可以使用void setExactAndAllowWhileIdle(int type, long triggerAtMillis, PendingIntent operation) See documentation . 请参阅文档

Example: 例:

 alarmManager.setExactAndAllowWhileIdle(android.app.AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

As its recommended, you shouldn't use a repeating alarm with exact times. 按照其建议,您不应使用具有确切时间的重复警报。 Instead, you can wait until the exact alarm fires and then schedule another alarm for the next day. 相反,您可以等到确切的警报响起后再安排第二天的另一次警报。

For API lower than 23, you can use void setExact(int type, long triggerAtMillis, PendingIntent operation) . 对于低于23的API,可以使用void setExact(int type, long triggerAtMillis, PendingIntent operation) And for API lower than 19, you can use void setRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation) with the expected exact firing as always. 对于低于19的API,您可以void setRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)使用void setRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)与预期的精确触发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM