1

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!");

        }
    }
}
tanvi
  • 927
  • 5
  • 17
  • 32

2 Answers2

1

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

Community
  • 1
  • 1
AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • I have the Receiver defined in the Manifest – tanvi Jul 06 '12 at 18:00
  • i have given more suggestion in the answer, Will u tried them?? – AAnkit Jul 06 '12 at 18:01
  • 1
    U are using getActivity, Try using getReceiver – AAnkit Jul 06 '12 at 18:02
  • I have tried that...doesn't work...and even if that was the problem. at least one alarm should have been ringing. – tanvi Jul 06 '12 at 18:05
  • And i need an activity to be called because i have to put some buttons on the activity also. – tanvi Jul 06 '12 at 18:06
  • that what i am telling, You are missing intent.setAction in your code, from receiver you can always start ur activity, Would you like try that or what ?? – AAnkit Jul 06 '12 at 18:08
  • Okay so you are telling me to create a receiver ... that will receive the alarm .. and then i shall call another activity through the receiver? – tanvi Jul 06 '12 at 18:10
  • 1
    Yes, exactly , and first try for one alarm, then try for multiple. – AAnkit Jul 06 '12 at 18:11
  • My code as it was...without the loops and everything was working for one alarm...with two activities only..With all these loops its not working...That is what led me to think..there is some problem with the added functionality of loops and multiple alarms – tanvi Jul 06 '12 at 18:17
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13530/discussion-between-ankit-and-tanvi) – AAnkit Jul 06 '12 at 18:19
0

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

Nate
  • 401
  • 2
  • 7
  • I have the receiver defined in the manifest. And I am changing the code just as you suggested..but it is still not working . – tanvi Jul 06 '12 at 18:09