Android Set Multiple Alarms
Asked Answered
B

4

47

I'm trying to implement an Android app that needs to alarm (or to alert) multiple times along the time.

I've already searched, but the nearest I found was a fixed-number of alarms set, and I guess the example didn't work.

What I want to know if there is exists an approach to dynamically set multiple alarms, like an Array of alarms and then to trigger those alarms in their specific timestamps.

Birkenhead answered 8/10, 2012 at 16:31 Comment(1)
Possible duplicate of how to set multiple alarms using android alarm managerStithy
J
104

If you want to set multiple alarms (repeating or single), then you just need to create their PendingIntents with different requestCode. If requestCode is the same, then the new alarm will overwrite the old one.

Here is the code to create multiple single alarms and keep them in ArrayList. I keep PendingIntent's in the array because that's what you need to cancel your alarm.

// context variable contains your `Context`
AlarmManager mgrAlarm = (AlarmManager) context.getSystemService(ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();

for(i = 0; i < 10; ++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 in 1, 2, ..., 10 minutes (in `i` minutes)
   mgrAlarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
                SystemClock.elapsedRealtime() + 60000 * i, 
                pendingIntent); 

   intentArray.add(pendingIntent);
}

Also, see this question: How to set more than one alarms at a time in android?.

Janeth answered 9/10, 2012 at 0:17 Comment(2)
I used above code, the problem is that my receiver class is getting called many times .In Receiver class I am starting a service through which I am fetching the current location and sending to server, so due to this same location is getting inserted in DB many times.Pious
@Nikolai Samteladze Does putting all the alarms in an ArrayList make it easy because it is easier to delete the ArrayList rather than deleting all of the alarms individually? Also, would loading the alarms in a Cursor object (for use say after device BOOT_COMPLETED) be more effective or efficient rather than the "for" loop you use above?Thundershower
R
6

You can set the repetition of the alarm:

in this case:

public void AddAlarm(int requestCode,MutableDateTime dueDate,int repeat) {
        Intent intent = new Intent(context, AlarmReceiver.class);
        intent.putExtra(Constants.RECORD_ID, requestCode);
        intent.putExtra("REPEAT", repeat);
        PendingIntent operation = PendingIntent.getBroadcast(context, requestCode, intent,  PendingIntent.FLAG_ONE_SHOT );
        MutableDateTime due = dueDate.toMutableDateTime();
        switch(repeat){
        case NO_REPEAT:
            due.addMinutes(0);
            break;
        case DAILY:

            due.addDays(1); 
            break;
        case WEEKLY:
            due.addWeeks(1);
            break;
        case MONTHLY:
            due.addMonths(1);
            break;
        case MONTHLY_2:
            due.addWeeks(5);            
            break;
        case YEARLY:
            due.addYears(1);
            break;
        }
        due.add(-(dueDate.getMillis()));
        due.setSecondOfMinute(0);
        dueDate.setSecondOfMinute(0);
        alarm.cancel(operation);
        alarm.set(AlarmManager.RTC_WAKEUP, dueDate.getMillis(), operation);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, dueDate.getMillis(), due.getMillis(), operation);
}
Ripply answered 8/10, 2012 at 16:58 Comment(3)
note that I used joda datetime library for easier date manipulation in case you don't knowRipply
I noticed :). Thanks about the tip and the answer. Check the toggled answer i chose to see what i was lookin for. But i thanked you also ;)Birkenhead
@Ripply what is MONTHLY_2 by the wayDevlen
R
2

To dynamically set up multiple alarms, the approach which I used is that I created a single alarm. Then in my alarm setting class, a static integer (to be used as requestcode) is initialized which will be incremented each time from my main activity whenever I click on "add alarm" button in my main activity. E.g.

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void addAlarmClick(View v) {
    AlarmActivity.broadcastCode++;
    startActivity(new Intent(this, AlarmActivity.class));
}
}

AlarmActivity.java

public class AlarmActivity extends AppCompatActivity {`

public static int broadcastCode=0;
/*some code here*/
Intent myIntent = new Intent(AlarmActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this,
                            broadcastCode, myIntent, 0);

I hope this will help.

Rifling answered 4/8, 2015 at 9:11 Comment(0)
J
0

You can set a connected to time requestCode in pendingIntent:

pendingIntent = PendingIntent.getBroadcast(
    this, (calendar.timeInMillis).toInt(), intent, 0
)

Note: your requestCode can also be absolutely random integer

Jackijackie answered 13/3, 2023 at 10:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.