Avatar of mhdi
mhdi
 asked on

Android Java - comparing date/time to create a popup.

Hi all,

I want to create a popup alert when the time equals the time stored in a string.

I have the date time in a string like this.
strAlertDate = "14-01-2014 00:45:39";

Open in new window


I then have a timer running which does the comparison.
private Runnable updateTimerThread = new Runnable() {
	public void run() {
		
		SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
		String currentDateandTime = sdf.format(new Date());
		
		if(currentDateandTime.equals(strAlertDate)) {
			alertMessge();
		}
		customHandler.postDelayed(this, 0);
	}
};

Open in new window


However this doesn't work. Nothing happens.

Could it be something to do with timezones? Is there a better way?
AndroidJava

Avatar of undefined
Last Comment
krakatoa

8/22/2022 - Mon
krakatoa

Although I don't do Android, it's extremely likely that your Timer resolution isn't right. Your code would certainly work. I ran it with a fixed rate schedule of 1000ms Period, and it was ok. I used a Timer handled by a separate class extending TimerTask.
mhdi

ASKER
Is there a better way?

The time/date of the alert changes based on other factors. I need to have an alarm go off if the time ever reaches the alert time/date.

Rather then run a timer constantly comparing like above, can I set an alarm in the android system? I would need the ability to remove and recreate the alarm if the alarm time changes before the time is reached.
krakatoa

I don't know if there are alarms you can set in Android. Nearly all I know is that a separate thread timer does work - cf: your comment that "nothing happens".

A separate thread has quite low running costs, at least on desktop Java.

So your requirement actually seems to include a dynamic alarm deadline, which might be redefined before the current alarm deadline is reached - is that right? That anyway sounds like a job for a separate thread responsible for updating the alarm params.

(From where is the alarm deadline issued?)
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
mhdi

ASKER
So your requirement actually seems to include a dynamic alarm deadline, which might be redefined before the current alarm deadline is reached - is that right?
That is correct.
(From where is the alarm deadline issued?)
The time and date comes from a webservice. The web service is automatically called every 10 minutes. Occasionally (every few hours) the alarm deadline changes.
ASKER CERTIFIED SOLUTION
krakatoa

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.