two alarms set at same time - android

in my application users are free to select time and date to set alarms .
What will happen if user choose exactly the same date and time for two alarms.
I am taking input from user(for date and time) and setting the alarm.
GregorianCalendar gc=new GregorianCalendar();
gc.set(2012, 1, 22, 11, 19,0);//values as given by the user
final Intent intent = new Intent(this, AlarmService.class);
gc.set(Calendar.AM_PM,0);
// final PendingIntent sender = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.set(AlarmManager.RTC_WAKEUP, gc.getTimeInMillis(),PendingIntent.getBroadcast(this,1, intent, PendingIntent.FLAG_UPDATE_CURRENT));
I have used a broadcastreceiver to recieve the alarm broadcast.
How should I handle this situation. What should I prefer to do?
I want to know the tecchnical aspect as what happens in this situation.

AFAIK,it won't create any technical problem for alarms set for the same time.It will fire all alarms at the same time.
It depends upon you how you want your user to set alarm.If you want them to set same time for various alarms,it's not a problem.
But if you don't want them to repeat the alarm time once it is set for other,then you can store alarms' time in database and at the time of setting new one,you can check for the conflict of newly set alarm time with previously set alarms from database and reject if it is found same.

Related

Developing reminder application in Android

I'm creating a reminder application that will show notifications on scheduled time, I'll be storing reminder related data in a database and created the working database and content provider for it, initially I thought of creating an alarm whenever user creates a new reminder, but now I'm not able to fire the notification whenever the alarm triggers, and don't know how can I retrieve the reminder related info from the database, and for repeating reminders like daily or weekly I want to reschedule the same alarm after it triggers as the android developer docs suggested so, but don't have the exact idea of it. Please guide me through this which approach will be better and is there any other better way to do it, and any working demo will be very helpful.
This seems like it is quite a few questions bundled up into one. I would suggest searching for each individual piece as all of the answers to your questions are on this site.
In any case, I copied over an answer on how to set a repeating alarm
AlarmManager am = (AlarmManager) ct.getSystemService(Context.ALARM_SERVICE);
Intent intent1 = new Intent(CurrentActivity.this, Alarm.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(ct, 0,intent1, PendingIntent.FLAG_CANCEL_CURRENT);
Date curr=new Date();
curr.setHours(h);
curr.setMinutes(m);
c.setTime(curr);
c.set(Calendar.SECOND, 0);
Calendar c1 = Calendar.getInstance();
am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
With this code an intent will be fired every day that calls Alarm.class. It is up to you to figure out if you need a service, intent, or anything else as Alarm.class, as it is unclear what the alarm needs to do based on your description.

Android setting multiple long date alarms

I'm trying to create a function in my App, which notifies the user at the expiration day of his rented books. I'll work with checkboxes in a listview, as below:
(Dates are for show purposes only)
Now i'm wondering how can i do it the best way. I'm having experiences with AlarmManager and BroadcastReceivers, but I didn't get a clear flowchart yet.
Thats because I need to set an specific alarm to each book and cancel that specific alarm when requested. Also, it needs to reactivate all Alarms when device is restared (by calling BOOT_COMPLETE broadcast).
PS.: Alarms will usually be set to one week after current date.
PS2.: Can I use Calendar to do it? I mean, this way i wouldn't have to reactivate all alarms, or calculate (expirationDate - currentDate) in millis.
Can someone, who has an idea, try to show me the way? Thanks!
I think the key would be to give each and every book its own alarm id as soon as you set the alarm for this book for the first time.
Then you should keep a list of the running alarm ids and timestamps (maybe in SharedPreferences).
With a method like this you can cancel a specific alarm with regards to its alarm id:
public static void cancelAlarm(Context context, int alarmId) {
PendingIntent pi = PendingIntent.getService(context, alarmId,
new Intent(context, YourService.class),
PendingIntent.FLAG_NO_CREATE);
if(pi!=null) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.cancel(pi);
}
}
When you receive the BOOT broadcast, you can get the list of alarm ids together with timestamps from SharedPreferences and start all the alarms with their respective alarm ids

set date and time from respective pickers to set alarm

I preparing an app like "To-Do List" in which user sets alarm to its task. Now the problem is that i don't know how to set alarm using date and time pickers.
The working i need is that user first selects date and then time. This date and time is to be used to set Notification on this specified date and time. And after clicking on that notification, the activity that contains whole to do list starts. In that activity, once the to do task is marked finish, the alarm must be removed from that specific date and time.
Please someone help me. I am trying this for the first time. Please help me learning this.
PS: The work i've done till now is, i have the prepared an activity that takes necessary details about task such as task brief, task priority, task date task time, and i am storing it in database. I can successfully mark the task done / undone. The work i'm remaining with is setting the Notification about that task.
find the difference between the current time and the time at which the work id to be done in milliseconds and set it to alarm.
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * diffInMillis , pi);
Hope it helps

about alarm manager in android

I have made an alarm manager to schedule some user defined events and it is working successfully
then I have made a setting screen (from preference activity) to let the user to change some setting like (days before alarm)
my question is If I schedule the event before event start date by 3 days, then the user change the days before from setting to one day only
then I schedule the event again before event start date by 1 day, is that mean the user will be notified twice
one before 3 days
one before 1 day
if that is true so how can I prevent that from happening
Thanks in Advance
Each alarm is accompanied by a PendingIntent which has a unique hash identifier. When using the same identifier, that alarm will overwrite the former as stated in the documentation:
http://developer.android.com/reference/android/app/AlarmManager.html#set(int, long, android.app.PendingIntent)
Schedule an alarm. Note: ...If there is already an alarm scheduled for the same IntentSender, it will first be canceled...
Note that PendingIntent are characterized by their parameters and identifiers.
Intent intent = new Intent(mContext, YourTarget.class);
// The hashcode works as an identifier here. By using the same, the alarm will be overwrriten
PendingIntent sender = PendingIntent.getBroadcast(mContext, hashCode, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
mAlarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
} else {
mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
}
public void cancel (PendingIntent operation)
Remove any alarms with a matching Intent. Any alarm, of any type, whose Intent
matches this one (as defined by filterEquals(Intent)),
will be canceled.
So you can call cancel on your pending intent alarmManager.cancel(myPendingIntent)
and the create a new one with a new time.
But you dont have to call cancel explicitly because as long as filterEquals returns true when comparing your new PendingIntent with the previous one then your alarm will start only 1 day before.
public boolean filterEquals (Intent other)
Determine if two intents are the same for the purposes of intent resolution
(filtering). That is, if their action, data, type, class,
and categories are the same. This does not compare any extra data
included in the intents.

Best way to set multiple alarms when some are of repeating in nature in Android

I'm building a reminders application where one time, weekly, monthly reminders and we notify the user of the reminder on due date and time. Reminders can be updated any time by the user to update the reminding time or delete the reminder altogether. I have thought of two ways I can solve the particular problem.
Whenever user sets a reminder, schedule an alarm accordingly with an unique Id and update or delete it in case user updates or deletes the alarm.
Since I store the reminding time in DB, better approach would be to schedule an alarm for the nearest reminder. And have the Service which is triggered by the alarm schedule a new alarm for the next nearest reminder.
2nd approach seems clean approach but how do we tackle the case where the Service triggered by alarm gets killed by the system before it schedules a new alarm for the next reminder?
Edit
Looks like if the system kills a Service for memory, it will re-create the Service. Does it mean it is safe to rely on the Service to schedule alarm every time it is run?
Edit 2
I've realized that Android kills any alarms whenever the device is restarted. This makes approach 2 a better solution. I've implemented it for now.
The pending intent needs to be created exactly in the same way for canceling the already set alarm. You can cancel previous alarm when you set a new alarm, if you want to have a single alarm activated at some time.
For setting alarm use:
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, now.getTimeInMillis(), pendingIntent);
For canceling the alarm use:
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent myIntent = new Intent(getApplicationContext(), SessionReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.cancel(pendingIntent);

Categories

Resources