how to repeat alarm week day on in android
Asked Answered
F

3

7

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.

Fete answered 20/9, 2012 at 7:29 Comment(1)
Another approach: Create 5 alarms (one for each day) and set repeating time to WEEK.Pteridophyte
F
20

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);
    }
Fete answered 20/9, 2012 at 11:14 Comment(7)
it won't work if user want to set Alarm for more than one day.Kylie
its not complete but demo for @user1662296 some changing is depend of user.Fete
i agree with @Kylie this wont work if the user sets alarm for multiple day of week this will only work on singlechoiceitemsCollative
remove "if{}else if{}.."just use "if{} if{}..." then it should set alarm for multiple days....:)Deviltry
Why do we set this to repeat every hour?Burkle
You are repeating it every hour. For every week formula would be 24 * 7 * 60 * 60 * 1000Rett
why don't you add all of your check boxes to an arrayOf checkboxes and then using a for loop check for the checked ones and save their index to an other array and after that use an other for loop to set alarms for every number in that arraySpile
K
4

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
  }
Kylie answered 20/9, 2012 at 8:3 Comment(7)
can you give one example pleaseFete
@user1662296, i have already coded for you, just study it and implement it your self.Kylie
thank you very much..but one more thing. i have 7 checkbox of week day,suppose i check 3 checkbox like monday,tuesday,friday so get alert only 3 days. can you suggest me pleaseFete
suggest you what ? ask your question briefly please.Kylie
suppose i have 7 check box like monday to sunday. i will check only 3 check box like monday , friday , sunday then get alarm play only those 3 day which is already check .alarm only play monday,friday, sunday .give any exampleFete
@ckpatel how u implemented your scenerio can u pls share i also stuck here suppose i have 7 check box like monday to sunday. i will check only 3 check box like monday , friday , sunday then get alarm play only those 3 day which is already check .alarm only play monday,friday, sunday .give any exampleAthelstan
You are repeating it every hour. For every week formula would be 24 * 7 * 60 * 60 * 1000Rett
A
2

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.

Adrea answered 20/9, 2012 at 7:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.