| Reply | « Previous Thread | Next Thread » |
|
i have these simple midlet that records the coordinates in a text file. when i save the file, the gps connection terminates.. how do i maintain the connection even after it saves?
Code:
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.location.Coordinates;
import javax.microedition.location.Criteria;
import javax.microedition.location.Location;
import javax.microedition.location.LocationListener;
import javax.microedition.location.LocationProvider;
import javax.microedition.midlet.*;
public class Test extends MIDlet implements CommandListener, LocationListener {
private Form form;
private Command save;
private Command exit;
private FileHandler fh;
private Thread saveThread;
private String coords = "";
public Test() {
}
protected void startApp() throws MIDletStateChangeException {
buildGUI();
getDisplay().setCurrent(form);
connectToGPS();
fh = new FileHandler(getDisplay());
}
protected void pauseApp() {
}
protected void destroyApp(boolean b) {
}
public void buildGUI() {
form = new Form("Coordinates");
save = new Command("Save", Command.ITEM, 1);
exit = new Command("Exit", Command.EXIT, 1);
form.addCommand(save);
form.addCommand(exit);
form.setCommandListener(this);
}
public void connectToGPS() {
try {
Criteria cr = new Criteria();
cr.setHorizontalAccuracy(500);
LocationProvider lp = LocationProvider.getInstance(cr);
lp.setLocationListener(this, -1, -1, -1);
form.append("Connecting to satellite...\n");
}
catch (Exception e) {
Alert alert = new Alert("Error");
alert.setString(e.toString());
alert.setTimeout(3000);
}
}
public Display getDisplay() {
return Display.getDisplay(this);
}
public void commandAction(Command cmd, Displayable disp) {
if(cmd == save) {
try {
fh.setCoords(coords);
form.deleteAll();
coords = "";
saveThread = new Thread(fh);
saveThread.start();
} catch (Exception ex) {
Alert alert = new Alert("ERROR");
alert.setString(ex.toString());
alert.setTimeout(3000);
getDisplay().setCurrent(alert, form);
}
}
else if(cmd == exit) {
destroyApp(true);
notifyDestroyed();
}
}
public void locationUpdated(LocationProvider locationProvider, Location location) {
if(location.isValid()) {
try {
Coordinates c = location.getQualifiedCoordinates();
if (c != null) {
String lat = c.getLatitude() + "";
String lon = c.getLongitude() + "";
coords = coords + lat + " | " + lon + "\n";
form.append(lat + " | " + lon + "\n");
}
else {
}
}
catch (Exception e) {
Alert alert = new Alert("ERROR");
alert.setString(e.toString());
alert.setTimeout(3000);
getDisplay().setCurrent(alert, form);
}
}
}
public void providerStateChanged(LocationProvider locationProvider, int i) {
}
}
class FileHandler implements Runnable {
private static String mFileRoot = "file:///E:/tracks/";
private String coords;
private Display display;
private String fileName;
public FileHandler(Display display) {
this.display = display;
fileName = "track.txt";
}
public void save() throws IOException {
FileConnection fc = null;
DataOutputStream dos = null;
try {
// If exists already, first delete file, a little clumsy.
StringBuffer fileURL = new StringBuffer(mFileRoot + fileName);
fc = (FileConnection) Connector.open( fileURL.toString(), Connector.READ_WRITE);
if (fc.exists()) {
fc.delete();
fc.close();
fc = (FileConnection) Connector.open( fileURL.toString(),
Connector.READ_WRITE);
}
fc.create();
dos = new DataOutputStream(fc.openOutputStream());
dos.write(coords.getBytes());
}
catch(Exception e) {
Alert alert = new Alert("SAVE");
alert.setString(e.toString());
alert.setTimeout(3000);
display.setCurrent(alert);
}
finally {
if (dos != null) {
dos.flush();
dos.close();
Alert alert = new Alert("SAVE");
alert.setString("Saving Successfull");
alert.setType(AlertType.INFO);
alert.setTimeout(3000);
display.setCurrent(alert);
}
if (fc != null) fc.close();
}
}
public void setCoords(String s) {
coords = s;
}
public void run() {
try {
save();
}
catch (IOException ex) {
}
}
}
|
|
about locatiolIstener, this is an Interface u have to implement in the class definition...
the callback function of this interface public void locationUpdated(LocationProvider locationProvider, final Location location) will be called every specified interval in the setLocationListener(LocationListener listener, int interval, int timeout, int maxAge)... So if u place interval 5 the locationUpdated will be called every 5 secs with a new Location... the timeout I actually never understood either but the maxAge is how old a Location may be before it gest discarded as a too old (and invalid) Location... anyways the description of methods, parameters, callabacks is all in the API-documentation... |
|
thank you sir for clearing that up! anyone knows the answer to my other question?
|
|
anyone? another question.. how do i know if the gps is connected?
|
|
even though what ur asking is a pretty basic question I never under stood it either on how to know if GPS is connected...
Do u want to know if a GPS-device has been found or if the GPS-device has a fix ? |
|
i am still having this problem.. can anyone help me solve it?
new question: what is i do not use the locationlsitener and get the location in a thread? is this ok? edit: Tiger79 - if the GPS-device has a fix |
| Reply | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|
| Thread | Thread Starter | Forum | Replies | Last Post |
|---|---|---|---|---|
| SPP Connection to GPS receiver - MTU problem | pz1974 | Bluetooth Technology | 3 | 2006-01-19 17:02 |
| Security Exception on a Bluetooth SPP connection between a Nokia 6230 and a GPS | xavoton | Bluetooth Technology | 6 | 2005-12-19 20:12 |
| SPP Connection to GPS receiver - MTU problem | pz1974 | Mobile Java Networking & Messaging & Security | 0 | 2005-12-01 10:11 |
| GPS - Bluetooth connection problem | can_bal99 | Symbian Networking & Messaging | 2 | 2005-08-22 14:11 |
| maintain socket connection | Rx-lee | Mobile Java Networking & Messaging & Security | 0 | 2005-03-31 15:50 |