RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Getting Down and Dirty with Google's Android : Page 3

The best way to understand a new SDK is to work with it. With that in mind, we'll show you how to do some basic location based services and simple UI layout using Google's newest upstart mobile offering.

Android in Action
This demonstration application will display the user's current latitude and longitude in text form. The constructor onCreate will be identical to the first listing we covered, except you'll add a bunch of code to the key handler. Now, onKeyDown will look like this:

public boolean onKeyDown(int keyCode, KeyEvent event) 
   	//Head out if we're loading or if it isn't the select key
   	if(keyCode != KeyEvent.KEYCODE_DPAD_CENTER || m_bLoading)
   		return true;
   	//Flip the loading bit so we don't start this again.
   	m_bLoading = true;
   	return true;
Let's break this down. First, the code checks to make sure the correct key has been struck and that it hasn't started this process already, as you only want to call getLocation() once. Then, it will flip the loading flag and call into getLocation(). Here's the getLocation function:

private void getLocation()
	Location loc;
	LocationManager locMan;
	LocationProvider locPro;
	List<LocationProvider> proList;

	//Show "Loading" on the screen.
	//Get the location manager from the server
	locMan = (LocationManager) getSystemService(LOCATION_SERVICE);

 	proList = locMan.getProviders();
	//Just grab the first member of the list. It's name will be "gps"
	locPro = proList.get(0);
	loc = locMan.getCurrentLocation(locPro.getName());

	Lat =  (float)loc.getLatitude();
	Lon =  (float)loc.getLongitude();


This is where things start getting a little more interesting. Sadly, Google's documentation on this subject is not overly helpful. After the variable declarations, you'll want to display some loading information. R.layout.loading corresponds to another simple XML layout View. A simple setContentView() call will paint the screen with the loading message.

Author's Note: At compile time, Android pre-packs all XML layout data. If you want to change layout properties after compile, as it stands now, you'll have to do it programmatically.

The only method for obtaining a LocationManager is through a getSystemService() call. With a LocationManager in hand, it's possible to get a list of location providers. On an actual handset this list could contain several GPS services. In reality, you'd want to be more selective about which provider you use based on power consumption, accuracy, and the availability of additional services. For now, the emulator provides a sample path through San Francisco. Custom GPS files can be uploaded for location based testing, consult the documentation here for more information. While more complicated and nuanced methods exist for testing and emulating location-based services, using the default path through San Francisco will work just fine for the sake of this simple application.

With a location Manager and a location provider, it's now possible to make the actual getCurrentLocation call. This returns a snapshot of the phone's current position in the form of a Location object. With this in hand, you can get the latitude and longitude. Methods for retrieving such information are available in the documentation. Now, with the location of the virtual handset, you can get into the final piece of this sample application: creating and displaying a custom view.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date