0

I am doing a project in android and i want to create the alarm for multiple days but the problem is when i select multiple days the alarm would be shown on the latest day only . Here is my attempt

public void onClick(View v) {
            CheckBox box1=(CheckBox)findViewById(R.id.checkBox1);
            CheckBox box2=(CheckBox)findViewById(R.id.checkBox2);       
            CheckBox box3=(CheckBox)findViewById(R.id.checkBox3);       
            CheckBox box4=(CheckBox)findViewById(R.id.checkBox4);       
            CheckBox box5=(CheckBox)findViewById(R.id.checkBox5);       
            CheckBox box6=(CheckBox)findViewById(R.id.checkBox6);       
            CheckBox box7=(CheckBox)findViewById(R.id.checkBox7);       

            /** Getting a reference to TimePicker object available in the MainActivity */
            TimePicker tpTime = (TimePicker) findViewById(R.id.tp_time);
            int hour = tpTime.getCurrentHour();
            int minute = tpTime.getCurrentMinute();

            Calendar c=Calendar.getInstance();
            int month;
            int day;
            int year;

            Date date =new Date();
            c.setTime(date);

            if(box1.isChecked()) {
                /** This intent invokes the activity DemoActivity, which in turn opens the AlertDialog window */
                Intent i = new Intent("com.example.servicealarmdemo2.demoactivity");
                /** Creating a Pending Intent */
                PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
                c.setTime(date);
                c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);


                month=c.get(Calendar.MONTH);
                day=c.get(Calendar.DATE);
                year=c.get(Calendar.YEAR);

                /** Getting a reference to the System Service ALARM_SERVICE */
                AlarmManager alarmManager1 = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
               /** Creating a calendar object corresponding to the date and time set by the user */
                GregorianCalendar calendar = new GregorianCalendar(year,month,day, hour, minute);

                /** Converting the date and time in to milliseconds elapsed since epoch */
                long alarm_time = calendar.getTimeInMillis();

                /** Setting an alarm, which invokes the operation at alart_time */
                alarmManager1.set(AlarmManager.RTC_WAKEUP  , alarm_time , operation);
                alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP,alarm_time,7*24*3600*1000, operation);
            }

            if(box2.isChecked()) {
                Intent i = new Intent("com.example.servicealarmdemo2.demoactivity");
                PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);
                c.setTime(date);
                c.set(Calendar.DAY_OF_WEEK,Calendar.TUESDAY);

                month=c.get(Calendar.MONTH);
                day=c.get(Calendar.DATE);
                year=c.get(Calendar.YEAR);                  
                AlarmManager alarmManager2 = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);
                GregorianCalendar calendar = new GregorianCalendar(year,month,day, hour, minute);
                long alarm_time = calendar.getTimeInMillis();
                alarmManager2.set(AlarmManager.RTC_WAKEUP  , alarm_time , operation);
                alarmManager2.setRepeating(AlarmManager.RTC_WAKEUP,alarm_time,7*24*3600*1000, operation);

            }

Any help would be appreciated

  • I may be missing something, but what is the difference between checking `box1` and `box2`? – Squirrel5853 Dec 03 '13 at 12:21
  • I think [this](http://stackoverflow.com/questions/18545115/setting-multiple-alarms-on-my-alarm-application) may answer your question. Using the same `PendingIntent` will replace the alarm. Its also mentioned in the [documentation](https://developer.android.com/reference/android/content/Intent.html#filterEquals%28android.content.Intent%29) – Squirrel5853 Dec 03 '13 at 12:29
  • checkboxes are for the days my application checks the days and create the alarms for the selected days – user3008437 Dec 03 '13 at 12:32
  • well I would move out what is common between the parts which handle when box(x) is checked and box(y) which, from what I can see, is most of your code :) – Squirrel5853 Dec 03 '13 at 12:34
  • 1
    Possible duplicate of [How can I setup multiple alarms in Android?](http://stackoverflow.com/questions/3273342/how-can-i-setup-multiple-alarms-in-android) – Darpan Jan 13 '16 at 13:59

1 Answers1

1

Here in your code PendingIntent

PendingIntent pi = PendingIntent.getActivity(context, **AlarmID**,
                    intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);

// for trigger alarm on day 
 Calendar calendar = Calendar.getInstance();

            calendar.set(Calendar.DAY_OF_WEEK,alarmday);
            calendar.set(Calendar.HOUR, hour);
            calendar.set(Calendar.MINUTE,minute );
            calendar.set(Calendar.AM_PM,am-pm);
            calendar.set(Calendar.SECOND, 0);


                am.setRepeating(AlarmManager.RTC_WAKEUP,
                    calendar.getTimeInMillis(),604800000, pi);

you are using same AlarmID for all so previous alrm is override us diffrent alarmId for diffrent .

kiran boghra
  • 3,662
  • 2
  • 18
  • 23
  • Thank you i have changed it it is now partially working but still have a bug when selecting a day before the current day I receive the alert directly.So if tdy was tuesday and i set the alarm on Monday I would be receiving the alarm directly.Any idea about this issue?So should I compare the chosen day with the current day? – user3008437 Dec 03 '13 at 12:49
  • 1
    Yes. An alarm triggered before now would natuarally be expected to trigger directly. So you must verify that any new alarms are created for the future. – Lmickos Dec 03 '13 at 13:08
  • Yes Lmickos is right if alarm is on past time that is trigger at the time of setAlarm so make sure your alarm is set for futuere time .. – kiran boghra Dec 04 '13 at 06:20
  • well I managed to solve the problem by comparing the current day in ms with the alarm set on a specified calendar and create the alarm when the alarm set at a certain time greater than the current time.Thank you for your reply :) – user3008437 Dec 04 '13 at 08:24