如何设置闹钟每天重复3次?

How to set an alarm to repeat 3 time every day ?

我想设置一个闹钟,每天早上 7 点、中午 12 点和晚上 10 点检查服务器是否获取新数据。
我试过这种方式:

 public void startAlarm(Context context) {
        AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, ShowNotificationReceiver.class);
        PendingIntent alarmIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, PendingIntent.FLAG_CANCEL_CURRENT);


        Calendar firstTurn = Calendar.getInstance();
        Calendar secondTurn = Calendar.getInstance();
        Calendar thirdTurn = Calendar.getInstance();

        // set times
        firstTurn.set(Calendar.HOUR_OF_DAY,FIRST_TURN_HOUR);
        firstTurn.set(Calendar.MINUTE,FIRST_TURN_MINUTE);
        secondTurn.set(Calendar.HOUR_OF_DAY,SECOND_TURN_HOUR);
        secondTurn.set(Calendar.MINUTE,SECOND_TURN_MINUTE);
        thirdTurn.set(Calendar.HOUR_OF_DAY,THIRD_TURN_HOUR);
        thirdTurn.set(Calendar.MINUTE,THIRD_TURN_MINUTE);

        alarmMgr.cancel(alarmIntent);
        alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstTurn.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
        alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, secondTurn.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
        alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, thirdTurn.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
    }  

如果我评论最后一行 2,它可以正常工作并及时触发,但如果我想要所有这些,它就不起作用。
我的错误在哪里,我该如何解决?

尝试为您的方法创建 3 个不同的 AlarmManager 对象。此外,您可以在 BroadcastReceiver

内的另一个警报结束后触发每个警报

如果您只是想让它每天重复三次,您可以将其注册为每 8 小时触发一次(24 小时除以 8 等于 3)。

alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstTurn.getTimeInMillis(), AlarmMAnager.INTERVAL_HOUR * 8, alarmIntent);

您的代码无法正常工作的原因是对 setInexactRepeating 的任何调用都将取消任何先前具有相同 PendingIntent 的调用。来自 documentation:

If there is already an alarm scheduled for the same IntentSender, it will first be canceled.

如果您想每天执行 3 次该操作,但不是每 8 小时执行一次,您可以使用 AlarmManager.setExact()(即不重复)在第一轮触发一个警报。一旦触发该警报(即在您的接收器中调用 onReceive()),您只需在第二个回合调用 AlarmManager.setExact(),依此类推。

经过一些搜索和更多的代码尝试后,我以这种方式解决了我的问题:

public void startAlarm(Context context) {

        AlarmManager alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, ShowNotificationReceiver.class);

        Calendar firstTurn = Calendar.getInstance();
        Calendar secondTurn = Calendar.getInstance();
        Calendar thirdTurn = Calendar.getInstance();

        // set times
        firstTurn.set(Calendar.HOUR_OF_DAY, FIRST_TURN_HOUR);
        firstTurn.set(Calendar.MINUTE, FIRST_TURN_MINUTE);
        PendingIntent alarmIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, PendingIntent.FLAG_CANCEL_CURRENT);


        secondTurn.set(Calendar.HOUR_OF_DAY, SECOND_TURN_HOUR);
        secondTurn.set(Calendar.MINUTE, SECOND_TURN_MINUTE);
        PendingIntent alarmIntent2 = PendingIntent.getBroadcast(context, REQUEST_CODE2, intent, PendingIntent.FLAG_CANCEL_CURRENT);


        thirdTurn.set(Calendar.HOUR_OF_DAY, THIRD_TURN_HOUR);
        thirdTurn.set(Calendar.MINUTE, THIRD_TURN_MINUTE);
        PendingIntent alarmIntent3 = PendingIntent.getBroadcast(context, REQUEST_CODE3, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        alarmMgr.cancel(alarmIntent);
        alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, firstTurn.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
        alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, secondTurn.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent2);
        alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, thirdTurn.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent3);
    }

this answer

中所述

If you want to set multiple alarms (repeating or single), then you just need to create their PendingIntents with different requestCode. If requestCode is the same, then the new alarm will overwrite the old one.