04-11-2011 08:22 AM
package com.logictreeit.partytemprature;
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.location.Location;
import javax.microedition.location.LocationException;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;
import javax.microedition.location.QualifiedCoordinates;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
public class CurLocationPicker {
private static String longitude;
private static String latitude;
private HomeScreen homeScreen;
private LocationProvider locationProvider;
private Timer timer;
public CurLocationPicker(HomeScreen homeScreen) {
this.homeScreen = homeScreen;
}
private LocationListener locationListener = new LocationListener() {
public void providerStateChanged(LocationProvider provider, int newState) {
}
public void locationUpdated(LocationProvider provider, Location location) {
// final Location myLoc = location;
if(timer!=null ){
timer.cancel();
}
homeScreen.popGpsFetchScreen();
QualifiedCoordinates coordinates = location
.getQualifiedCoordinates();
if(coordinates != null){
latitude = String.valueOf(coordinates.getLatitude());
longitude = String.valueOf(coordinates.getLongitude());
System.out.println("Latiude GPS- " + latitude);
System.out.println("Longitude GPS- " + longitude);
Dialog.alert("Latiude GPS- " + latitude+"Longitude GPS- " + longitude);
}else{
location = LocationProvider.getLastKnownLocation();
coordinates = location.getQualifiedCoordinates();
if(coordinates != null){
latitude = String.valueOf(coordinates.getLatitude());
longitude = String.valueOf(coordinates.getLongitude());
System.out.println("Latiude GPS-Last Known location " + latitude);
System.out.println("Longitude GPS-Last Known location " + longitude);
}else{
Runnable showGpsUnsupportedDialog = new Runnable() {
public void run() {
Dialog.alert("Unable to fetch GPS location, exiting...");
System.exit(1);
}
};
UiApplication.getUiApplication().invokeLater(
showGpsUnsupportedDialog);
}
}
}
};
public boolean startLocationUpdate() {
timer = new Timer();
timer.schedule(new gpsTask(), 15000);
boolean retval = false;
try {
locationProvider = LocationProvider.getInstance(null);
if (locationProvider == null) {
// We would like to display a dialog box indicating that GPS
// isn't supported,
// but because the event-dispatcher thread hasn't been started
// yet, modal
// screens cannot be pushed onto the display stack. So delay
// this operation
// until the event-dispatcher thread is running by asking it to
// invoke the
// following Runnable object as soon as it can.
Runnable showGpsUnsupportedDialog = new Runnable() {
public void run() {
Dialog.alert("GPS is not supported on this platform, exiting...");
System.exit(1);
}
};
UiApplication.getUiApplication().invokeLater(
showGpsUnsupportedDialog); // Ask event-dispatcher
// thread to display dialog
// ASAP.
} else {
// Only a single listener can be associated with a provider, and
// unsetting it
// involves the same call but with null, therefore, no need to
// cache the listener
// instance request an update every second.
locationProvider.setLocationListener(locationListe ner, 1, 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 gpsTask extends TimerTask {
public void run() {
QualifiedCoordinates coordinates = LocationProvider
.getLastKnownLocation().getQualifiedCoordinates();
latitude = String.valueOf(coordinates.getLatitude());
longitude = String.valueOf(coordinates.getLongitude());
homeScreen.popGpsFetchScreen();
System.out.println("LastKnownLatiude - " + latitude);
System.out.println("LastKnownLongitude - " + longitude);
}
}
public static String getLatitude() {
return latitude;
}
public static String getLongitude() {
return longitude;
}
public void stopUpdates() {
if (locationProvider != null) {
locationProvider.reset();
locationProvider.setLocationListener(null, -1, -1, -1);
}
}
}
hiiiiiii in the above program
timer is not working i kept the timer if gps location is not fetched with in that time but here it is not executing the sheduled thread .....
timer = new Timer(); timer.schedule(new gpsTask(), 15000);