Android development-workshop-v2-1228043706544191-8

54
copyright © 2008  cod technologies ltd  www.codtech.com mobile dev camp android development workshop diego torres milano [email protected] amsterdam, november 2008

Transcript of Android development-workshop-v2-1228043706544191-8

Page 1: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

mobile dev campandroid development workshop

diego torres [email protected]

amsterdam, november 2008

Page 2: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

“I have always wished that my computer would be as easy to use as my telephone.

My wish has come true.

I no longer know how to use my telephone.”

­­ Bjarne Stroustrup

Page 3: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

agenda

● introduction to android

● android architecture

● building blocks

● your first android application

● testing and performance

● best practices

Page 4: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

introduction to android

● identify unique features of android platform

● compare android against other platforms

● understand android building blocks

after this section you will...

Page 5: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

what is android ?● android is the first complete, open 

and free mobile platform● developed by Open Handset 

Alliance● software stack than includes

– operating system– middleware– key applications– rich set of APIs

Portions of this page are reproduced from work created and shared by Google and used according to terms described in 

the Creative Commons 2.5 Attribution License.

Page 6: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

is android linux ?

● no native windowing system

● no glibc support● no GNU/Linux utilities

android is based on a linux kernel  but it's not GNU/Linux

NO, android is not linux !

Page 7: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

so is android java ?

● uses the java language● implements part of the 

Java5 SE specification● runs on a dalvik virtual 

machine instead of JVM

android is not an implementation of any of the Java variants

NO, android is not java !

Page 8: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

android linux kernel

● security● memory management● process management● network stack● driver model● abstraction layer

android is based on a linux 2.6 kernel, prnel, providing

kernel source: source.android.com

Page 9: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

linux kernel enhancements

● alarm● ashmem● binder● power management● low memory killer (no swap space available)

● logger

android introduces some linux kernel patches

Page 10: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

unique platform characteristics

● open source

● “all applications are equal” model

● dalvik virtual machine

android characteristics not found on other platforms

Page 11: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

other characteristics

● application framework enabling reuse of components

● integrated browser based on WebKit OSS engine

● 3D graphics based on the OpenGL ES

● SQLite for structured data storage

● media support for common audio, video, and still images

● camera, GPS, compass, and accelerometer

interesting features as well, but they are more common across other mobile platforms

Page 12: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

android architectureco

urte

sy o

f Goo

gle

Page 13: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

after this section you will...

android building blocks

● recognize the fundamental building blocks

● use these building blocks to create applications

● understand applications lifecycle

Page 14: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

building blocks

Page 15: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

Activities

● Activities are stacked like a deck of cards

● only one is visible

● only one is active

● new activities are placed on top

Page 16: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

Activities lifecycle

rectangles are callbacks wherewe can implement operationsperformed on state changes

Page 17: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

Activities states● active

– at the top of the stack

● paused– lost focus but still visible– can be killed by LMK

● stopped– not at the top of th stack

● dropped– killed to reclaim its memory

Page 18: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

Views

● Views are basic building blocks

● know how to draw themselves

● respond to events

● organized as trees to build up GUIs

● described in XML in layout resources

Page 19: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

pattern: load layout

android compiles the XML layout code that is later loaded in code usually by

public void onCreate(Bundle savedInstanceState) { ... setContentView(R.layout.filename); ...}

Page 20: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

Views and Viewgroups● Views and 

Viewgroups trees build up complex GUIs

● android framework is responsible for– measuring– laying out– drawing

Page 21: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

pattern: ids

using a unique id in a XML View definition permits locating it later in Java code

private View name;

public void onCreate(Bundle savedInstanceState) { ... name = (View) findViewById(R.id.name); ...}

Page 22: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

Intents● Intents are used to move from Activity to Activity● describes what the application wants● provides late runtime binding

primary attributes

attribute description

action

data

the general action to be performed, such as VIEW, EDIT, MAIN, etc.

the data to operate on, such as a person record in the contacts database, as URI

Page 23: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

intents playground

http://codtech.com/android/IntentPlayground.apk

Page 24: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

Services

● services run in the background

● don't interact with the user

● run on the main threadof the process

● is kept running as long as

– is started

– has connections

Page 25: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

Notifications

● notify the user about events

● sent through NotificationManager

● types

– persistent icon

– turning leds

– sound or vibration

Page 26: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

ConentProviders

● ContentProviders are objects that can

– retrieve data

– store data

● data is available to all applications

● only way to share data across packages

● usually the backend is SQLite

● they are loosely linked to clients

● data exposed as a unique URI

Page 27: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

AndroidManifest.xml● control file that tells 

the system what to do and how the top­level components are related

● it's the “glue” that actually specifies which Intents your Activities receive

● specifies permissions

Page 28: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

after this section you will...

your first android

● create your own android map project

● design the UI● externalize resources● react to events● run the application

Page 29: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

android project

Page 30: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

default application● auto­generated 

application template● default resources

– icon– layout– strings

● default AndroidManifest.xml

● default run configuration

Page 31: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

designing the UIthis simple UI designs contains● the window title

● a spinner (drop down box) containing the desired location over the map

● a map displaying the selected location

Page 32: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

create the layout

● remove old layout● add a RelativeLayout● add a View (MapView not 

supported by ADT)

● replace View by com.google.android.mapview

● change id to mapview● add a Spinner filling 

parent width

Page 33: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

run the application

● com.google.android.maps it's an optional library not included by default

● add <uses-library android:name="com.google.android.maps" /> to manifest as application node

Page 34: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

Google Maps API key

● checking DDMS logcat we find

● to access Google Maps we need a key● application must be signed with the same key● key can be obtained from Google● MapView should include

java.lang.IllegalArgumentException: You need to specify an API Key for each MapView.

android:apiKey="0GNIO0J9wdmcNm4gCV6S0nlaFE8bHa9WXXXXXX"

Page 35: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

MapActivy

● checking DDMS logcat again

● change base class to MapActivity

● fix imports

● add unimplemented methods

java.lang.IllegalArgumentException: MapViews can only be created inside instances of MapActivity.

Page 36: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

where is the map ?

● still no map displayed

● check DDMS logcat

● lots of IOExceptions !

● some uses permissions are missing

– ACCESS_COARSE_LOCATION

– INTERNET

Page 37: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

finally our map

still some problems ...

● spinner is covered

● has no prompt

● externalize resource

android:layout_alignParentTop="true"

prompt: @string/prompt

Page 38: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

pattern: adapters

an Adapter object acts as a bridge between an AdapterView and the underlying data for that view

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.array, android.R.layout.layout);

view.setAdapter(adapter);

The Adapter is also responsible for making a View for each item in the data set.

Page 39: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

pattern: resources

resources are external files (that is, non­code files) that are used by your code and compiled into your application at build time.

<resources> <string-array name=”array”> <item>item</item> </string-array></resources>

res = getResources().getType(id);

Page 40: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

arrays.xml

<?xml version="1.0" encoding="UTF-8"?>

<resources><!-- No support for multidimensional arrays or complex objects yet (1.0r1) -->

<string-array name="location_names"><item>Mediamatic Duintjer</item><item>NH Hotel</item><item>Airport</item>

</string-array>

<string-array name="locations"><item>52.363125,4.892070,18</item><item>37.244832,-115.811434,9</item><item>-34.560047,-58.44924,16</item>

</string-array></resources>

Page 41: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

complete the class● create the locations array

● get the views (ids pattern)

● create the adapter

locations = getResources().getStringArray(R.array.locations);

spinner = (Spinner) findViewById(R.id.Spinner01);mapView = (MapView) findViewById(R.id.mapview);

ArrayAdapter<CharSequence> adapter = ArrayAdapter. createFromResource(this, R.array.location_names, android.R.layout.simple_spinner_item);spinner.setAdapter(adapter)

Page 42: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

almost there● map is displayed● spinner is displayed● drop down is 

displayed● but there's no 

selection button ...adapter. setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);

Page 43: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

respond to events● when an item is 

selected map should be centered at that location

● invoke goToSelectedLocation(arg2);

spinner. setOnItemSelectedListener( new OnItemSelectedListener() { });

Page 44: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

goToSelectedLocation

protected void goToSelectedLocation(int position) {String[] loc = locations[position].split(",");

double lat = Double.parseDouble(loc[0]);double lon = Double.parseDouble(loc[1]);int zoom = Integer.parseInt(loc[2]);

GeoPoint p = new GeoPoint((int)(lat * 1E6), (int)(lon * 1E6));

Log.d(TAG, "Should go to " + p);

mapController.animateTo(p);mapController.setZoom(zoom);

}

Page 45: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

more events● turn map clickable

● override onKeyDown

android:clickable="true”

switch (keyCode) {case KeyEvent.KEYCODE_I:

mapController.zoomIn();break;

case KeyEvent.KEYCODE_O:mapController.zoomOut();break;

case KeyEvent.KEYCODE_S:mapView.setSatellite(

!mapView.isSatellite());break;

}

Page 46: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

we did it !

● Some things to try

– select a location

– pan

– zoom in

– zoom out

– toggle satellite

Page 47: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

“Remember that there is no code faster than no code”

­­ Taligent's Guide to Designing Programs

Page 48: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

after this section you will...

testing and performance

● understand the best practices to develop for android

●  identify the alternatives to test units, services and applications

● performance

Page 49: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

best practices

● consider performance, android is not a desktop● avoid creating objects● use native methods● prefer virtual over interface● prefer static over virtual● avoid internal getter/setters● declares constants final● avoid enums

Page 50: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

testing

● android sdk 1.0 introduces

– ActivityUnitTestCase to run isolated unit tests

– ServiceTestCase to test services

– ActivityInstrumentationTestCase to run functional tests of activities

● ApiDemos includes some test samples

● monkey, generates pseudo­random of user events

Page 51: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

performance

Action Time

1

4

5

5

1 2

.1 2 5

1 5

1 65

60 0

220 0 0

250 0 0

1 0 0 0 0 0

1 350 0 0

30 0 0 0 0 0

Add a local variable Add a member variable 

Call String.length()Call empty static native methodCall empty static method 

Call empty virtual method Call empty interface method Call Iterator:next() on a HashMap Call put() on a HashMapInflate 1 View from XML Inflate 1 LinearLayout with 1 TextView Inflate 1 LinearLayout with 6 ViewInflate 1 LinearLayout with 6 TextView

Launch an empty activity 

Add a

loca

l variable

Add a

mem

ber

variable

Call

Str

ing.le

ngth

()

Call

em

pty

sta

tic

native

meth

od

Call

em

pty

sta

tic

meth

od

Call

em

pty

virtu

al m

eth

od

Call

em

pty

inte

rface

meth

od

Call

Itera

tor:

nex

t()

on a

Hash

Map

Call

put(

) on a

Hash

Map

Infla

te 1

Vie

w f

rom

XM

L

Infla

te 1

Lin

earL

ayo

ut

with 1

Tex

tVie

w

Infla

te 1

Lin

earL

ayo

ut

with 6

Vie

w

Infla

te 1

Lin

earL

ayo

ut

with 6

Tex

tVie

w

Launch

an e

mpty

act

ivity

0

500000

1000000

1500000

2000000

2500000

3000000

Time

Page 52: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

summary● introduction to android● android building blocks●

Page 53: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

“If things seem under control, you're not going fast enough.”

­­ Mario Andretti

Page 54: Android development-workshop-v2-1228043706544191-8

copyright © 2008  cod technologies ltd  www.codtech.com

thank youandroid development workshop

diego torres [email protected]