Location Polling with LocationListener

Back | Tutorial Home | Next

The above LBS tracking program "Where Am I?" fetches the Location parameters once and the user has to initiate the next fetch. But when your application needs Location information at regular intervals then the Location API provides you with an Interface LocationListener. You can easily convert the "Where Am I " application to a Position polling application with a few changes to your code.

Your application should implement LocationListener Interface to poll the LocationProvider regularly. This interface has two methods which you have to be override.

locationUpdated(LocationProvider provider, Location location) : Called by the LocationProvider to which this listener is registered.
providerStateChanged(LocationProvider provider, int newState) : Called by the LocationProvider to which this listener is registered if the state of the LocationProvider has changed.

The LocationProvider is registered to the LocationListener using method

setLocationListener(LocationListener listener, int interval, int timeout, int maxAge)

Parameter Description

intervel is the time between each Location Position fetch.
timeout is the timeout for each fetch.
maxAge defines how old the location result is for updation. The time stamp of the fetch is campared and the next position fetch happens only if the last position fetch is stale with respect to maxAge.

'-1' is default value for interval, timeout and maxAge
provider.setLocationListener(this, -1, -1, -1);

While the LocationProvider creation can go into the Thread's run method, Each Location fetch code is pushed to locationUpdated() method. This method is called regularly depending on setLocationListener() parameter “interval”.

public void run() {
    	
    //First Tell the user LBS fetch is going to take some time
    Form pleaseWaitScreen = new Form("Please Wait");
    pleaseWaitScreen.append("Processing LBS Request... \n\nPlease Wait");
    display.setCurrent(pleaseWaitScreen);
    	
	// LBS code starts here
    try {
    		
 		Criteria cr = new Criteria();
 		// Required Accuracy is set to 100 Meters 
		cr.setHorizontalAccuracy(100);
		provider = LocationProvider.getInstance(cr);
		// '-1' is default value for intervel, timeout and maxAge
		provider.setLocationListener(this, -1, -1, -1);
			
	} catch (LocationException e) {
			
		e.printStackTrace();
	}
	// LBS code ends here
}
    
public void locationUpdated(LocationProvider arg0, Location arg1) {
	pollNumber = pollNumber + 1;
	try {
	    	String displayString = "";
		Location location;
		location = provider.getLocation(90);
		QualifiedCoordinates c = location.getQualifiedCoordinates();
		double latitude = c.getLatitude();
		double longitude = c.getLongitude();
		float altitude = c.getAltitude();
			
		displayString = "Location has been polled " + pollNumber +" times\n";
		displayString = displayString + "Latidude: " + latitude +"\n";
		displayString = displayString + "Longitude: " + longitude +"\n";
		displayString = displayString + "Altitude: " + altitude +"\n";
			
		// Display results on form
		resultsForm = new Form("Your Location");
		resultsForm.append(displayString);
		cmdExit = new Command("Exit", Command.SCREEN, 0); 
		resultsForm.addCommand(cmdExit);
		display.setCurrent(resultsForm);
		resultsForm.setCommandListener(this);
	} catch (LocationException e) {
		e.printStackTrace();
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
}
	
public void providerStateChanged(LocationProvider arg0, int arg1) {}

LocationListener MIDlet "Location Polling?"

This MIDlet demonstrates the use of LocationListener to get location parameters at regular intervels. The code segments in the above section are from this application. Download the complete application from salsa goodies.

j2meSasa goodies
Download demo code for deploying directly to WTK Click here to download Zip File Containing WTK compatible file structure. This zip file is a MIDlet suite of all the examples in this in this LBS tutorial

Back | Tutorial Home | Next

site comments powered by Disqus

More Tutorials

MIDP Basics
MIDP Gaming

This page is a part of a frames based web site. If you have landed on this page from a search engine click here to view the complete page.