19

有人可以为设置重复的星期几警报提供良好的逻辑吗?我已经通过使用每周警报

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 天后自动触发。

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

就像每周一、周二、周四上午 9:00 一样 - 警报应该会自动触发。我该如何在 setRepeating 中执行此操作?

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

谢谢!

4

4 回答 4

18

这些问题与您讨论的内容相同。这些答案会有所帮助:

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

如何使用警报管理器获得工作日的重复警报?

特定工作日的 Android 通知直接关闭

如何在android中重复闹钟工作日

更新:

在你的评论中你说

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

我的理解是,如果今天是星期二,如何设置闹钟让我们说星期三重复,对吧?首先是的,您可以使用多个 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); 
}

我从上述问题之一中举了一个例子。希望现在更清楚。

于 2013-07-27T04:23:18.340 回答
6

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

        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);
于 2018-07-30T05:47:28.433 回答
4
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);
}
于 2017-07-27T13:12:41.820 回答
2
Intent intent1 = new Intent(getApplicationContext(),
                            NotificationReceiver.class);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(getApplicationContext(),
                                                          1,
                                                          intent1,
                                                          PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager1 = (AlarmManager)getSystemService(ALARM_SERVICE);
java.util.Calendar calendar1 = java.util.Calendar.getInstance();

calendar1.set(java.util.Calendar.DAY_OF_WEEK,
              Calendar.MONDAY);
calendar1.set(java.util.Calendar.HOUR_OF_DAY,
              22);
calendar1.set(java.util.Calendar.MINUTE,
              8);
calendar1.set(java.util.Calendar.SECOND,
              0);

alarmManager1.setExact(AlarmManager.RTC, calendar1.getTimeInMillis(), pendingIntent1);
于 2017-01-29T11:47:31.943 回答