This class will find the value only once, it will not continuously update the location value.
public class GPSDemo extends UiApplication
{ private MainScreen screen;
private static int _interval = 1;
private LocationProvider _locationProvider;
public static void main(String[] args)
{
new GPSDemo().enterEventDispatcher();
}
// Constructor
public GPSDemo() {
screen = new MainScreen();
screen.add(new LabelField(“Finding Location………”));
if ( startLocationUpdate() == false )
{
System.exit(1);
}
pushScreen(screen);
}
/**
* Call the Next Screen As soon the value is retrieved
*/
private void updateLocationScreen(final String msg)
{
invokeAndWait(new Runnable()
{
public void run()
{
//As soon the longitude and latidue is retrieved the Location Update is
//close and new screen is pushed with the value.
if (msg.length()>0) {
close();
UiApplication.getUiApplication().pushScreen(new DisplayScreen(msg));
}
}
});
}
/**
* Invokes the Location API with the default criteria.
*
*/
private boolean startLocationUpdate()
{
boolean retval = false;
try
{
_locationProvider = LocationProvider.getInstance(null);
if ( _locationProvider == null )
{
Runnable showGpsUnsupportedDialog = new Runnable()
{
public void run() {
Dialog.alert(“GPS is not supported on this platform, exiting…”);
System.exit( 1 );
}
};
invokeLater( showGpsUnsupportedDialog ); // Ask event-dispatcher thread to display dialog ASAP.
}
else
{
_locationProvider.setLocationListener(new LocationListenerImpl(), _interval, 1, 1);
retval = true;
}
}
catch (LocationException le)
{
System.err.println(“Failed to instantiate the LocationProvider object, exiting…”);
System.err.println(le);
System.exit(0);
}
return retval;
}
private class LocationListenerImpl implements LocationListener
{
public void locationUpdated(LocationProvider provider, Location location)
{
if(location.isValid())
{
double longitude = location.getQualifiedCoordinates().getLongitude();
double latitude = location.getQualifiedCoordinates().getLatitude();
StringBuffer sb = new StringBuffer();
sb.append(“Longitude: “);
sb.append(longitude);
sb.append(“\n”);
sb.append(“Latitude: “);
sb.append(latitude);
GPSDemo.this.updateLocationScreen(sb.toString());
}
}
public void providerStateChanged(LocationProvider provider, int newState)
{
// Not implemented.
}
}
public void close()
{
if ( _locationProvider != null )
{
_locationProvider.reset();
_locationProvider.setLocationListener(null, -1, -1, -1);
}
}