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

时间:2015-07-22 05:25:44

标签: android android-alarms

我想设置一个闹钟来检查服务器是否每天早上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);
    }  

如果我在最后一行评论其工作正常并及时开火,但如果我想要所有这些,它就无法工作。
我的错误在哪里,我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

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

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

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

  

如果已经为同一个IntentSender安排了警报,它将首先被取消。

如果您想每天执行三次操作,而不是每隔8小时执行一次操作,则可以使用AlarmManager.setExact()在第一个转弯处触发一个警报(即不重复)。一旦触发了该警报(即在接收器中调用onReceive()),您只需在第二个转弯时调用AlarmManager.setExact(),依此类推。

答案 1 :(得分:1)

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

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

中所述
  

如果您想设置多个警报(重复或单个),那么您   只需要用不同的requestCode创建他们的PendingIntents。   如果requestCode相同,则新警报将覆盖旧警报   之一。

答案 2 :(得分:0)

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

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