Peter Kiers
asked on
execute a methode if a certain time is reached
Hi,
I have code that I have put in the code-section. This code
get the GPS-coordinates when the application starts up.
What I would like instead of get the GPS-coordinates
at the startup of the application. I would like is to get
the GPS-coordinates at a certain time. F.e: If in the code
is programmed the time: "14:40". Then when the time is
reached then get the GPS-coordinates. How can I do that?
Greetings,
Peter Kiers
I have code that I have put in the code-section. This code
get the GPS-coordinates when the application starts up.
What I would like instead of get the GPS-coordinates
at the startup of the application. I would like is to get
the GPS-coordinates at a certain time. F.e: If in the code
is programmed the time: "14:40". Then when the time is
reached then get the GPS-coordinates. How can I do that?
Greetings,
Peter Kiers
package mypackage;
import java.util.*;
import javax.microedition.location.*;
import net.rim.device.api.command.*;
import net.rim.device.api.gps.*;
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.util.*;
public class DMLocator extends UiApplication {
private static final int GRADEinterval = 5;
private static final long ID = 0x5d459971bb15ae7aL;
private static int interval = 1;
private static Vector previousPoints;
private static float[] altitudes;
private static float[] horizontalDistances;
private static PersistentObject store;
static
{
store = PersistentStore.getPersistentObject(ID);
if(store.getContents() == null)
{
previousPoints = new Vector();
store.setContents(previousPoints);
}
previousPoints = (Vector) store.getContents();
}
private long startTime;
private float wayHorizontalDistance;
private float horizontalDistance;
private float verticalDistance;
private double latitude;
private double longitude;
private EditField status;
private LocationProvider locationProvider;
private GPSDemoScreen screen;
//*---------------------------------------------------------------*//
public static void main(String[] args)
{
new DMLocator().enterEventDispatcher();
}
//*---------------------------------------------------------------*//
public DMLocator()
{
startTime = System.currentTimeMillis();
altitudes = new float[GRADEinterval];
horizontalDistances = new float[GRADEinterval];
screen = new GPSDemoScreen();
screen.setTitle("DMLocator");
status = new EditField(Field.NON_FOCUSABLE);
screen.add(status);
if(startLocationUpdate())
{
screen.setState(locationProvider.getState());
}
pushScreen(screen);
}
//*---------------------------------------------------------------*//
private void updateLocationScreen(final String msg)
{
invokeLater(new Runnable()
{
public void run()
{
status.setText(msg);
}
});
}
//*---------------------------------------------------------------*//
private boolean startLocationUpdate()
{
boolean returnValue = false;
if(!(GPSInfo.getDefaultGPSMode() == GPSInfo.GPS_MODE_NONE))
{
try
{
Criteria criteria = new Criteria();
criteria.setCostAllowed(false);
locationProvider = LocationProvider.getInstance(criteria);
if(locationProvider != null)
{
locationProvider.setLocationListener(new LocationListenerImpl(), interval, -1, -1);
returnValue = true;
}
else
{
invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("Failed to obtain a location provider, exiting...");
System.exit(0);
}
});
}
}
catch(final LocationException le)
{
invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("Failed to instantiate LocationProvider object, exiting..." + le.toString());
System.exit(0);
}
});
}
}
else
{
invokeLater(new Runnable()
{
public void run()
{
Dialog.alert("GPS is not supported on this device, exiting...");
System.exit(0);
}
});
}
return returnValue;
}
//*---------------------------------------------------------------*//
private void markPoint()
{
long current = System.currentTimeMillis();
WayPoint point = new WayPoint(latitude, longitude, startTime, current, wayHorizontalDistance, verticalDistance);
addWayPoint(point);
startTime = current;
wayHorizontalDistance = 0;
verticalDistance = 0;
}
//*---------------------------------------------------------------*//
private void viewPreviousPoints()
{
// PointScreen pointScreen = new PointScreen(previousPoints);
// pushScreen(pointScreen);
}
//*---------------------------------------------------------------*//
private synchronized static void addWayPoint(WayPoint point)
{
previousPoints.addElement(point);
commit();
}
//*---------------------------------------------------------------*//
synchronized static void removeWayPoint(WayPoint point)
{
previousPoints.removeElement(point);
commit();
}
//*---------------------------------------------------------------*//
private static void commit()
{
store.setContents(previousPoints);
store.commit();
}
//*---------------------------------------------------------------*//
private class LocationListenerImpl implements LocationListener
{
public void locationUpdated(LocationProvider provider, Location location)
{
if(location.isValid())
{
float heading = location.getCourse();
longitude = location.getQualifiedCoordinates().getLongitude();
latitude = location.getQualifiedCoordinates().getLatitude();
float altitude = location.getQualifiedCoordinates().getAltitude();
float speed = location.getSpeed();
float horizontalDistance = speed * interval;
horizontalDistance += horizontalDistance;
wayHorizontalDistance += horizontalDistance;
float totalDist = 0;
for(int i = 0; i < GRADEinterval - 1; ++i)
{
altitudes[i] = altitudes[i + 1];
horizontalDistances[i] = horizontalDistances[i + 1];
totalDist = totalDist + horizontalDistances[i];
}
altitudes[GRADEinterval - 1] = altitude;
horizontalDistances[GRADEinterval - 1] = speed * interval;
totalDist = totalDist + horizontalDistances[GRADEinterval - 1];
float grade = (totalDist == 0.0F) ? Float.NaN : ((altitudes[4] - altitudes[0]) * 100 / totalDist);
float altGain = altitudes[GRADEinterval - 1] - altitudes[GRADEinterval - 2];
if(altGain > 0)
{
verticalDistance = verticalDistance + altGain;
}
StringBuffer sb = new StringBuffer();
sb.append("Longitude: ");
sb.append(longitude);
sb.append("\n");
sb.append("Latitude: ");
sb.append(latitude);
sb.append("\n");
sb.append("Altitude: ");
sb.append(altitude);
sb.append(" m");
sb.append("\n");
sb.append("Heading relative to true north: ");
sb.append(heading);
sb.append("\n");
sb.append("Speed : ");
sb.append(speed);
sb.append(" m/s");
sb.append("\n");
sb.append("Grade : ");
if(Float.isNaN(grade))
{
sb.append(" Not available");
}
else
{
sb.append(grade + " %");
}
DMLocator.this.updateLocationScreen(sb.toString());
}
}
//*---------------------------------------------------------------*//
public void providerStateChanged(LocationProvider provider, int newState)
{
if(newState == LocationProvider.TEMPORARILY_UNAVAILABLE)
{
provider.reset();
}
screen.setState(newState);
}
}
//*---------------------------------------------------------------*//
private final class GPSDemoScreen extends MainScreen
{
TextField statusTextField;
GPSDemoScreen()
{
statusTextField = new TextField(Field.NON_FOCUSABLE);
statusTextField.setLabel("GPS Status: ");
add(statusTextField);
RichTextField instructions = new RichTextField("Waiting for location update...", Field.NON_FOCUSABLE);
add(instructions);
MenuItem markWayPoint = new MenuItem(new StringProvider("Mark waypoint"), 0x230010, 0);
markWayPoint.setCommand(new Command(new CommandHandler()
{
public void execute(ReadOnlyCommandMetadata metadata, Object context)
{
DMLocator.this.markPoint();
}
}));
MenuItem viewWayPoints = new MenuItem(new StringProvider("View waypoints"), 0x230020, 1);
viewWayPoints.setCommand(new Command(new CommandHandler()
{
public void execute(ReadOnlyCommandMetadata metadata, Object context)
{
DMLocator.this.viewPreviousPoints();
}
}));
addMenuItem(markWayPoint);
addMenuItem(viewWayPoints);
}
//*---------------------------------------------------------------*//
public void setState(final int newState)
{
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
switch(newState)
{
case LocationProvider.AVAILABLE:
statusTextField.setText("Available");
break;
case LocationProvider.OUT_OF_SERVICE:
statusTextField.setText("Out of Service");
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
statusTextField.setText("Temporarily Unavailable");
break;
}
}
});
}
//*---------------------------------------------------------------*//
public void close()
{
if(locationProvider != null)
{
locationProvider.reset();
locationProvider.setLocationListener(null, -1, -1, -1);
}
super.close();
}
}
//*---------------------------------------------------------------*//
static class WayPoint implements Persistable
{
long startTime;
long endTime;
double latitude;
double longitude;
float distance;
float verticalDistance;
WayPoint(double latitude, double longitude, long startTime, long endTime, float distance, float verticalDistance)
{
startTime = startTime;
endTime = endTime;
distance = distance;
verticalDistance = verticalDistance;
latitude = latitude;
longitude = longitude;
}
//*---------------------------------------------------------------*//
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>The easiest way would be to run a thread.
no ;)
no ;)
Why no?
You want to avoid busy waits in threads - the api already has the means to execute at a specific time without that
Oh ok. That does make sense. What do you mean by "busy waits" though?
ASKER
Greetings, Peer Kiers