JSIG - Google Android Tutorial

Below are a few notes about what we did during the Google Android tutorial for JSIG. Instructions are for Google Android 1.1, and Eclipse Ganymede.

About Google Android

  • It is an open source operating environment targeted for mobile devices.

Setting Up Google Android

There are four major components of setting up Eclipse to work with Android.

Downloading the SDK

  • Go to http://developer.android.com/
  • Click on the link that says: Download Android 1.1 SDK r1
  • Pick the right one for the operating system you are using, and follow the directions to download the .zip file.
  • Unpack the .zip file to a location on your computer that you can remember. I put mine in a folder called "C:/Android"
  • Two things in particular to notice in the Android directory: Android.jar and the /tools directory.

Add Android to the Path Variable

You'll also need to add the /tools directory to the path environment variable. This is done in different ways on different operating systems. On Windows:

  • Right click on My Computer (or Computer on Vista)
  • Select Properties
  • Select Advanced Tab
  • Click Environment Variables button
  • Select the Path variable from the list at the bottom
  • Add the full path to the /tools directory at the end of the variable value, separated by a semi-colon (so add something like ";C:\Android\android-sdk-windows-1.1_r1\tools\" to the end of the variable value).

Get the Android plugin for Eclipse

  • Go to Help > Software Updates > Available Software Tab
  • Click "Add Site" button
  • Add the following site: http://dl-sll.google.com/android/eclipse
  • Put a check in the box by this site in the list
  • Click on the "Install" button
  • After installing, let Eclipse restart.

Pointing Eclipse to Android SDK

Eclipse now needs to know where the android.jar file and other stuff is located.

  • Go to Window > Preferences
  • Click on "Android" in the tree
  • type in the full path to your Android directory (e.g. "C:\Android\android-sdk-windows-1.1_r1")

A Simple Google Android Project

Creating a new Google Android Project

  • File > New > Project
  • Choose the Android/Android Project wizard.
  • Give it a project, package, activity, and application name (e.g. "Demo", "package.name", "Demo", "Demo")

Running your Program

  • Click on the down arrow by the green run button and choose "Run Configurations"
  • Double click on the "Android Application" in the left part to create a new launch configuration.
  • Give the configuration a name and tell it to use your project, and optionally, select the activity that you want to launch.
  • Click "Apply"
  • Click "Run" to start the emulator. You don't need to terminate the emulator between launches, which will save time the next time you launch.

Debugging Google Android

  • Go to the "Open Perspective" button in the upper right and choose the "DDMS" perspective. I don't know what it stands for, but that's the one you want.
  • You can switch back and forth between the Java and DDMS perspectives as you go between developing and debugging.
  • To "write to the console", you use the command Log.d("tag", "message");. Alternatively, you can use Log.v(), Log.e(), Log.w(), or Log.i(), which are short for different debug levels. e=Error, w=Warning i=Info, d=Debug, v=Verbose. In the DDMS perspective, you can change what levels you want to see.

An Advanced Google Android Project

We'll create a project that reads contacts addresses and gets you a map to their house. So first add in some contacts to the emulator and put in addresses!

Setting up the GUI

  • Open up /res/layout/main.xml
  • Get rid of the TextView item in the Outline window on the right (hidden by default)
  • Add in a ListView and change its id in the properties to @android:id/android:list

Security Settings

  • Open up AndroidManifest.xml
  • Click on Permissions
  • Click Add
  • Click Uses Permission
  • In the name field put "android.permission.READ_CONTACTS"

Coding the Program

The code for the main activity class:

package demo.demo;
 
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.util.Log;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
 
public class Demo extends ListActivity // This is important!  We've made it a ListActivity!
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        Cursor cursor = this.getContentResolver().query(People.CONTENT_URI, null, null, null, null);
        startManagingCursor(cursor);
 
        ListAdapter adapter = new SimpleCursorAdapter(this, 
                android.R.layout.simple_list_item_1, 
                cursor, 
                new String[] {People.NAME},
                new int[] {android.R.id.text1}); 
        Log.d("tag", "message");
 
        setListAdapter(adapter);
    }
 
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) 
    {
        super.onListItemClick(l, v, position, id);
 
        try
        {
            Uri selectedContact = Uri.parse(android.provider.Contacts.ContactMethods.CONTENT_URI.toString());
            Log.d("Selected Contact", selectedContact.toString());
            Cursor c = managedQuery(selectedContact, null, " type = 1 and person = " + id, null, null);
            c.moveToNext();
 
            String address = c.getString(c.getColumnIndex("data"));
 
            address = address.replace("\n","");
            address = address.replace(",","");
            address = address.replace(" ","+");
 
            Intent geoIntent = new Intent("android.intent.action.VIEW", Uri.parse("geo:0,0?q=" + address));
            startActivity(geoIntent);
        }
        catch(Exception e)
        {
            e.printStackTrace();
            Log.e("Error", "Broken?", e);
        }
    }
 
}

References

http://developer.android.com/ Be sure to check out the DevGuide.
Developing Android Applications using Eclipse is a great place to start (and where I got my inspiration, but it is out of date. Perhaps and old version of Android.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License