AlarmManager to launch at certain time and repeating - java

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.

Related

Android calendar provider schedules clock alarm automatically

I am making an activity which gets the next alarm clock with the method getNextAlarmClock(). It works perfectly and shows as expected the earlier alarm clock set in the system (in my case from the stock MIUI clock app).
Now comes the problem:
I noticed that if I set the alarm clock for the next day that the alarm is set for today's midnight. But I don't have any alarm set before tomorrow, and this repeats every day (one day is midnight, one day is 7:50pm, one day is 11:50 pm) but at that hour nothing happens.
So being totally confused by my phone behavior I connected to adb shell and executed dumpsys alarm command. I found these lines in the output:
Batch{38cd120 num=1 start=298318940 end=298318940 flgs=0x3}:
RTC_WAKEUP #0: Alarm{5d9a2c9 type 0 when 1510599024750 com.android.providers.calendar}
tag=*walarm*:com.android.providers.calendar.SCHEDULE_ALARM
type=0 whenElapsed=+5h12m31s451ms when=2017-11-13 19:50:24
window=0 repeatInterval=0 count=0 flags=0x3
Alarm clock:
triggerTime=2017-11-13 19:50:24
showIntent=null
operation=PendingIntent{db9bdce: PendingIntentRecord{b3ec4ef com.android.providers.calendar broadcastIntent}}
So it seems that the android calendar provider fires an alarm clock every day, but no memos or events are in the calendar.
My questions are:
Why the calendar provider fires this alarm? and why it is an alarm clock instead of a simple alarm?
How can i remove all the future calls to this alarm?

Perform some action when certain time comes

I'm building an Android organizer app.
I have an Event class that has a start time and I want to trigger some action when this time comes. So, how can I constantly compare this start time with current time, to know when it comes?
Thanks!
Instead of constantly checking for current time against scheduleStartTime, you can schedule task to trigger at scheduledStartTime.
You can use either JobScheduler or AlarmManager
https://developer.android.com/reference/android/app/job/JobInfo.Builder.html
https://code.tutsplus.com/tutorials/using-the-jobscheduler-api-on-android-lollipop--cms-23562
You can set when to trigger task by specifying time difference between start time and current time in millisecond.
builder.setPeriodic(5000);
Or you can also use Alarm manager
https://developer.android.com/training/scheduling/alarms.html
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
Hope this help
You could use one of the following solutions :
Have a time and on Timer tick, you could compare your start time against the current time and trigger you action if needed.
Or, you could use a ScheduledExecutorService to schedule the task you want to archive x seconds in the future base on the current time and the start time of your Event
As you are on Android, I would recommend to do this as part of a Service.
Hope this will help.

How does Android handle alarms set for the past

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.

Android setting alarm to a past date

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.

Alarm manager Calendar

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

Categories

Resources