Solved

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

Posted on 2014-01-13
5
489 Views
Last Modified: 2014-01-21
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?
0
Comment
Question by:mhdi
  • 3
  • 2
5 Comments
 
LVL 16

Expert Comment

by:krakatoa
ID: 39778712
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.
0
 

Author Comment

by:mhdi
ID: 39781485
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.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 39781860
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?)
0
 

Author Comment

by:mhdi
ID: 39781943
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.
0
 
LVL 16

Accepted Solution

by:
krakatoa earned 500 total points
ID: 39783153
So these two pieces of code should simulate something like your requirements. This one:

import java.text.*;
import java.util.*;

class Tim extends TimerTask {



String currentDateandTime;
public static Date alarmTime;
SimpleDateFormat  sdf ;

	public Tim(){

		
		
		Timer t= new Timer();
		t.scheduleAtFixedRate(this,0,1000);
 
	}


	public void run(){

		sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");
		currentDateandTime = sdf.format(new Date());

		System.out.println(currentDateandTime);
	
		
		try{
		if(currentDateandTime.equals((new SimpleDateFormat("dd-MM-yyyy HH:mm")).format(alarmTime))){
		

			System.out.println(currentDateandTime);
			
			System.exit(0);
		}	

		System.out.println((new SimpleDateFormat("dd-MM-yyyy HH:mm")).format(alarmTime));

		}catch(Exception er){}

		
	}

}

Open in new window


will set off the alarm (just a sysout here of course), and this one :

import java.util.*;
import java.text.*;

class ArmAlarm {


static GregorianCalendar cal = new GregorianCalendar(2014,1,15,18,0);
static int mins = 0;

   public static void main(String[] args){

		Tim tim = new Tim();

		tim.run();

	while (true){

		try{
			Thread.sleep(1000);

			mins = (int)(Math.random()*60);

			System.out.println(mins);
					
			cal.set(2014, 0, 15, 19, mins);

			tim.alarmTime = cal.getTime();						

		}catch(Exception exx){}

	}

   }

}

Open in new window


serves the time.

(The way I decided was to use a random minute, so eventually the match will be made, so it takes a few iterations).

I'm not saying this is the answer - we're not near that yet I don't think.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 178
login form jsp example 2 37
jsp insert to database example 2 47
Is there a simpler dropbox system? 10 34
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
You should read OS supplied guidelines before developing. I can't stress that enough. The guidelines will help you understand the reasons mobile app developers do what they do.  Apple is very particular when they review appstore submissions.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question