How to set multiple alarms in android - android

I wanted to create a birthday reminder app. I save the name of person , date of birth and a birthday message in the database . I wanted to send the message automatically on that date .
Can anyone suggest an idea to do this. Can shared preference be used here? Can the specific id be passed to the database on a specific date for sending greetings to a person at his DOB.
Can anyone please suggest an idea to handle multiple alarms in this case.

Ignoring the fact that there are multiple apps that can do this already (and as a disclaimer I've released an app which does exactly that) here's how I did it.
Simple Steps:
Set an alarm to wake the device once a day
When you receive an alarm grab a wake-lock
Work out if any birthdays are going to happen today
If so send / queue a message etc
Set the alarm to be tomorrow
You will also need to catch the reboot of the device because any alarms will be destroyed when the device reboots. In which case simply have the intent for BOOT_COMPLETED call into the steps above at number 2.
My alarm code:
long alarmTime = System.currentTimeMillis()+(24*60*60*1000);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
Intent intent = new Intent("<package name>.WAKEUP_ALARM");
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
I actually specify a fixed point in time each day, say about 5am to wake the device and work out what might need to be done that day, but that's relatively easy to deal with (and happens elsewhere in my app for other reasons).
Whilst you could set alarms for all the birthdays over the next year it's a waste of time as all alarms are removed when the device reboots and if the user changes anything you may have to throw the alarm away anyway.
If you really want to pass a database ID through the alarm simply add it to the Intent:
Intent intent = new Intent("<package name>.WAKEUP_ALARM");
intent.putExtra("DatabaseKey", 1);
Sending the message (assuming an SMS message?) automatically requires that you have the SEND_SMS permission and you send an SMS message in the background - like this stackoverflow answer

Related

How to avoid SystemAlarms creeping to my application getNextAlarmClock() of my application

I am building an application where I set Alarms for particular times.For this, I used AlarmClock of Android's to set Alarms.Here I try showing next AlarmClock time on his application to know the very next alarm.
Eg: I set 9:00AM, 6:00PM alarms using alarmManager.setAlarmClock(...) ,My Application shows next Alarm time using alarmManager.getNextAlarmClock() on homescreen saying 9:00AM.
Here is an issue I am facing where when I set some other alarms on my System provided Clocks App and set Alarm for 8:00 AM.This alarm time is getting reflected in my application next alarm time showing that the next time is set for 8:00 Am instead of 9:00 AM., which it is not supposed to.
private String getNextAlarmClock() {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
AlarmManager.AlarmClockInfo alarmClockInfo = alarmManager.getNextAlarmClock();
Log.d(TAG, "time of next alarm in millis" + alarmClockInfo.getTriggerTime());
return alarmClockInfo.getTriggerTime() + "";
}
could anyone please help me in this regard.
This help is Appreciated, please.
Thanks in Advance!
If you call System Alarm manager, then this is common to all the applications in your mobile. And getNextAlarmClock() will give you the next alarm set in your mobile and not by your application. If you want alarm specific to your application, then you have to create this without using Alarm Manager.
If you are ok to get alarmed by system alarm manager and issue is only with display of next alarm time, then i would recommend to use system alarm manager to set time and save this information to DB/shared preference. To display next alarm time, you can fetch saved data from DB instead of taking from alarm manager.

Registering an alarm in Android, where does it go?

I have an Android application which provides users in a particular city info on food and drink deals. I would like to create an alarm so that a user can click "Remind Me" and when a particular deal begins (such as 2pm) the app opens to that specific info.
Here is the code that creates my alarm:
AlarmManager alarmMgr;
PendingIntent alarmIntent;
alarmMgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, ViewListingActivity.class);
alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
alarmMgr.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
Question:
Where in my phone can I see this alarm registered? I looked in the Clock -> Alarm but I do not see it.
I just want to see it so I know it has been setup correctly.
Where in my phone can I see this alarm registered? I looked in the Clock -> Alarm but I do not see it.
I think you've got a wrong idea about the concept of AlarmManager :
AlaramManager is an android system component allowing you (as a developer) to schedule execution of one of the three:
1) start service
2) send broadcast
3) start activity.
one of the best thing is that your app don't have to be running at all. instead, the the OS will wake up your process if your application scheduled with the AlaramManager any alarm for this time, and start the service/activity/broadcast based on when you specified..
Registering an alarm in Android, where does it go?
the pendingIntent object you provided to the alramManager is saved by the system, in order to execute it at the right time.
there is no any system UI component that shows you that visually!! and there is not make sense that there will be such, because - app can schedule alarms for any kind of reasons that most of them not directly been visualized to the user.
for example: my app schedule alram each two hours, in order to update in front of my company servers, the local database of my app. this action don't involved any interaction from the user, and happens completely in background.
after understanding that, I can tell you that there is way to check programatically what scheduled alarms your app have in front of the AlaramManager: follow this answer: How to check if AlarmManager already has an alarm set?
for more information, I strongly recommends you to read the documention of PendingIntent , and AlaramManager:
http://developer.android.com/reference/android/app/AlarmManager.html
http://developer.android.com/reference/android/app/PendingIntent.html
You can do it by using ADB. From the ADB console within Android Studio try the following:
adb shell dumpsys alarm
This should show you all of the alarms that have been set, as well as their details, such as alarm time, interval, etc.

Is there an equivalent solution like iOS's localNotification on Android platform?

My app needs to show notifications to users at certain specified time in the future (maybe months away). In iOS, all I need to do is this:
UILocalNotification* localNotification = [[UILocalNotificationalloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:60];
localNotification.alertBody = #"Your alert message";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
However, in Android, it is much more complicated than I originally expected:
I have to install alarms with Alarm Manager, to start a service at each specified time in future.
These services, in turn, create a notification, and use Notification Manager to push the notification to status bar, etc.
But this solution has problems:
If the device is restarted before the alarm fires, the alarm is lost. I can register for boot-up broadcast like this and re-install the services. However, I really want to avoid installing duplicate alarms for the same event, but it seems there is no way to get currently installed alarms from the Alarm Manager?
The notification is composed at the scheduled time in future (say a month later), not when I set the notification(now). At that time, the required data for the notification may no longer be available. I don't see any way around this, except to store the relevant data, and wait for the alarm to fire.
Is there a pain-free solution to the local notification problem on Android?
If the device is restarted before the alarm fires, the alarm is lost.
I can register for boot-up broadcast like this and re-install the
services. However, I really want to avoid installing duplicate alarms
for the same event, but it seems there is no way to get currently
installed alarms from the Alarm Manager?
How are you going to get duplicated alarms when the device is restarted? When the device is restarted all alarms are canceled so you would do like you said where you start it again in the BroadcastReceiver when the device boots
The notification is composed at the scheduled time in future (say a
month later), not when I set the notification(now). At that time, the
required data for the notification may no longer be available. I don't
see any way around this, except to store the relevant data, and wait
for the alarm to fire.
that is correct you need to store the data that you want to show in the notification, SharedPreferences will probably be the easiest
EDIT:
You do not need to keep a reference to the pending intent all you have to do is create the intent the same way example
when you first created the alarm you used this intent
Intent intent = new Intent(context,MyClass.class);
//any flags or extras
PendingIntent.getBroadcast(context, 0, intent, 0);
Now you want to cancel that alarm so just create the same intent again, it must be a 1 for 1 of the origional
Intent intent = new Intent(context,MyClass.class);
//any flags or extras that you previously had
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
alarmMgr.cancel(pi);

how to show notification on device start up

I am developing notification app in android that shows notification on particular date and time that i set using date picker. It shows correctly if my app is running in background. But when i force stop my app or if device switched off &restarted, notification does not show. How to show notification even if app force closed or device restarted.
You can't. Notifications are attached to application's context. If the application is killed/destroyed, your notification also goes away with it.
What you may do is to re-create those notifications once your application or it's service is started. For that, make sure you do catch android.intent.action.BOOT_COMPLETED broadcast in order to implement this automatically.
You should consider using AlarmManager instead of a service.
Set an alarm at the desired date/time with a custom intent. In the BroadcastReceiver, you create and show the Notification.
If the Device is restarted, you might need also to listen to the BOOT_COMPLETED Intent and reset the alarms.
EDIT:
An example:
long time = // time in milliseconds of when you want your Alarm
PendingIntent mIntent = PendingIntent.getBroadcast(context,
0, new Intent("YOUR_CUSTOM_INTENT"), PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,
time, mIntent );
Then Catch the YOUR_CUSTOM_INTENT intent in a BroadcastReceiver, show the notification and set the following alarm.

how to get notification of Alarm which is passed in between switchoff to switched on mobile

Hi
I am developing a application in which i am using AlarmManager
Problem
When i set pending Intent using Alarm manager on perticular date and time its work fine
but suppose i set alarm time on date 30-05-2011 and time 10:00 AM and suppose current time is date 30-05-2011 and time 09:50 AM
and now after creating pending intent i switched off my device and after 10:01 AM i starts my device
at that time i expect notification for 10:00 AM alarm but i am not getting it
Any idea how i can get notification After switch on my mobile
Through AlarmManager, you can only wake up your device when it is sleeping.
To do that use
setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)
or set(...)
with RTC_WAKEUP or ELAPSED_REALTIME_WAKEUP
But it doesn't work with a device at off.
So you should consider having your alarms in a database, along with the last time your app was on, and count the alarms you missed since last time you were up.
Regards,
Stéphane
If you read the AlarmManager API doc page:
Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.
As an alternative, you can register a broadcast receiver for the intent android.intent.action.BOOT_COMPLETED and check your SharedPreferences if you need to perform an action.
See this question for details about the broadcast: Trying to start a service on boot on Android

Categories

Resources