36

So far and thanks to this website, I've been able to set up an alarm that will be set up and active, even if I turn of my phone.

Now, I set up a alarm to show a reminder for event A and I need the application to setup another alarm to show another reminder for event B.

I must be doing something wrong, because it only fires the reminder for event A. It seems that once set up, any other alarm is understood as the same one. :-(

Here is the detail of what I am doing in two steps:

1) From an activity I set an alarm that at certain time and date will call a receiver

                Intent intent = new Intent(Activity_Reminder.this,
                        AlarmReceiver_SetOnService.class);

                intent.putExtra("item_name", prescription
                        .getItemName());
                intent
                        .putExtra(
                                "message",
                                Activity_Reminder.this
                                        .getString(R.string.notif_text));
                intent.putExtra("item_id", itemId);
                intent.putExtra("activityToTrigg",
                        "com.companyName.appName.main.Activity_Reminder");

                PendingIntent mAlarmSender;

                mAlarmSender = PendingIntent.getBroadcast(
                        Activity_Reminder.this, 0, intent, 0);

                long alarmTime = dateMgmt.getTimeForAlarm(pickedDate);
                Calendar c = Calendar.getInstance();
                c.setTimeInMillis(alarmTime);
                // Schedule the alarm!
                AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
                am.set(AlarmManager.RTC_WAKEUP, alarmTime + 15000,
                        mAlarmSender);

2) From the receiver I call a service

        Bundle bundle = intent.getExtras();
        String itemName = bundle.getString("item_name");
        String reminderOrAlarmMessage = bundle.getString("message");
        String activityToTrigg = bundle.getString("activityToTrigg");
        int itemId = Integer.parseInt(bundle.getString("item_id"));
        NotificationManager nm = (NotificationManager) context.getSystemService("notification");
        CharSequence text = itemName + " "+reminderOrAlarmMessage;
        Notification notification = new Notification(R.drawable.icon, text,
                System.currentTimeMillis());
        Intent newIntent = new Intent();
        newIntent.setAction(activityToTrigg);
        newIntent.putExtra("item_id", itemId);
        CharSequence text1= itemName + " "+reminderOrAlarmMessage;
        CharSequence text2= context.getString(R.string.notif_Go_To_Details);
        PendingIntent pIntent = PendingIntent.getActivity(context,0, newIntent, 0);
        notification.setLatestEventInfo(context, text1, text2, pIntent);
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notification.defaults = Notification.DEFAULT_ALL;
        nm.notify(itemId, notification);

Thanks in Advance,

monn3t

monn3t
  • 675
  • 1
  • 6
  • 22

3 Answers3

78

Ok, when you set an PendingIntent, you're supposed to assign it a unique ID to it, incase you want to identify it later (for modifying/canceling it)

static PendingIntent    getActivity(Context context, int requestCode, Intent intent, int flags) 
//Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent).
static PendingIntent    getBroadcast(Context context, int requestCode, Intent intent, int flags) 
//Retrieve a PendingIntent that will perform a broadcast, like calling Context.sendBroadcast().

The Request code is that ID.

In your code, you keep resetting the SAME PendingIntent, instead use a different RequestCode each time.

PendingIntent pIntent = PendingIntent.getActivity(context,uniqueRQCODE, newIntent, 0);

It has to be an integer, i suppose you have a primaryid (itemId) that can identify Alarm A from Alarm B.

st0le
  • 33,375
  • 8
  • 89
  • 89
  • Thank you stOle... what you suggested did the trick. I didn't answer before because I wanted to make sure it was working properly and it is... Again thank you, monn3t – monn3t Jul 25 '10 at 02:57
  • @stOle: Please help me for this one also:http://stackoverflow.com/questions/8665021/android-multiple-alarm-not-working/8665978#8665978 – Shreyash Mahajan Dec 29 '11 at 09:39
  • 1
    @st0le: i am able to get the alarm for different date and time but they all get broadcast the same message. How to handle that? I want to set the different message for different alarm. . . – Shreyash Mahajan Dec 29 '11 at 10:34
  • @iDroidExplorer, posted an answer for you. – st0le Dec 29 '11 at 10:37
  • @st0le:I have managed to setup multiple alarm .But how will i cancel these alarms..Could u plzz help me..? – Shahzad Imam Apr 27 '12 at 05:54
  • 2
    @ShahzadImam, See [here](http://stackoverflow.com/questions/3330522/how-to-cancel-this-repeating-alarm) Call the `alarmManager.cancel()` function with a PendingIndent, with Id equal to the id you used to setup the alarm. – st0le Apr 27 '12 at 06:26
  • @st0le:Ok thats it.Then its not a big deal..but can u help here i am facing context problem cause i am not using it inside activity extended class.This line shows error under Alarm_service AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);.....It is defined in other class – Shahzad Imam Apr 27 '12 at 06:53
  • 2
    @ShahzadImam, What class is it? Eithor Way you need a Context to call getSystemService(), Pass a Context Variable and call `context.getSystemService(Context.ALARM_SERVICE)` – st0le Apr 27 '12 at 07:47
  • @st0le API docs say that the requestCode parameter is currently not used, so is it wrong? – Gianni Costanzi Aug 27 '12 at 10:11
  • @st0le Docs say *If the creating application later re-retrieves the same kind of `PendingIntent` (same operation, same Intent action, data, categories, and components, and same flags), it will receive a `PendingIntent` representing the same token if that is still valid, and can thus call `cancel()` to remove it*, so it should not matter which `requestCode` you pass to `getBroadcast()`.. if you use the same `requestCode` but two different `Intents` you should get two different `pendingIntents` not overwrite the same one. At least this is what I've understood on the `PendingIntent` API docs – Gianni Costanzi Aug 27 '12 at 10:21
  • @GianniCostanzi, [see here](http://code.google.com/p/android/issues/detail?id=863) – st0le Aug 27 '12 at 17:57
  • @st0le I see that, but information is not up to date there... So I'm used to specify different values for `requestCode` but I still think that as long as the two intents are not equal (i.e. the action, class, category etc are not all equal) you do not overwrite an existing `pendingIntent`, even if the `requestCode` is the same. Maybe the request code is important in case you want to schedule two pending intents with the same action/category/etc but different extras (which are not taken into account when testing Intent equality). BTW thx for the link – Gianni Costanzi Aug 28 '12 at 20:21
  • @st0le thanks I was building a somewhat similar app. Just in practical situation, how do you suggest to keep track of requestCode? Should I use a counter? –  Aug 21 '17 at 16:04
  • Probably a column in the database. The answer is 7 years old, I've been away from Android for a while now. Sorry can't help more. – st0le Aug 21 '17 at 19:25
1

You can set up multiple alarms by supplying different request code in pendingIntent.getBroadcast(......)

The approach which I used to setup multiple alarm is that I created a single alarm. I initialized a static integer in alarm setting class 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;
//........
Intent myIntent = new Intent(AlarmActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(AlarmActivity.this,
                            broadcastCode, myIntent, 0);
Junaid
  • 3,477
  • 1
  • 24
  • 24
0

For an easier way, if you are listing your Alarms by RecyclerView,

PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), recyclerAdapterAlarm.getItemCount()+1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

It works for me.