Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I setup multiple alarms in Android?

Tags:

android

alarms

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

like image 858
monn3t Avatar asked Jul 17 '10 21:07

monn3t


People also ask

Can I set multiple alarms at once?

The impact of your morning alarms can be far-reaching, with the general consensus being it's not a good idea to set multiple alarms. How many alarms should you set? The answer is just one, because setting multiple alarms to wake up may actually be harmful to your health.

Can I set two alarms on my phone?

For Android devices, the built-in Clock app can schedule one-time alarms and weekly repeating alarms. It's possible to create multiple alarms and turn them on or off individually.

Is there an alarm clock that you can set multiple alarms?

The Jall Wooden Digital Alarm Clock looks great and has everything you need in an alarm clock. It's simple to set, read and use, and can wake you dependably with multiple alarms.


2 Answers

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.

like image 93
st0le Avatar answered Sep 23 '22 20:09

st0le


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); 
like image 29
Junaid Avatar answered Sep 23 '22 20:09

Junaid