Question about alarm manager
I have this code
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 5);
Integer prof=t.getProfile();
String prof2=prof.toString();
Intent intent = new Intent(this, AlarmActivity.class);
intent.putExtra("prof",(String)prof2);
PendingIntent pendingIntent = PendingIntent.getActivity(this,(int)t.getId(), intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am =
(AlarmManager)getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
pendingIntent);
Its unfinished yet...
I know I can set time of the calendar with cal.set(Calendar.MINUTES,minutes); and same for hours..
But how do I set day? For example - monday?
day_of_week sets it? If so - range is 0-6 or 1-7? And lowest value is monday or sunday?
Also, if Im going to make repeating event (once a week) - should I make new calendar and set day of week/hours/minutes? or should I user getInstance() and change hour/min/day of week?
About alarm manager. When u make an alarm, you give request_code which
should be uniq. If I reboot my phone - does all the request codes stay
in alarm manager on phone? If no - how to make they stay... If yes -
how do I delete unnecesery made ones while testing?
You can also use:
cal.add(Calendar.DATE, 7)
to set a calendars time to one week from the current calendars setting (and subtract and so forth).
I think a link to the docs is probably warranted here:
http://developer.android.com/reference/java/util/Calendar.html
Regarding your second question, no alarm managers do not persist on phone reboot, you have to save them in shared prefs or SQL and then reload the alarms the next time the phone (and your app) restarts...
To cancel an alarm you use alarm.cancel(pendingIntent). The pendingIntents you need to keep track of on your own.
An example of how to set the calendar can be found here. Additionally, the Android Developer's API shows multiple methods on how to set the day
Related
In my application I want to trigger a recurring alarm at about a specified time to check for some conditions and notify the user if necessary.
I'm using the following code to schedule the alarm:
Calendar cal = ...;
...
mAlarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, mAlarmIntent);
I now noticed (because I accidentially had the wrong day in cal) that the alarm would be triggered right away if cal was some date/time in the past.
So let's say it is 2016-09-20 18:00:00 and I schedule the alarm for 2016-09-20 17:00:00. I'd get a notification right away (or a couple of seconds after scheduling the alarm). This does not happen if I schedule the alarm for a future time like 2016-09-20 18:15:00.
So my questions are:
Will Android always catch up on the missed alarm?
The alarm is scheduled to repreat daily. Will it then repeat at 17:00:00 tomorrow or will it be at 18:00:00, because that was when the alarm was last triggered?
Will Android always catch up on the missed alarm?
if the set time is in past then android trigger alarm as soon as possible. check the docs
The alarm is scheduled to repreat daily. Will it then repeat at
17:00:00 tomorrow or will it be at 18:00:00, because that was when the
alarm was last triggered?
Next time it will be triggered on time
Additional Info : if the use clear app data or forced close your app from application manager then alarm won't trigger until user open your app again plus reboot can also cause this.
My app needs to update its data every 24hrs. The user should not be able to access some parts of the app if its out of date.
When the app starts up, I check if the database is out of date. But, the users might keep the app open of in the background for a long time, and I need to alert them when the update is required.
What's the best way to do this? My Initial thought was to use some kind of Handler thread and save a timestamp somewhere every time the app was paused, and then calculate the new time and restart the timer on resumer.
This leaves two questions:
How can I detect whenever my app is paused or resumed, regardless of activity?
And what is the best idiom for a long-running timer in Android? (keeping in mid that it has to be able to modify UI components ie show an alert when he time is up)
You could do that, but you should not, instead store the time stamp in the shared preferences whenever the app get closed, when open again read that value again and calculate the difference by getting the actual timestamp.... then after that update if necessary the lapsed time!
timeStamp is just a long value..
and you can get it by just calling the System.currentTimeMillis()
and for the shared preferences use the Class SharedPreferences
You can have a Service do the query every 24 hours for you and notify the user if it fails. You can use the AlarmManager for that. Eg:
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent m_intent = new Intent(this, YourService.class);
PendingIntent pi = PendingIntent.getService(this, 2, m_intent, 0);
alarm.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000 * 24 * 60 * 60, pi);
The Service can then ask the AlrmManager to call itself after 24 hours again using the above code only.
NOTE: Alarms can be cancelled by the system or the user (force stop). So, this solution comes with its caveats.
Right now, I am setting alarms like this:
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
timedate = cal.getTime();
//convert date to milis
long timeInMilis = (timedate.getTime());
//Set Alarm to Repeat
manager.setRepeating(AlarmManager.RTC_WAKEUP, timeInMilis, interval, pendingIntent);
Unfortunately, the scheduled times for repeating are inexact and I read that they could be inexact for a full interval!
So, I would like to switch over to seting an exact repeating alarm. My device requires a minimum only of the latest API 19/Android 4.4, so I can't use setRepeating.
What can I do instead to set an exact repeating alarm?
Unfortunately, the scheduled times for repeating are inexact and I read that they could be inexact for a full interval!
If your targetSdkVersion is 19 or higher, yes.
What can I do instead to set an exact repeating alarm?
Use setExact() to get control for your initial delay. Then, as part of your work for processing that event, use setExact() to get control at your next desired time. IOW, you do the "repeating" part yourself.
Yes, this is irritating.
It is intentionally irritating, to steer developers to not use exact repeating alarms, as those are worse for the battery than are their inexact brethren. If the user will perceive the inexact behavior and will not appreciate it, feel free to use setExact() to make the alarms occur when the user wants. However, if the user will not perceive the inexact behavior, please use inexact alarms, as the user may notice the battery impact of your exact alarms.
What happens should I add an alarm but set the starting date to a past date?
Does is get executed immediately or is it put in the queue and never executed?
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startDate, repeatingValue, alarmIntent);
From documentation, if the startDate time is in the past, the alarm will be triggered immediately.
If the date is in past then alarm will trigger immediately. However you may try to use setInexactRepeating instead of setRepeating:
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startDate, setInexactRepeating , alarmIntent);
From the setInexactRepeating() docs:
Schedule a repeating alarm that has inexact trigger time requirements;
for example, an alarm that repeats every hour, but not necessarily at
the top of every hour.
Actually AlarmManager works with the current time.
So when you will set past date alarm then AlarmManager will execute
public void onReceive(Context context, Intent intent)
{
}
method.
I think the alarm is set only for hours(at least through the Android user interface), not for a particular day, this way it will start on the exact hour:minute you set.
As i can conclude from my previous experience with AlarmManager. Date that is in the past will trigger alarm immediately.
As far as I can tell, AlarmManager.set will execute now when the time is set to a past time, the documentation says as much. This sentence is missing for AlarmManager.setInexactRepeating , this alarm will not trigger when it's set for a past time, it will trigger at the next interval, starting from the given time.
AlarmManager.set and AlarmManager.setInexactRepeating are both calling setImpl, with the triggertime they got passed as parameter (checked in Android 7.1.2 sources).
--> there's no difference in both methods, if the triggertime is in the past.
I'm developing an app that has to run certain tasks when the user schedules them to be executed. I'm not sure what the following means in the Android docs.
triggerAtMillis: time in milliseconds that the alarm should go off, using the appropriate clock (depending on the alarm type)
This a quote from the set method documentation.
If I want the task to run at, for example, 27.10.2013 18:05, should I use 1382810700000 which is the date in milliseconds, or the time as milliseconds between now and that time?
If u want to create alarm for say for example-27.10.2013 18:05 you can use try something like this :
Calendar cal = Calendar.getInstance();
cal.set(year, month, day, hourOfDay, minute, second);
Alarm Service:
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal .getTimeInMillis(), pendingIntent);
"time in milliseconds that the alarm should go off, using the appropriate clock (depending on the alarm type)."
So that means the date(hours min etc) in miliseconds you want it to run.
This tutorial might help you.