1

So I need to create a certain number of alarms. If the user needs 10 alarms at regular intervals throughout the day, how can I efficiently write my code so that I make 10 alarms?

Or, is it possible to overwrite a single alarm multiple times?

This is in regards to Android App Dev.

2 Answers2

1

Is the choice of number of alarms left to the user? If yes,

  • Make a user interface (in your activity) for the user itself to add new alarm or give the number of alarms.
  • Declare an Alarm and instantiate a new object for each user request while maintaining the total number of alarms and change all the time for each accordingly.

// context variable contains your `Context`
    AlarmManager mgrAlarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);
    ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
    int[] TimeForAlarm = new int[UserInput];
    // Set the time for each alarm according to your need and UserInput.
    for(i = 0; i < UserInput; ++i)
    {
       Intent intent = new Intent(context, OnAlarmReceiver.class);
       // Loop counter `i` is used as a `requestCode`
       PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, intent, 0);
       // Single alarms according to time we have in TimeForAlarm.
       mgrAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
                    TimeForAlarm[i], 
                    pendingIntent); 

       intentArray.add(pendingIntent);
    }

This will create 'UserInput' number of alarms as times according to TimeForAlarm array. At the end intentArray will have all the pending intents (if you need them).

Lohit
  • 126
  • 8
  • I'm sorry I didn't make it clear. The user will enter some value, which will in turn create a number of alarms. for example, if the user says he has a meal 4 times a day, then it will create 4 alarms so he can eat on time and not at odd timings. Do you understand what I mean? –  Jun 25 '13 at 07:56
  • In that case, use Alarm Manager with intent for each time of the day. – Lohit Jun 25 '13 at 08:34
  • Yeah. That's what I need to do. But see, how do I know how many intents to make? Like what if he has 6 meals? I need to set 6 intents. i know I can just make like 30 intents and use as many as I need, but that's not an effective way to do it. So I was wondering. :) –  Jun 25 '13 at 14:50
  • Make a loop (variable i) and declare pending intent with request code as loop variable (i) and then set alarm using this intent, time (depending on loop variable i) and alarm manager. Keep these in ArrayList. Also keep the Pending intent as array for later use. [This](http://stackoverflow.com/questions/12785702/android-set-multiple-alarms) will help. – Lohit Jun 26 '13 at 05:40
  • Okay I will look into that! This seems like a good option to follow. Also, if its possible, can you please include some sample code, because I have never used an array/arraylist for AlarmManager or PendingIntent. However I will still google that! –  Jun 26 '13 at 06:02
0

Take the number from user, and make a method setAlaram(int numOfMeals){ } and you can use your formula with this number to set the alarams !

Prachi
  • 3,584
  • 2
  • 24
  • 39