简体   繁体   English

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

[英]How to set an alarm to repeat 3 time every day ?

I want to set an alarm to check the server for getting new data everyday at 7 AM , 12 AM and 10 PM. 我想设置一个警报,以每天7 AM,12 AM和10 PM检查服务器是否有新数据。
I tried this way : 我尝试过这种方式:

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

If i comment 2 last line its work correctly and fire in time , but it doesnt work if i want all of them . 如果我在最后2条评论中,它的工作正常并且及时进行,但是如果我要他们全部的话它是行不通的。
Where is my mistake and how can i fix this ? 我的错误在哪里,我该如何解决?

If you simply want it to repeat three times a day you can register it to trigger every 8th hour (As 24 hours divided by 8 is 3). 如果您只是希望它每天重复三次,则可以将其注册为每8小时触发一次(24小时除以8为3)。

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

The reason your code isn't working is because any call to setInexactRepeating will cancel any previous call with an identical PendingIntent. 您的代码无法正常运行的原因是,对setInexactRepeating的任何调用都会取消之前具有相同PendingIntent的所有调用。 From the documentation : 文档中

If there is already an alarm scheduled for the same IntentSender, it will first be canceled. 如果已经为同一IntentSender安排了警报,则将首先将其取消。

If you want to perform the operation three times a day but not at every 8th hour, you could instead trigger one alarm at the first turn using AlarmManager.setExact() (ie, without repeat). 如果要一天执行三次操作,而不是每8小时执​​行一次,则可以改为使用AlarmManager.setExact()在第一轮触发一个警报(即,不重复)。 Once that alarm is triggered (ie, onReceive() is called in your receiver) you simply call AlarmManager.setExact() with the second turn and so on. 触发警报后(即,在接收器中调用onReceive()),只需在第二个回合中调用AlarmManager.setExact(),依此类推。

After some search and playing more with code i resolve my problem in this way : 经过一些搜索并更多地使用代码,我以这种方式解决了我的问题:

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

As explained in this answer 本答案所述

If you want to set multiple alarms (repeating or single), then you just need to create their PendingIntents with different requestCode. 如果要设置多个警报(重复警报或单个警报),则只需使用不同的requestCode创建它们的PendingIntents。 If requestCode is the same, then the new alarm will overwrite the old one. 如果requestCode相同,则新警报将覆盖旧警报。

Try to create 3 different AlarmManager objects for your approach. 尝试为您的方法创建3个不同的AlarmManager对象。 Also, you could instead trigger each alarm after the end of another alarm, inside your BroadcastReceiver 另外,您也可以在BroadcastReceiver内部另一个警报结束后触发每个警报

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

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