在android中设置重复周日警报

时间:2013-07-27 03:53:53

标签: java android alarmmanager repeatingalarm

有人可以为设定的重复周日警报提供良好的逻辑吗?我已经使用

完成了每周警报
            alarmCalendar.set(Calendar.HOUR, AlarmHrsInInt);
            alarmCalendar.set(Calendar.MINUTE, AlarmMinsInInt);
            alarmCalendar.set(Calendar.SECOND, 0);
            alarmCalendar.set(Calendar.AM_PM, amorpm);

            Long alarmTime = alarmCalendar.getTimeInMillis();

Intent intent = new Intent(Alarm.this, AlarmReciever.class);
                intent.putExtra("keyValue", key);
                PendingIntent pi = PendingIntent.getBroadcast(Alarm.this, key, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 7*1440*60000 , pi); 

警报触发按时触发,7天后会自动触发。

但我的要求是我想选择几天而不是仅仅七天。

类似于每周一,周二,周四上午9:00 - 警报应自动触发。我如何在setRepeating中执行此操作。

有人可以帮我解决这个问题吗?

谢谢!

4 个答案:

答案 0 :(得分:14)

这些问题谈论你想要的同样的事情。这些答案将有所帮助:

您只需指定开始日期,然后每7天重复一次。在给定问题的答案中指定的方法很少:

How can i get the repeat alarm for week days using alarm manager?

Android Notification on specific weekday goes off directly

how to repeat alarm week day on in android

<强>更新

你在评论中说过

  

如何在setRepeating中设置triggerAtMillis部分。比如说今天是星期二,我选择每周一,周三,周五。 - 我星期三放什么?

据我所知,如果今天是星期二,如何设置警报让我们说星期三重复,对吗?首先是的,你可以使用mulltiple id来分别为每一天设置警报。

然后,您可以在现有代码中添加alarmCalendar.set(Calendar.DAY_OF_WEEK, week);行。根据工作日(从1-7开始),它会在当天重复。您可以将其作为参数传递给函数。像:

    setAlarm(2); //set the alarm for this day of the week

    public void setAlarm(int dayOfWeek) {
        // Add this day of the week line to your existing code
        alarmCalendar.set(Calendar.DAY_OF_WEEK, dayOfWeek);

        alarmCalendar.set(Calendar.HOUR, AlarmHrsInInt);
        alarmCalendar.set(Calendar.MINUTE, AlarmMinsInInt);
        alarmCalendar.set(Calendar.SECOND, 0);
        alarmCalendar.set(Calendar.AM_PM, amorpm);

        Long alarmTime = alarmCalendar.getTimeInMillis();
        //Also change the time to 24 hours.
        am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 24 * 60 * 60 * 1000 , pi); 
}

我从上面的一个问题中得到了例子。希望现在更清楚。

答案 1 :(得分:1)

{{1}}

答案 2 :(得分:1)

if (Monday.isChecked()) {
                        setalarm(2);
                    } else if (Tuesday.isChecked()) {
                        setalarm(3);
                    } else if (Wednesday.isChecked()) {
                        setalarm(4);
                    } else if (Thursday.isChecked()) {
                        setalarm(5);
                    } else if (Friday.isChecked()) {
                        setalarm(6);
                    } else if (Saturday.isChecked()) {
                        setalarm(7);
                    } else if (Sunday.isChecked()) {
                        setalarm(1);
                    }

public void setalarm(int weekno) {

        cal.set(Calendar.DAY_OF_WEEK, weekno);
        cal.set(Calendar.MINUTE, min);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,


     cal.getTimeInMillis(), 1 * 60 * 60 * 1000, pendingIntent);
}

答案 3 :(得分:1)

要设置工作日重复警报,请使用以下代码。希望这会有所帮助。

        Calendar calender= Calendar.getInstance();

        calender.set(Calendar.DAY_OF_WEEK, weekNo);  //here pass week number
        calender.set(Calendar.HOUR_OF_DAY, hour);  //pass hour which you have select
        calender.set(Calendar.MINUTE, min);  //pass min which you have select
        calender.set(Calendar.SECOND, 0);
        calender.set(Calendar.MILLISECOND, 0);

        Calendar now = Calendar.getInstance();
        now.set(Calendar.SECOND, 0);
        now.set(Calendar.MILLISECOND, 0);

        if (calender.before(now)) {    //this condition is used for future reminder that means your reminder not fire for past time
            calender.add(Calendar.DATE, 7);
        }

        final int _id = (int) System.currentTimeMillis();  //this id is used to set multiple alarms

        Intent intent = new Intent(activity, YourServiceClass.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, _id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, pendingIntent);