This is a simplified version of the second application using the location listener method. I also tried calling the functions in reverse order with still the same problem that the GPS power consumption does not drop, e.g:
provider.reset();
provider.setLocationListener(null, -1, 0, 0);
Code:
/* Here is sample code which compiles and runs on emulator and E90
* StopGPS() stops the location updates, but if the GPS module
* has a satellite fix, power consumption does not drop...
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.location.*;
import javax.microedition.io.*;
import java.io.*;
public class MyMidlet extends MIDlet implements CommandListener, LocationListener {
Command Exit = new Command("Exit",Command.EXIT,0);
Command StopGPS = new Command("Stop GPS", Command.ITEM, 0);
Command StartGPS = new Command("Start GPS", Command.ITEM, 0);
public Form f=new Form("LocationListener");
public LocationProvider provider;
public Location location;
public QualifiedCoordinates qc;
public void startApp() {
// test whether location API is supported
if (System.getProperty("microedition.location.version") == null) {
destroyApp(true);
notifyDestroyed();
}
f.addCommand(Exit);
f.setCommandListener(this);
Display.getDisplay(this).setCurrent(f);
StartGPS();
}
public void pauseApp() { }
public void destroyApp(boolean destroy) { }
public void commandAction(Command c, Displayable s) {
if (c == Exit) {
destroyApp(true);
notifyDestroyed();
}
if (c == StartGPS) {
StartGPS();
}
if (c == StopGPS) {
StopGPS();
}
}
public void StartGPS() {
try {
provider = LocationProvider.getInstance(null);
provider.setLocationListener(this, -1, 0, 0);
f.removeCommand(StartGPS);
f.addCommand(StopGPS);
f.deleteAll();
f.append("Searching...");
} catch ( SecurityException e ) { // added catch 22/8/2008
f.removeCommand(StopGPS);
f.addCommand(StartGPS);
}catch (Exception e ) {
f.append("Exception in StartGPS()");
}
}
public void StopGPS() {
try {
provider.setLocationListener(null, -1, 0, 0); // here
provider.reset(); // here
f.removeCommand(StopGPS);
f.addCommand(StartGPS);
f.deleteAll();
f.append("Location listener stopped.");
} catch (Exception e ) {
f.append("Exception in StopGPS()");
}
}
public void locationUpdated(final LocationProvider prov, final Location loc) {
new Thread() {
public void run() {
if (loc != null && loc.isValid()) {
qc = loc.getQualifiedCoordinates();
f.deleteAll();
f.append("Latest update:\n");
f.append(" Lat: "+ qc.getLatitude() +"°\n");
f.append(" Lon: "+ qc.getLongitude() +"°\n");
} else {
f.deleteAll();
f.append("Location null or invalid object:\n");
}
}
}.start();
}
public void providerStateChanged(LocationProvider prov, int newState) {
}
}