Perform some action when certain time comes - java

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.

Related

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.

Performing a task everyday using TimerTask doesn't work for the next day.(with other problems)

I have a TimerTask that updates a few tables everyday at specific times(not one task per day, different tasks at different times in a day). I get a few time periods from a database and check if the current time is equal to one of the times in the database when the run() of TimerTask is called and perform the respective task for respective time(time from database which is equal to current time)
I think it's not working for the next day because I set the time(which I obtain from database) for the timer when the service starts. The next day when the timer is supposed to do the task and checks with the current time it checks with the previous day's time(the day service is started and time is set for TimerTask) and does not do the task. I should start the Service everyday to set the time(which is obtained from database to check with current time).
How do I start the service everyday at specific time so that the time to be checked with current time is set everyday.
I can try to schedule to do the tasks everyday 24 hours from the day service is started, but sometimes I have to change the start date and for that the service should be started again and I dont know if a service will run for 24 hours.
You should use Alarmanager for this. The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running.

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.

How to schedule timer for specific date and time in android?

eventPoller.schedule(new EventPoller(adapter),dateOfEvent , 100000);
I want to schedule remainder for particular date and time by using TimerTask. i am doing this without broadcast receiver as I just want to schedule it and show some notification as remainder to user.
so please suggest me, how can I pass the date and time both in this method so that Notification will come on that exact time and date.
is there any other way to remind events to user other than broadcast receiver?????
please suggest any solution asap.............
Use alarmManager.
Keep in mind that you need to set alarmManager every time user restarts phone. This means you need to implement BroadcastReciver and set it there.
If you need an example in can post it for you.

Best class to schedule a task

I'm planning to make an app that enables the Mobile Data of the android every X hours for Y minutes. I was wondering which class should I use to schedule this task. Thanks
For tasks for which the user can give you a specific time and date, you can use AlarmManager to have your task executed at that time.
This way, your app does not need to be running the whole time, and will be launched by Android when the time comes.
You can even use the setRepeating() method to set it only once and have it run everytime.

Categories

Resources