how to repeat alarm week day on in android

2019-02-01 15:17发布

I want to get alarm on monday to friday only. my code is here

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

                    }

Have a Idea.

3条回答
家丑人穷心不美
2楼-- · 2019-02-01 15:51

One way is when the alarm notify is receive in broadcast then check the next day if its saturday then set the alarm for monday otherwise just create with adding 1 day.

You need to set new alarm every time for this.

查看更多
Melony?
3楼-- · 2019-02-01 15:53

From your question i believe you want to perform certain activity on daily basis except Saturday, Sunday. So your code is right but you declare it wrong way, make changes as follows and try

declare alarm in OnCreate() method

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

Now your alarm is set to repeat daily, and you need to perform action daily except Sat,Sunday

  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
  }
查看更多
Lonely孤独者°
4楼-- · 2019-02-01 16:04

please try this code. is successfully run in my apps

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);
    }
查看更多
登录 后发表回答