0

i just added a for loop for generating multiple request codes to make my multiple alarms work. But still, one alarm is overriding to another. I want to know that where is my mistake. Thanks for your time in advance.

Code is

    private void setAlarm(Calendar targetCal){

AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();

        for(int i = 0; i < 10; ++i)
        {
            Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
        // Loop counter `i` is used as a `requestCode`
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(),
                i, intent, 0);

        alarmManager.set(AlarmManager.RTC_WAKEUP, 
                        targetCal.getTimeInMillis(),    
                pendingIntent); 

        intentArray.add(pendingIntent); 
        textAlarmPrompt.setText( "\n\n***\n"
                + "Alarm is set@" + targetCal.getTimeInMillis() + "\n"
                + "***\n");
        }
}
  • Setting up multiple alarms is not necessary. the earliest alarm set will always be fired before any others so why not just set up the earliest alarm and then in the code that handles the alarm, set up the next earliest alarm. This makes your alarm code so much simpler and negates the need to track multiple alarm id's – Kuffs Oct 12 '15 at 11:51
  • @Kuffs According to your logic, i will have to write a lot of lines of code by defining multiple request codes. Now how will i determine that how many times one user will set alarm ? Or make your suggestion more clear that what actually you are trying to say? – zeeshan haider Oct 12 '15 at 12:55

1 Answers1

0

You are doing it right by using different requestCode, the problem I think it may rely on the context you are using.

Try this:

AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);

and this:

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, intent, 0)

and try to use the right context.

denis_lor
  • 6,212
  • 4
  • 31
  • 55
  • should i define Context context; at global scope? – zeeshan haider Oct 12 '15 at 12:01
  • That's not necessary, you may pass it as parameter at `setAlarm(Context context, Calendar targetCal)`. If you are inside an activity, your context may be `this` or the class name. If you are in a fragment your context can be `getActivity`. – denis_lor Oct 12 '15 at 12:05
  • You can also use your solution, defining `Context context` in the global scope. – denis_lor Oct 12 '15 at 12:08
  • You can get the context by invoking `getApplicationContext()`, `getContext()`, `getBaseContext()` or `this` (when in the activity class). – denis_lor Oct 12 '15 at 12:12
  • No dude . I tried to get context by giving all options like getApplicationContext(), getContext(), getBaseContext() but did not work. Still overriding issue – zeeshan haider Oct 12 '15 at 12:53
  • Is `targetCal.getTimeInMillis()` different in every cycle? Try with logging its value for every cycle or debugging. And then get back here, if they are the same value, you are putting the alarms in the same time. – denis_lor Oct 12 '15 at 12:59
  • Heyy!! Something is going wrong! When i set my first alarm like recently, i set alarm at 6:04pm, i get the given line 4 times *** "Alarm is set@ 1444655040000" *** And when i tried to set the another alarm like 6:10pm ,It displayed different line, like above, 4 times. – zeeshan haider Oct 12 '15 at 13:09
  • this time i added the line in my code to view the alarm behaviour textAlarmPrompt.setText( "\n\n***\n" + "Alarm is set@" + targetCal.getTimeInMillis() + "\n" + "***\n"); It shows that the value of first alarm is updated when the second alarm is set. – zeeshan haider Oct 12 '15 at 13:15
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92050/discussion-between-zeeshan-haider-and-denis-lor). – zeeshan haider Oct 12 '15 at 13:17