G1 Development Environment and Applica 4 on Development

fansbutterflyMobile - Wireless

Jul 19, 2012 (4 years and 5 months ago)

289 views

G1
Development
Environment

and
Applica4on
Development

Adam
C.
Champion

CSE
788X11

Prof.
Dong

Xuan

Outline



Introduc<on



G1
development
environment



SeAng
it
up



Hello
Android
example



G1
applica<on
development



Mobile
app
dev
rules



Applica<on
components



GUI
design
and
implementa<on

The
Big
Picture
(1)



The
T‐Mobile
G1
has
many

sensors

you
can

easily
program,

e.g.
,



Camera



GPS,
loca<on‐based
services



Accelerometer
and
orienta<on
sensor
(games)



You
can
also
program
the
G1
to



Play
music



Work
with
online
services,

e.g.
,
Google

gData



Vibrate
the
phone
when
“something
happens”

The
Big
Picture
(2)



Google
has
developed
a
set
of
Eclipse‐based

tools
and
the
Android
SDK
to
program
the
G1

in
Java



Assump4on:

you
already
know
Java
or
can

learn
it
very
quickly



All
the
apps
on
the
G1
are
wriXen
in
Java

using
this
SDK,

e.g.
,
Amazon
MP3,
the
Web

browser,
instant
messenger,
…

Development
Environment
Setup
(1)



Development
prerequisites:




Download
recent

JDK

from

http://java.sun.com

(version
6
preferable)



Download
Eclipse
for
Java
Developers
from

http://www.eclipse.org/downloads





Download
the
Android
SDK
from

http://code.google.com/android

and
unzip
it
to

a
directory
(remember
the
directory!)

+

+

+



setup
=

Development
Environment
Setup
(2)



Within
Eclipse,
add

https://dl‐ssl.google.com/

android/eclipse/

as
a
Soaware
Update
site
(
Help



SoCware
Update
)



Alterna<vely:
use

http://dl‐ssl.google.com/android/

eclipse/site.xml


if
an
error
occurs



Download
&
install
Google’s
Android
Dev
Tools

plugin



Restart
Eclipse;
go
to

Preferences

(in

Window

menu,

Eclipse

menu
on
OS
X),
select

Android
,
enter
your

SDK
directory

+

+

+


setup
=

Development
Environment
Setup
(3)

You’re
in
business!

Development
Environment
Tools
(1)



Android
provides
several
dev

tools:



Hardware‐agnos<c
mobile
phone

emulator



Implements
Dalvik,
a
Java
VM

op<mized
for
mobile
devices



Dalvik
uses
the
Linux
kernel
to

manage
process
isola<on,
memory



Dalvik
Debug
Monitor
Service

(DDMS)
lets
user
monitor
Dalvik

threads,
heap,
log
output,
etc.



Android
Debug
Bridge
(ADB)

provides
interface
between
dev

computer
and
G1



Android
Asset
Packaging
Tool

(AAPT)
packages
Android
project

files
into
an
Android
Package

(
.apk
)
file

Development
Environment
Tools
(2)



Google’s
Eclipse

plugin

seamlessly
integrates
all
these
tools



Easy
Android
project
management,
edi<ng
XML
files/UIs



Automa<c
AAPT
compila<on
to
Android
packages



Eclipse
runs
your
app
on
the
emulator
or
your
G1
phone
if
it’s
connected



Debugging
is
straighlorward
with
the
DDMS
View

Example:
Hello
Android



Eclipse
demo
on
laptop



If
you
plan
to
develop
G1
apps,
you
should

work
through
Google’s
simple
“Note
Pad”

tutorial
(
http://code.google.com/android/

intro/tutorial.html
)
to
get

real
hands‐on

development
experience

Key
Mobile
App
Dev.
Principles



Mobile
handsets
are
resource‐constrained
devices:



Short
baXery
life



Small
amount
of
memory,
disk
space



Code
must
be

fast

–
users
will
only
tolerate
100–200

ms
latency

maximum

–
and
your
apps
must
be

responsive



Avoid
object
crea<on,
dynamic
lookup,
floats



Launch
resource‐intensive
opera<ons
in
separate
threads



Always
use
UI
elements
to
inform
user
of
“progress”



Save
state
and
data

whenever
app
could
be

interrupted
(which
is
oaen!)



Security:
compartmentalize
data
within
apps

Key
Android
Development
Principles



All
of
the
above

principles
obviously

apply
to
Android
apps,

with
a
few
caveats:



Android

non‐
determinis:cally

kills

processes
when
it’s
low

on
memory



Android’s
“nuclear

op<on”:
if
an
app

doesn’t
respond
within
5

seconds,
Android
kills
it

Android
App
Components



Android
apps
have
six
“building
blocks”:



Ac4vity
:
app
presenta<on
layer
(every
screen
in
the
app
is

an
Ac<vity)



Intent
:
instance
of
a
message‐passing
framework

(abstracted,
encapsulated
ac<ons)



Broadcast
Receiver
:
global
event
listener
that
executes

when
the
app
broadcasts
the
corresponding
Intent



Content
Provider
:
data
store

shared
across
apps



Service
:
app’s
background
worker,

e.g.
,
a
fall‐down

detector
for
seniors
that
watches
for
large
accelera<ons



No4fica4on
:
instance
of
a
user‐no<fica<on
framework

(can
make
sound,
vibrate
phone,
flash
LED)

The

AndroidManifest.xml

file



All
of
these
components
are
“bound
together”
in

the
app’s

AndroidManifest.xml

file



Ac<vi<es
and
Services
have

<activity>

and

<service>

tags,
respec<vely



The

<intent‐filter>

tag
says
what
Intents
launch

which
Ac<vi<es
and
Services



Two
permission‐related
tags
(for
security)



<uses‐permission>

tag
determines
what
permissions
users

must
grant
the
app
in
order
for
it
to
work
correctly,

e.g.
,

users
must
explicitly
allow
SMS
and
loca<on‐based
services



<permission>

tags
restrict
access
to
app



Eclipse
provides
a
GUI
to
edit
this
file

Other
App
Resources



Within
a
project’s

res

directory,
other
app
resources
can
be

stored,

e.g.
,



Simple
values
(strings,
colors,
dimensions,
arrays)



Drawables

(bitmaps
and

PNGs
)



Layouts
for
an
Ac<vity’s
UI
(
best
prac:ce

MVC
idiom)
as
well
as

UI
“styles”
and
“themes”



Anima<ons



XML



“Raw”
resources,

e.g.
,
MP3
files



Resources
can
be
dynamically
selected
at
run<me
for

different
languages,
screen
orienta<ons,
etc.



Eclipse
automa<cally
generates
the

R.java

file
with
all

your
app’s
resources,
GUI
layouts,
etc.
indexed
by
name

Android
App
Life
Cycle



Android
apps

have
no
control
over
their
life
cycles!



Process
classifica<on:



Ac<ve
Process
(Ac<vity
interac<ng
with
user,


handling
events)



Visible
Process
(Ac<vity
visible
in
background,


not

handling
events
–

e.g.
,
a
dialog
box
pops
up)



Started
Service
Process



Background
Process
(invisible
Ac<vity
with
no
Services)



Empty
Process
(terminated
process
that
Android
caches

for
performance)

Highest
priority

Lowest
priority

(most
likely
to
be
killed)

Ac4vity
Event
Handling



Android
programming
is

event‐driven



Ac<vity
event‐handling
methods
are
as
follows:



onCreate
()
:
called
at
Ac<vity’s
birth,

ini<alizes
it



onRestart()
:
called
aaer

onCreate()

finishes,
restores
UI
state



onStart()
:
called
when
Ac<vity
becomes

visible



onResume()
:
called
when
Ac<vity
becomes

ac<ve



onSaveInstanceState()
:
called
when

Ac<vity
becomes
invisible,

saves
UI
state

changes



onPause()
:
called
when
Ac<vity
becomes

inac<ve
(
save
state
here
)



onStop()
:
called
when
Ac<vity
becomes

invisible
(
save
state
here
)



onDestroy
()
:
called
at
Ac<vity’s
death



Your
Ac<vi<es
will
need
to
implement
some
or

all
of
these
methods

Source:

R.
Meier,

Professional
Android

Applica:on
Development

UI
Design
for
Apps



Good
UI
design
is
essen<al
for

your
app
to
provide
a
pleasant

user
experience



Android
provides
several

means
to
build
UIs
and
menus



Android’s
UI
terminology:



View
s
are
the
basic
class
for

visual
interface
controls
(also

known
as
widgets)



ViewGroup
s

extend
the

View

class
and
can
contain
mul<ple

View
s,

e.g.
,
compound
controls



Ac<vi<es
are
the
windows
or

screens
shown
to
user

Source:

R.
Meier,

Professional
Android

Applica:on
Development

Android
Widget
Views



Android
provides
many
standard

View
s,

e.g.
,



TextView
:
read‐only
text
label;



EditText
:
editable
text
entry
box



ListView
:
displays
values
of
items

as

TextView
s

in
a
list



Spinner
:
“combo
box”
composite

widget



TextView

showing
currently‐selected

item



Dialog
to
select
item
from
a

ListView

of
items



Button
,

CheckBox
,

RadioButton
,
…




See

many

more
examples
at


http://code.google.com/

android/reference/view‐
gallery.html

Android
View
Layouts



Android
provides
5

layouts

–

ViewGroup

extensions
that
control
child

widget
posi<ons
on
screen



FrameLayout
:
Pins
each
child

View

to
top
lea
corner,
each
new

View

obscures

the
last



LinearLayout
:
Adds
each
child

View

in
a
straight
line,
either
horizontal
or

ver<cal



RelativeLayout
:
Defines
posi<ons
of
each
child

View

rela<ve
to
other
child

views
and
screen
boundaries



TableLayout
:
Lays
out

View
s
in
a
grid
of
rows
and
columns



AbsoluteLayout
:
Defines
each
child

View
’s
posi<on
in

absolute
coordinates
,

can’t
dynamically
adjust
to
new
screen
resolu<ons
and
orienta<ons



Best
prac:ce:

define
layouts
and

View
s
in
XML
with
external
resources
as

opposed
to
genera<ng
them
in
code
(
c.f.

Java
Swing
dev.)



Google
provides
thorough
documenta<on
on
layouts
at

http://code.google.com/android/devel/ui/layout.html



Modifying
Views
and
Layouts



You
can
easily
modify
Android’s
exis<ng

View
s
and
layouts

by
defining
the
new

View

or
layout
in
XML
and
extending

the
respec<ve
class,

e.g.
,

TextView

and

LinearLayout



For
modifica<ons,
override
the
following
event‐handling

methods:



onDraw
()

method
to
draw
customized
widgets



onMeasure
()

method
to
determine
how
much
space
to
use




You

must

call

setMeasuredDimension
()

in
the
overridden

onMeasure
()

to

avoid
throwing
an
excep<on



onKeyDown
()
,

onKeyUp
()
,

onTrackballEvent
()
,

onTouchEvent
()

methods
handle
pressing
any
device
key,
releasing
a
pressed
key,

moving
trackball,
interac<on
with
touch
screen



Make
sure
you
put

@Override

before
the
method
defini<on,
call

corresponding

super.method
()

immediately



Best
prac:ce:

make
as
many
drawn
objects
class‐scoped
as

possible
and
create
them
in
the
constructor

Android’s
Menu
System



Android’s
menu
system
is
ini<ated
when
you
press
the

“Menu”
buXon
(obviously!)



The
menu
system
has
not
one
but

three

types
of
menus:



Icon
menu:
displays
up
to
6
items
with
or
without
icons,

no

checkboxes
or
radio
buXons



Expanded
menu:




If

there
are
more
than
6
items,
a
“More”
entry
appears
in
the
menu



Selec<ng
“More”
brings
up
the

expanded

menu,
which

can

include

checkboxes
and
radio
buXons



But
you

cannot

force
Android
to
display
the
expanded
menu

first



Submenu



When
user
selects
a
submenu
in
the
icon
menu,
a
dialog
box
pops
up



Checkboxes
and
radio
buXons
are
allowed
in
this
dialog



Confused
yet?
There’s
yet

another

type
of
menu…

Context
Menus



Context
menus
are

View
‐specific
and
are

triggered
by
pressing
trackball
or
screen
for
3

seconds



Example:
at
the
“home”
screen,
press
the
screen

for
a
few
seconds



You
can
move
icons
around,
delete
them



You
can
add
pictures,
Google
search,
an
analog
clock,

and
music
playlists
to
the
home
screen



If
you
swipe
the
screen
lea
and
right,
you’ll
find
a
total

of
3
“screens”
on
which
you
can
insert
these
items

Implemen4ng
Android
Menus
(1)



Override
Ac<vity’s

onCreateOptionsMenu(Menu

menu)

method
to
create

menu



Use
the

menu.add(groupID
,

menuItemID
,

itemOrder
,

itemText
)

call
to
add
a
menu
item



Checkboxes
:
Invoke

setCheckable(true
)

on
the

add()

call
above,

i.e.
,

add(…).
setCheckable(true
)


Radio
buXons
:
Add
several
menu
items
with
the
same

groupID
,
then
call

menu.setGroupCheckable(groupID
,

true,
true)


Shortcut
keys
:
Call

setShortcut(a
,

b
)

on
a

MenuItem

object,

e.g.
,

item
,
where

a

and

b

are
the
keys
to
use

with
a
numeric
keypad
and
full
keyboard,
respec<vely

Implemen4ng
Android
Menus
(2)



Condensed
4tles
:
Call

item

.
setTitleCondensed
(“…”)

to
set

icon

menu

text



Icons
:
Call

item.setIcon(R.drawable.iconName
)

to
set
the
icon

that’s
displayed

only

in
the

icon
menu



Make
sure
you
have
the
icon
file
stored
in

res/
drawable

directory
(Eclipse

automa<cally
updates

R.java

so
icon
can
be
sta<cally
accessed
by
name)



.PNG
files
are
preferred,

e.g.
,

iconName.png



Click
listener
:
call

item.setOnMenuItemClickListener
()


This
is
inefficient;
you
should
override

onOptionsItemSelected
()


Intent
:
Call

item.setIntent
()

to
trigger
an
Intent



Caveat:
item

must
not
already

be
handled
by
the
Ac<vity’s

onOptionsItemSelected
()

method
or
a
click
listener



Within

onCreateOptionsMenu
()
,
you
can
dynamically
update
menu



You
can
also
create
submenus
by
calling

menu.addSubMenu
()



No
nested
submenus
allowed



Call

setIcon
()
,

setHeaderIcon
()

on

SubMenu

to
set
icon,
header
icon

Implemen4ng
Android
Menus
(3)



Override

onOptionsItemSelected
()

to
handle
menu

selec<ons



Simply
get
the

MenuItem
’s

ID
by
calling

getItemId
()

and
run

“dispatcher”
control
logic



Create
context
menus
in
one
of
two
ways:



Override

onCreateContextMenu
()

for
a
given

View


Best
prac:ce:

Override

onCreateContextMenu
()

to
create

Ac<vity‐specific
context
menus
and

register
the

View
s

that
use
it

by
calling

registerForContextMenu
()

in

onCreate
()


Context
menus
can
be
populated
dynamically
at
run<me
by

specifying

View

data
&
asking
if
other
Android
apps

support
ac<ons
for
it,

e.g.
,
copy/paste
text



Override

onContextItemSelected
()

to
handle
context

menu
selec<ons

Other
Topics



Please
see

Reto

Meier’s

Professional
Android
Applica:on

Development

book
for

much

more
detail
on
advanced

Android
programming,

e.g.
,
loca<on‐based
services



Presently,
there
are
certain
things
you

can’t

program
the
G1

to
do
–
yet



Record
music
as
it
is
played



Bluetooth
connec<vity



Google
Talk
instant
messaging



Video
recording



Accessing
the
underlying

phone

hardware



Google
may
add
some
of
this
func<onality
as
well
as
a

touchscreen

keyboard
in
an
upcoming
firmware/SDK

update
that
should
be
released
in
Q1
2009

Ques4ons
or
Comments?

Thank
you!

References



Reto

Meier,

Professional
Android
Applica:on

Development
,
Wiley,
2009
–

essen4al
reading



Online
documenta<on
at

http://code.google.com/android/
documentation