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)
//Flip the loading bit so we don't start this again.
m_bLoading = 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
private void getLocation()
//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.