Monday, January 4, 2010

Setup of Wiktionary (Simplified) Android project

Looks like my previous instrutions for Api Demos don't apply for the new Simplewiktionary project for Android.

Even File->Import and other variations are failing. I discovered that something new and more specific was required. You might be wondering why I'm even bothering to blog this. Well, it seems that there are many wrong ways to get these sample projects into Eclipse. It seems that the 'art' is find the few right ways to do this.

1. Start Eclipse

2. File->New->Project->Android->Android Project

3. Click on the 'next' button

4. Choose your build target

5. Uncheck the 'Create Activity' checkbox

6. Specify the Project Name as 'WiktionarySimple'

7. Uncheck the 'Use default location' checkbox

8. Click on the 'Browse' of the Location textbox and go to the WiktionarySimple directory created by unzipping latest_samples.zip in the directory of your choice.

9. In the contents area, select the radio button 'Create project from existing source'

10. In the properties area, set the application name to 'WiktionarySimple'

11. Click the 'Finish' button

12. If these steps were followed, when Eclipse is done updating the workspace, the WiktionarySimple project will be compiled and the R.java file will be generated in the project.

Saturday, January 2, 2010

Creating Android Sample Application

Recently, Android 2.0 had some new sample projects added. They are available as a .zip file from the Android developers site. However, if you unzip and import a project, something wrong happens. The R.java is not generated. The instructions for importing these sample project are as follows from the Android developer site:

You can easily create new Android projects with these samples, modify them if you'd like, then run them on an emulator or device. For example, to create a project for the API Demos app from Eclipse, start a new Android Project, select "Create project from existing source", then select ApiDemos in the samples/ directory. To create the API Demos project using the android tool, execute:

android update project -s -n API Demos -t -p /samples/ApiDemos/

The given instructions give 2 possible actions. The first, as indicated previously, does not work. The second action uses the 'android' executable which is part of the SDK. The options given do the following:


update project : updates an existing android project
-s : only errors produce output (silent mode)
-n : name of new android device (in this case it is 'API Demos'
-t : target id of new android device
-p : Location path of the directory where the new AVD will be created

If you run the above 'android' command-line, you will get the following result:

Updated default.properties
Updated local.properties
File build.xml is too old and needs to be updated.
Added file /home/me/Projects/A20/samples/ApiDemos/build.xml
Updated default.properties
Updated local.properties
File build.xml is too old and needs to be updated.
Added file /home/me/Projects/A20/samples/ApiDemos/tests/build.xml

So, neither of the above procedures really lets you create one of the sample projects for use. The entry will show you the right way to do this for the new ApiDemos sample.

1. The ApiDemos project is an Android 1.6 project. So, in general, make sure before using any of the sample projects that you have the right SDK version installed.

2. Start Eclipse.

3. File->New->Project...->Android->Android Project

4. click on the 'next' button

5. In the next window that appears, select the radio button 'Create Project from existing source'

6. Click on the 'Browse...' button next to the 'Location' text box.

7. Browse to the directory that contains the ApiDemos directory from the latest_samples.zip file.
The 'Location' text box should have a path like /path/to/latest/ApiDemos

8. Select OK

9. In the 'Build Target' area, select the 'Android 1.6' build target.

10. Select the radio button 'Create Project from Existing Sample'

11. In the 'Samples' drop-down, select 'ApiDemos'

12. Click on the 'Finsh' button

13. If this is succesful, the ApiDemos project will build with no errors and the R.java files will be generated automagically.

The above method should be used for the other samples, too. If you do not, the R.java file will not be generated.

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.