Friday, January 1, 2010

Accessing GPS on Android 2.0 Platform

One of the great things about building applications for an Android device is the ability to create GPS aware applications. In a single hardware platform, using Android, it is possible to create an application that will change behavior based upon real-world coordinates.

The hard part is getting started. At the moment, there are very few tutorials on creating GPS aware Android apps. I've spent the past few days digging through the Android developer sites and other blogs. I finally found the minimal amount of information to create a very simple GPS aware application. This application will list the current longitude and latitude. In addition to the minimal number of steps needed, I will show you how to test the GPS aware app via the Eclipse debugger and ddms tool from the Android SDK.

My setup consists of Ubutnu 9.04 32-bit, Java 1.6.0_17, Android SDK 2.0, and Eclipse build 20090619-0625. The application created will display the current longitude and latitude as a simple ListView.

Steps to create an Android 2.0 application that will display the current longitude and latitude using an Android phone's GPS

1. Open Eclipse and create an Android project.

2. The project you create will be based upon the Andriod ListActivity. Inherit from ListActivity. For purposes of these instructions, I will assume that this new subclass is called MyGps.

3. In your new Android project, open the AndroidManifest.xml file. Add the following entry just after the tag:

android:name="android.permission.ACCESS_FINE_LOCATION" />

This entry is needed to get info from the Android GPS.

4. In the definition of the MyGPS class, you need to define 2 private helper classes inside the MyGPS class. The first private helper class you need to add will implement the GpsStatus.Litener interface.. The second private helper class you need to add will implement the LocationListener interface. For the purposes of these instructions, the helper class that implements the GpsStatus.Listener interface will be callsed MyGpsStatus. The helper class that implements the LocationListener interface will be called MyLocationListener. In MyLocationListener, create a constructor that takes as argument a ListActivity object.

5. In the MyGps class, add these members:

LocationManager myLocationManager;
ArrayList results;
String[] temp;
Criteria myBestCriteria;
String myBestGps;

myLocationManger is an instance of the LocationManager class that is used to check if the GPS feature is available and to set the sensitivity of GPS position changes.

The results ArrayList holds the various info from GPS queries.

The info stored in the ArrayList results is converted into a regular String[] array called temp.

myBestCriteria is an instance of a Criteria class. This instance is used to define the desired features of the GPS. myBestCriteria can be used to define the number of attributes that a given GPS source will provide(such as altitude, speed, bearing, etc). myBestCriteria is also used to define the amount of position change that is considered a 'change'.

The myBestGps is a String object that holds the name of the GPS provider that best meets the myBestCriteria requirements.

6. In the onCreate method of the MyGps class definition, do the following:

a. Initialize the myLocationManager reference with an instance created by the getSystemService method.

b. Initialize the results ArrayList. Add an initial item so that there is at least 1 member of the array is not null.

c. Use addGpsListner to add a GPS listner instance to MyGps. Use the creation of MyGpsStatus object as an argument.

d. Use requestLocationUpdates to define the resolution of GPS change.

e. Instantiate myBestCriteria's Critera object.

f. Initialize the features of the GPS source by using the various methods of the Criteria class.

g. Get the name of the best GPS source by using the getBestProvider method on the myLocationManger object. Use myBestCriteria as an argument.

h. Display the initial contents of the GPS features by using these last lines of code at the end of the onCreate method:

setListAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, temp));

getListView().setTextFilterEnabled(true);

temp must be the String[] version of results.

i. Go to the private class definition of MyLocationListener and implement the onLocationMethod as follows:

temp = new String[ 3 ];
String tempLatitude = String.valueOf(location.getLatitude());
String tempLongitude = String.valueOf(location.getLongitude());
temp[0] = new String("status");
temp[1] = tempLatitude;
temp[2] = tempLongitude;

listactivity.setListAdapter(new ArrayAdapter(listactivity,
android.R.layout.simple_list_item_1, temp));

The setListAdatper method is sent to the listactivity reference, which points to the MyGps object. The setListAdapter redisplays the new info from the GPS reading. Note, this method of displaying new info is different from what one finds in the current Android 2.0 docs. The current Android 2.0 docs say that the onUpdate method will work but I've found that it does not work.

j. Download the application to the emulator and wait for the emulator to run the application.

k. When the application is running, run the ddms.

m. In the ddms gui, select the instance of the emulator.

n. In the upper right hand corner area of the ddms gui, click on the rh arrow until the 'Emulator Control' tab is selected.

o. Scroll down until you see the 'Location Controls' area in the 'Eumlator Control' area. The textboxes and send button should not be grayed out. If they are, go back to step m and make sure that the instance of the emulator is selected.

p. Change the longitude and/or latitude coordiantes.

q. Hit the send button.

r. When the send button is clicked, you should see the contents of the application on the Android emulator show the new longitude and/or latitude coordinates.

No comments: