Mobile Me(dia)

Shawn Van Every Shawn.Van.Every@nyu.edu
Spring 2009
H79.2690.1

Week 9 - Android

Android Introduction

Android, despite being a baby has an interesting history. A group of companies including Google setup an organization called the Open Handset Alliance whose mission is to:

"to accelerate innovation in mobile and offer consumers a richer, less expensive, and better mobile experience. Together we have developed Android&tm;, the first complete, open, and free mobile platform."

As of August 2005, The Android platform was being developed by a company called Android outside of Google. In August 2005, Google purchased the company and we are now seeing the fruits of that acquisition.

Android is only an operating system, the G1, manufactured by HTC, a member of the Open Handset Alliance is merely the first phone that supports Android out in the market. It is sold as a locked device through T-Mobile (although you can unlock it for free after 90 days of service or if you pay for it from companies offering the codes online). (There is also a developer version of the phone available through Google that is unlocked already.)

The Android OS is built on top of Linux and is (almost) completely open source meaning that it can be modified and distributed freely.

The Android OS itself is written using Java but not Sun's Java, a different implementation so that Google or whomever does not have to pay Sun royalties. The same goes for the development of Android applications.

More Information: Dalvik: how Google routed around Sun's IP-based licensing restrictions on Java ME
Android Source Code
Main Android Site

Getting Started - Setup

Setup of the tools for Android development is slightly complicated but not terrible. Let's walk through it:

The Android Developer Site: http://developer.android.com/

First, Get the SDK: You can download the latest version of the Android SDK from http://developer.android.com/sdk/1.1_r1/index.html

Then, Install the SDK: After you download, extract and move the SDK, you have to setup your environment. The first thing you have to do is tell your computer where the SDK lives. The means to do that requires using Terminal on the Mac.

The following assumes I have moved the sdk to /Developer/Java/ and it is called "android-sdk-mac_x86-1.1_r1".

Launch Terminal and type:
		nano .profile
		
That should put you in an editor window. Near the bottom of the file, you need to put in the following:
		export SDK_ROOT=/Developer/Java/android-sdk-mac_x86-1.1_r1/
		export PATH=$PATH:$SDK_ROOT/tools
		
Following that, you need to save the file by typing CNTRL-o, return and then CNTRL-x to exit. Last, you need to execute that file:
		source .profile
		


That's it for the SDK.. Now we move on to Eclipse.

Download Eclipse: http://www.eclipse.org/downloads/. You probably want "Eclipse IDE for Java Developers".

After you install Eclipse, you need to install the Android Development Tools (ADT): http://developer.android.com/guide/developing/eclipse-adt.html (Don't forget about the Eclipse preferences)

Now you are ready to go!!!

More Information:
Getting Started with Android using Eclipse

Hello World

In Eclipse, choose File: New: Project: Android Project
Project Name: Something for your reference, will be the Eclipse project name
Package Name: com.yourname.something
Activity Name: Application Name (no spaces) (Activity == A Java Class)
Application Name: How it will appear on the phone

After you do that it will show up in Eclipse on the left under Package Explorer with your Project name.

Click the triangle to the left and see what is in there.

Under "src", your.package.name you will see a ActivityName.java file. This is the main place to write your code.

Change the public void onCreate method to the following:
public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	TextView tv = new TextView(this);
	tv.setText("Hello World");
	
	setContentView(tv);
	
	
   //setContentView(R.layout.main);
}		
		
add in import android.widget.TextView; with the rest of the imports.

Now you should have an app that when run, say's Hello World!

You can run it by clicking the green triangle at the top and selecting Android Application. (If you have a G1 plugged in to your computer, it will probably run on it. If not, it will run in the emulator)

To make this an Application that you can send to other people who have Android phones, right click on the package and click Android Tools: Export Unsigned Android Application. That should create an apk (Android Package) file that you can distribute.

Android UI Concepts

You will notice in the Eclipse package that their is a "res" folder which contains a "layout" folder. Inside the layout folder is a "main.xml". This main.xml file is what you can use to define a layout without having to do it programatically. It looks something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent" 
              android:layout_height="fill_parent"
              android:orientation="vertical" >
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="Hello, I am a TextView" />
    <Button android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello, I am a Button" />
</LinearLayout>
		
The User Interface section of the Android Deveveloper's Guide explains it in detail.
You will notice that I have a button and textview in the above example. Both of these have an id which is set to be @+id/button or @+id/text. Those values are defined in the strings.xml file in the res/values directory: More Information:
Fundamentals

Doing Something

Mostly from here: CameraPreview.java
package com.testing.pictures;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.hardware.Camera;

public class APicture extends Activity {
	
	Camera mCamera;
	Preview mPreview;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        
        // Create our Preview view and set it as the content of our activity.
        mPreview = new Preview(this);
        setContentView(mPreview);        
    }
}

class Preview extends SurfaceView implements SurfaceHolder.Callback {
    SurfaceHolder mHolder;
    Camera mCamera;

    Preview(Context context) {
        super(context);

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
        mCamera = Camera.open();
        mCamera.setPreviewDisplay(holder);
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        // Because the CameraDevice object is not a shared resource, it's very
        // important to release it when the activity is paused.
        mCamera.stopPreview();
        mCamera = null;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(w, h);
        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }

}		
More Information:
Class Reference
Sample Code

Next week, we'll look at doing some more...