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

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.

Related

Scheduling a notification?

Is there any way to schedule a notification to appear at a specific time?
I am developing an app that reminds a user to change their bandage every 6/12 hours and I wish to send a notification to the user 6/12 hours after they confirm that they have applied the bandage.
Is there any way to do this? I have tried to implement the alarmManager Class however every example I have found uses a specific time of the day as opposed to 6/12 hours after an event.
I have an app which checks whether to show a notification every 24 hours. You can change it to whatever interval you would like.
with(context.getSystemService(Context.ALARM_SERVICE) as AlarmManager) {
// Create a PendingIntent which AlarmManager would raise.
// You should have a BroadcastReceiver to receive the intent and send a push notification.
setInexactRepeating(
AlarmManager.RTC_WAKEUP,
startAt.toEpochSecond() * 1000,
intervalInMillis,
pendingIntent
)
}
startAt -> Epoch when your alarm should start working (preferably in future). If this is in past, it will immediately boradcast the intent.
intervalInMillis -> Interval in milliseconds. For your case this should be 6 hours.
If your app requires different alarms (notifications) at 6 and 12 hours, I would still go with 6 hours or even 3 hours as the interval. When the broadcast is received, you should check if the app is supposed to send a notification or not. If not, don't do anything.
Read more about Scheduling repeating alarms.
Note: AlarmManager is affected by doze mode and you need to reset all your alarms after the phone restarts. Clubbing this with WorkManager would be ideal. But this should get you started.
Schedule tasks with WorkManager
https://developer.android.com/topic/libraries/architecture/workmanager/
Previously firebase-jobdispatcher-android was working fine now google introduced new WorkManager for scheduling task.
Here is a simple implementaion
http://thetechnocafe.com/how-to-use-workmanager-in-android/

How do I make a timer that gives the same time for every user even if they close the app

I need to do a count down timer for 24 hours but the timer needs to give equal time to all users and simultaneous.
Nobody here can really help you too much because the question is very vague and there isn't much for us to work off of, but I'll give answering it a shot and we'll see how that goes.
If I am correct, you're asking how you can make your program show every user the same timer. To do that you're going to need a server with the timer information. Each user's program should send a request for the timer information to the server whenever it needs the information and then your program does what it does with the information.
You don't even really need a timer for that. You just need to post the exact date/time of the event on a web page. In the app, on app start-up, it reads the date of the event from your web page, converts it to a Java Datetime object, then gets the current Datetime and does a date diff compare, to display how long until the event date/time. If you wish, you set a timer in the app, and every second or minute you get the current time again and update the date diff display.

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.

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.

storing the count down timer value in android

As the Count Down timer pauses, how would you store the value?
which then when you want to resume the timer eventually will restart a new timer
with this stored value. I am so confused how to hold the value.
I am a beginner in java and android programming.
could any one guide me.
Android has inbuilt class for count down i.e. CountDownTimer class . You can use this.
http://developer.android.com/reference/android/os/CountDownTimer.html
Countdown timer doesn't have a pause() method. Workaround that i can think off is try getting the time at onTick().
So you have an application displaying count down timr, and like that it continues countdown from very same moment after being paused? In this case you have to save your
remaining time in some location in onPause() method ( best location would be shared preference )
Or do you like to have real countdown time and fire some event after it expires, even if your application is inactive? Then schedule an alarm at desired time, and store scheduled time somewhere (shared preferences would be ideal). In this case you can forget keeping time yourself, just update UI periodically using real time and stored time difference

Categories

Resources