Intro to Android: Getting going with...

52
Intro to Android: Getting going with development Assoc. Professor Donald J. Patterson INF 241 Winter 2012 1 Friday, February 3, 12

Transcript of Intro to Android: Getting going with...

Page 1: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android:Getting going with development

Assoc. Professor Donald J. PattersonINF 241 Winter 2012

1Friday, February 3, 12

Page 2: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android

http://developer.android.com/guide/index.htmlFriday, February 3, 12

Page 3: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android

http://developer.android.com/guide/index.htmlFriday, February 3, 12

Page 4: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android

http://developer.android.com/guide/index.html

• SDK (Software development kit)

• AVD (Android Virtual Device)

• .apk (Android application

package file)

• “platform”: Gingerbread v2.3.6 /

v2.3.4 is our target

• ADT (Android Development

Tools) Eclipse plug-ins plus

command line tools

Friday, February 3, 12

Page 5: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android

• Requirements

• SDK/AVD

• Eclipse Plug-in

• Hello World

http://developer.android.com/guide/index.html

Actually Developing for Android 2.3.6

Friday, February 3, 12

Page 6: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android

• Requirements

• SDK/AVD

• Eclipse Plug-in

• Hello World

http://developer.android.com/guide/index.html

Actually Developing for Android 2.3.6

Friday, February 3, 12

Page 7: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android

• Requirements

• SDK/AVD

• Eclipse Plug-in

• Hello World

http://developer.android.com/guide/index.html

Actually Developing for Android 2.3.6

Friday, February 3, 12

Page 8: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android: Requirements

•OS

• Windows XP (32), Vista (32/64)or Windows 7(32/64)

• Mac OS 10.5.8 or later (Leopard, x86 only)

• Linux (e.g., Ubuntu Lucid Lynx (32))

• IDE

• Eclipse 3.5 or greater

• “Eclipse IDE for Java Developers” (for example)

• Java

• JDK 5 or 6 (1.5 or 1.6 (not just JRE!))

http://developer.android.com/guide/index.htmlFriday, February 3, 12

Page 9: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android

• Requirements

• SDK/AVD

• Eclipse Plug-in

• Hello World

http://developer.android.com/guide/index.html

Actually Developing for Android

Friday, February 3, 12

Page 10: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android

• Requirements

• SDK/AVD

• Eclipse Plug-in

• Hello World

http://developer.android.com/guide/index.html

Actually Developing for Android

Friday, February 3, 12

Page 11: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android: SDK

• Download and unpack the appropriate “Android SDK and

AVD manager”

• This is a program that downloads and manages the

installation of libraries for you

• Load the correct “Platform SDK”

• Create an Android Virtual Device

http://developer.android.com/sdk/adding-components.htmlFriday, February 3, 12

Page 12: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android: SDK

Friday, February 3, 12

Page 13: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android: AVD

Friday, February 3, 12

Page 14: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android

• Requirements

• SDK/AVD

• Eclipse Plug-in

• Hello World

http://developer.android.com/guide/index.html

Actually Developing for Android

Friday, February 3, 12

Page 15: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android

• Requirements

• SDK/AVD

• Eclipse Plug-in

• Hello World

http://developer.android.com/guide/index.html

Actually Developing for Android

Friday, February 3, 12

Page 16: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android: Eclipse Plug-in

• The Eclipse Plug-in is called the “ADT”

• Android Development Tools

• Support for menu options in Eclipse which support

• Automatically building Android projects

• User-Interface building for Android

• Debugging support for Android

• Packaging files for the Android Market (.apk files)

• The ADT is installed from within Eclipse

http://developer.android.com/sdk/eclipse-adt.htmlFriday, February 3, 12

Page 17: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android: Eclipse Plug-in

• Install the plug-in

• Restart Eclipse

• Configure the plug-in

http://developer.android.com/sdk/eclipse-adt.htmlFriday, February 3, 12

Page 18: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android: Eclipse Plug-in

http://developer.android.com/sdk/eclipse-adt.htmlFriday, February 3, 12

Page 19: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android

• Requirements

• SDK/AVD

• Eclipse Plug-in

• Hello World

http://developer.android.com/guide/index.html

Actually Developing for Android

Friday, February 3, 12

Page 20: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android

• Requirements

• SDK/AVD

• Eclipse Plug-in

• Hello World

http://developer.android.com/guide/index.html

Actually Developing for Android

Friday, February 3, 12

Page 21: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android

http://developer.android.com/guide/index.html

Actually Developing for Android

Friday, February 3, 12

Page 22: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android

• Create a project

• Build a basic U/I

• Run the Application

• Improve the U/I

• Debug the Application

http://developer.android.com/guide/index.html

Hello World

Friday, February 3, 12

Page 23: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intro to Android

• Create a project

• Build a basic U/I

• Run the Application

• Improve the U/I

• Debug the Application

http://developer.android.com/guide/index.html

Hello World

Friday, February 3, 12

Page 24: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Friday, February 3, 12

Page 25: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

User Interaction:Intro to Android

Assoc. Professor Donald J. PattersonINF 241 Winter 2012

19Friday, February 3, 12

Page 26: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Checking out the phone

• Unpack the phone

http://www.google.com/support/android/bin/topic.py?hl=en&topic=28930

Friday, February 3, 12

Page 27: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

• Take a look at the sensors

http://www.google.com/support/android/bin/topic.py?hl=en&topic=28930

Checking out the phone

Friday, February 3, 12

Page 28: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

• Install the battery - Do Not Damage My Phones!

http://www.google.com/support/android/bin/topic.py?hl=en&topic=28930

Checking out the phone

Friday, February 3, 12

Page 29: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

• Charge the phone to 100%

• USB to computer

• USB to wall plug

•While charging, go through on-phone tutorial

• Do not sync to your Google

• Enable Location reporting

• Set Date and Time

• Register your device with OIT (or send me the MAC address)

• Home -> Menu -> Settings -> About Phone -> Status

http://www.google.com/support/android/bin/topic.py?hl=en&topic=28930

Checking out the phone

Friday, February 3, 12

Page 30: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Activity Lifecycle

http://developer.android.com/reference/android/app/Activity.html

• Key loops

• Entire Lifetime

• onCreate()- onDestroy()

• Visible Lifetime

• onStart() - onStop()

• Foreground Lifetime

• onResume() - onPause()

• onPause() may be followed

by kill

Friday, February 3, 12

Page 31: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Activity Stack

Friday, February 3, 12

Page 32: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Intents

http://developer.android.com/reference/android/content/Intent.html

• Requests made by an Activity to have the phone do something

• e.g., take a picture, look up a contact etc.

• Specified abstractly so that new software can answer the Intent

• Calling an Intent requires an Action and some data

• e.g., ACTION_DIAL, tel:555-1212 = use phone to call 555-1212

• An Activity can start an Intent

• A Broadcast Receiver can respond to an Intent

• An Intent is a message sent from one app to another

Friday, February 3, 12

Page 33: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Simulating Sensors

•On the emulator

Friday, February 3, 12

Page 34: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

User Application

Emulator

Android OS

Request Sensor Data by Intent

No Services that fulfill that Intent

#FAIL

Sensors in the emulator

Friday, February 3, 12

Page 35: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

• Simulating Sensors on the

Emulator requires:

• A 3rd-party program

on the emulator

• A 3-rd party program

on your computer

• configuration of the

two

• small changes to your

code

User Application

Emulator

Android OS

Request Sensor Data by Intent

#FTW

Sensor Simulator

Intent gets fulfilled by software not

hardware sensors

Program on computer specifies

what sensor readings are on the emulator

Sensors in the emulator

Friday, February 3, 12

Page 36: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

How to handle the assignment

• Your approach: Stage 1

• get your environment

working

User Application

Emulator

Android OS

"Hello World"

Friday, February 3, 12

Page 37: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

• Your approach: Stage 2

• get your program

working with

simulated sensors

• fast and easy to

iterate

User Application

Emulator

Android OS

Request Sensor Data by Intent

#FTW

Sensor Simulator

Intent gets fulfilled by software not

hardware sensors

Program on computer specifies

what sensor readings are on the emulator

How to handle the assignment

Friday, February 3, 12

Page 38: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

• Your approach: Stage 3

• get your program

working with a real

phone

User Application

Real Phone

Android OS

Request Sensor Data by Intent

#FTW

Hardware Sensors

How to handle the assignment

Friday, February 3, 12

Page 39: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

• Stage 2

• Start a new project on Android 2.3.3

• download the sensor simulator from

• http://code.google.com/p/openintents/downloads/list?q=sensorsimulator

• unzip the code into a lib directory

• add the sensor-simulator jar to your build path

• create a basic UI in xml

• launch the emulator

• load the sensor simulator into the emulator

• adb install SensorSimulatorSettings-2.0-rc1.apk

http://developer.android.com/guide/developing/device.html

How to handle the assignment

Friday, February 3, 12

Page 40: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

• Stage 2

• Give your application permission to use the Internet

in AndroidManifest.xml

• <uses-permission android:name="android.permission.INTERNET"/>

• Start the sensor simulator on your computer

• java -jar ./sensorsimulator-2.0-rc1.jar

• Test the sensor software on the phone

• Make your application debuggable

• Run your application that requests sensor information

http://developer.android.com/guide/developing/device.html

How to handle the assignment

Friday, February 3, 12

Page 41: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

• Stage 2

How to handle the assignment

Friday, February 3, 12

Page 42: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

• Stage 2

How to handle the assignment

Friday, February 3, 12

Page 43: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

How to handle the assignment

Friday, February 3, 12

Page 44: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

How to handle the assignment

Friday, February 3, 12

Page 45: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

What it looks like when it’s working (Stage 2)

Friday, February 3, 12

Page 46: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Hints

• Playing a sound

• The key is the MediaPlayer call

• Do not instantiate more than one MediaPlayer object

• http://developer.android.com/guide/topics/media/index.html

Friday, February 3, 12

Page 47: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Hints

• Playing a sound

• You will need to get the audio media onto the phone

Friday, February 3, 12

Page 48: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Hints

Friday, February 3, 12

Page 49: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Hints

•One possible architecture for getting sensor readings

• Steps

• Create a U/I for the data

• Instantiate your Sensor Recorder

• Register for sensor callbacks

• Instantiate your UI Updater

• Have a timer occasionally run it

Sensor Recorder

Class

Local Copy of last sensor

callback

UI Updater Class

Rapidly arriving sensor data

Occasional updates to

screen

1.0180.0

User Application

Friday, February 3, 12

Page 50: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Hints

Sensor Recorder

Class

Local Copy of last sensor

callback

UI Updater Class

Rapidly arriving sensor data

Occasional updates to

screen

1.0180.0

User Application

Friday, February 3, 12

Page 51: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Troubleshooting

Friday, February 3, 12

Page 52: Intro to Android: Getting going with developmentdjp3/classes/2012_01_INF241/Lectures/Lecture08Slides.pdf · Intro to Android: Getting going with development Assoc. Professor Donald

Friday, February 3, 12