如何在android中重复闹钟周日

时间:2012-09-20 07:29:39

标签: android alarmmanager android-alarms

我想在星期一到星期五闹钟。 我的代码在这里

if (chk_weekday.isChecked()) {

                    int day = calNow.get(Calendar.DAY_OF_WEEK);
                    if (day == 2 || day == 3 || day == 4 || day == 5
                            || day == 6) {

                        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                                calSet.getTimeInMillis(), 1 * 60 * 60 * 1000,
                                pendingIntent);

                    }

有个主意。

3 个答案:

答案 0 :(得分:19)

请尝试此代码。已成功在我的应用中运行

if (chk_monday.isChecked()) {
                        forday(2);
                    } else if (chk_tuesday.isChecked()) {
                        forday(3);
                    } else if (chk_wednesday.isChecked()) {
                        forday(4);
                    } else if (chk_thursday.isChecked()) {
                        forday(5);
                    } else if (chk_friday.isChecked()) {
                        forday(6);
                    } else if (chk_sat.isChecked()) {
                        forday(7);
                    } else if (chk_sunday.isChecked()) {
                        forday(1);
                    }

public void forday(int week) {

        calSet.set(Calendar.DAY_OF_WEEK, week);
        calSet.set(Calendar.HOUR_OF_DAY, hour);
        calSet.set(Calendar.MINUTE, minuts);
        calSet.set(Calendar.SECOND, 0);
        calSet.set(Calendar.MILLISECOND, 0);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                calSet.getTimeInMillis(), 1 * 60 * 60 * 1000, pendingIntent);
    }

答案 1 :(得分:4)

根据您的问题,我相信除了周六,周日外,您每天都要开展某项活动。因此,您的代码是正确的,但您声明错误的方式,进行如下更改并尝试

在OnCreate()方法中声明警报

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), 1 * 60 * 60 * 1000,  pendingIntent);

现在您的闹钟设置为每天重复,您需要每天执行操作,周六,周日除外

  if (chkMonday.isChecked()) 
  {
      activityToPerform();
  }

  if (chkTuesday.isChecked()) 
  {
      activityToPerform();
  }

  if (chkWednesday.isChecked()) 
  {
      activityToPerform();
  }

  if (chkThrusday.isChecked()) 
  {
      activityToPerform();
  }

  if (chkFriday.isChecked()) 
  {
      activityToPerform();
  }

  if (chkSaturday.isChecked()) 
  {
      activityToPerform();
  }

  if (chkSunday.isChecked()) 
  {
      activityToPerform();
  }

  private void activityToPerform()
  {
    // your action code
  }

答案 2 :(得分:2)

一种方法是在广播中接收到警报通知,然后检查第二天是否在星期六,然后将警报设置为星期一,否则只需创建并添加1天。

每次都需要设置新警报。