tanvi
tanvi

Reputation: 997

Multiple Alarm-Alarm not ringing

I am trying to make an alarm that would ring at specified n number of times. My code is given below. I am able to get the entries correctly but no alarm rings at all.

public void setAlert(View view) {

    int h[] = new int[TOTAL_ALARMS];
    int m[] = new int[TOTAL_ALARMS];
    Intent intent[] = new Intent[TOTAL_ALARMS];
    PendingIntent pendingIntent[] = new PendingIntent[TOTAL_ALARMS];
    int piID = 12345; 
    Calendar[] cal = new Calendar[TOTAL_ALARMS];
    for (int i = 0; i < TOTAL_ALARMS; i++) {
        cal[i] = Calendar.getInstance();
    }

    for (int i = 0; i < TOTAL_ALARMS; 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[i] = new Intent(this, AlarmService.class);
             pendingIntent[i] = PendingIntent.getActivity(this,
                    piID++, intent[i], PendingIntent.FLAG_CANCEL_CURRENT);
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            alarmManager.set(AlarmManager.RTC_WAKEUP,
                    cal[i].getTimeInMillis(), pendingIntent[i]);
            Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();

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

        }
    }
}

Upvotes: 1

Views: 361

Answers (2)

Nate
Nate

Reputation: 401

Creating your alarms that way will simple replace the old alarm each time because they are being created with the same code. Modifying the request code each time would allow multiple alarms to be created. If you simply added i to your 123 each time through your loop that should suffice.

Also, do you have a receiver class set up along with the receiver defined in your manifest? If not, the alarms would not be received.

An alarm manager example

Upvotes: 0

AAnkit
AAnkit

Reputation: 27549

I just answered same question, You are missing defining Receiver in Manifest withe specific intent.

Answer link

And you must change Second parameter from "123" to random and unique number you can do it by have a int count, and use count ++ in pending intent in place of 123 // fixed

Upvotes: 1

Related Questions