user3008437
user3008437

Reputation: 41

Multiple alarms in android

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

Upvotes: 0

Views: 1726

Answers (1)

kiran boghra
kiran boghra

Reputation: 3822

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 .

Upvotes: 1

Related Questions