0

I am trying to make an alarm application that takes multiple "n" time inputs from the user and rings at those particular times. For that I have created an array of EditTexts for both the hour and minute entries. In the onclick listener for the start button, I wish to have alarm managers for all the entered time inputs initialized.

After creating an array of calendar, should i create an array of intents and pending intents and alarm manager objects also--one each for each calendar object?

This is the code for my start button:

public void setAlert(View view) {

    int length = editHour.length;
    int h[] = new int[length];
    int m[] = new int[length];
    Calendar[] cal = new Calendar[length];
    for (int i = 0; i < length; i++) {
        cal[i] = Calendar.getInstance();
    }
    for (int i = 0; i < length; i++) {
        try {

            m[i] = Integer.parseInt(editHour[i].getText().toString());

            h[i] = Integer.parseInt(editMinute[i].getText().toString());

            cal[i].set(Calendar.HOUR, h[i]);
            cal[i].set(Calendar.MINUTE, m[i]);

            Intent intent = new Intent(this, AlarmService.class);
            PendingIntent pendingIntent = PendingIntent.getActivity(this,
                    12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP,
                    cal[i].getTimeInMillis(), pendingIntent);
            Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();

        } catch (Exception ex) {
            ex.printStackTrace();
            System.out.print("OOPS!");

        }
    }
}
tanvi
  • 927
  • 5
  • 17
  • 32
  • you don't need to save the pending intens in an object, just set the alarm. what is your question? does it work or not? – Ran Jul 06 '12 at 11:03
  • this code does not work...no alarms are being generated...my question is taht...if i have a total of 5 times....how do i call my AlarmService class 5 times...I will need to have 5 calendar objects....then do i need 5 differenct alarmmanager objects, with 5 different intents and pending intents? – tanvi Jul 06 '12 at 13:36

2 Answers2

1

your request code is unique and this is your problem. you must use unique code for your different alarms. your code with one request code override alarm time. (sorry for my bad english, it is not my language!)

use this code:

for (int i = 0; i < length; i++) {
    .
    .
    .

    int requestCode = (int) (System.currentTimeMillis());
    PendingIntent pendingIntent = PendingIntent.getActivity(this,
                requestCode, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    Thread.sleep(10);

    .
    .
    .
}
Muhammad Ali
  • 720
  • 8
  • 21
0

If you want 5 different alarms then your PendingIntent should be different. You can make a PendingIntent unique by putting an extra in it. but anyway, your code should at least generate one alarm. Verify that the time you are setting in the calendar is correct.

Anyway, you don't need 5 separate calendars, intents or anything. just update them and set the alarm. you definitively don't need to save them in an array.

Ran
  • 4,117
  • 4
  • 44
  • 70
  • I do not wish to update the calendar objects, as I want to store the sequence entered by the user. And, I will be taking all the entries at the same time by the user and then, one start button will be used to set all of the alarms at the same time. Can that be done without using the calendar array? – tanvi Jul 06 '12 at 14:56