You are on page 1of 36

HC VIN CNG NGH BU CHNH VIN THNG

KHOA CNG NGH THNG TIN


***o0o***

BO CO THC TP TT NGHIP
ti: TM HIU V H IU HNH ANDROID
V XY DNG NG DNG MINH HA

Gio vin hng dn


Sinh vin

Lp

: PGS.TS. Nguyn Duy Phng

: Hong Khnh Dng


: Nguyn Th nh
: Nguyn Vn Cng
: H09CN7

Bo co thc tp tt nghip

H ni ngy 25 11 2011

GVHD : PGS.TS. Nguyn Duy Phng

-2-

Bo co thc tp tt nghip

Mc lc
A : TM HIU V JAVA.....................................................................................6
CHNG I : TNG QUAN V JAVA ..........................................................6
1.1. Gii thiu v java...................................................................................6
1.2. Java l g?...............................................................................................6
1.3. Cc c trng ca Java...........................................................................6
1.4. Cc loi chng trnh Java.....................................................................6
1.5. My o Java............................................................................................7
1.6. Cc gi chun ca Java...........................................................................7
1.7. c trng mi ca java2........................................................................7
1.8. L do chng ta chn Java.......................................................................7
CHNG 2 : NN TNG CA NGN NG JAVA...................................8
2.1. Cu trc mt chng trnh Java.............................................................8
2.2. Chng trnh Java u tin.....................................................................8
2.3. Cc bin..................................................................................................8
2.4. Cc ton t s hc .................................................................................8
2.4.1. Cc ton t s hc...........................................................................8
2.4.2. Ton t Bit......................................................................................8
2.4.3. Cc ton t quan h.........................................................................9
2.4.4. Cc ton t logic..............................................................................9
2.4.5. Cc ton t iu kin.......................................................................9
2.4.6. Ton t gn......................................................................................9
2.4.7. Th t u tin ca cc ton t.........................................................9
2.4.8. Thay i th t u tin....................................................................9
2.5. iu khin lung....................................................................................9
CHNG 3 : CC GI V GIAO DIN.....................................................11
3.1. Gii thiu..............................................................................................11
3.2. Lp String (lp chui)..........................................................................11
3.3. Lp Hashtable (bng bm)...................................................................12
3.4. Lp random..........................................................................................12
3.5. Lp Vector............................................................................................13
CHNG 4 : X L NGOI L (Exception Handling)............................14
4.1. Gii thiu..............................................................................................14
4.2. Mc ch ca vic x l ngoi l..........................................................14
4.3. X l ngoi l.......................................................................................15
4.4. M hnh x l ngoi l.........................................................................15
4.4.1. Cc u im ca m hnh catch v throw...................................16
4.4.2. Cc khi try v catch................................................................16
4.4.3. Cc khi cha nhiu Catch............................................................16
4.4.4. Khi finally.................................................................................16
4.4.5. Cc ngoi l c nh ngha vi lnh throw v throws..........16
4.4.6. Danh sch cc ngoi l..................................................................17
CHNG 5 : CU TRC CA GOOGLE ANDROID...............................18
GVHD : PGS.TS. Nguyn Duy Phng

-3-

Bo co thc tp tt nghip

5.1. Applications..........................................................................................18
5.2. The Application Framework.................................................................18
5.3. Libraries................................................................................................19
5.4. Android Runtime..................................................................................19
5.5. Linux Kernel.........................................................................................19
B. LP TRNH C BN VI ANDROID.......................................................20
I. Ci t Eclipse v tch hp Android SDK vo Eclipse................................20
1.1. Dowload Android SDK........................................................................20
1.2. Tch hp Android SDK vo Eclipse.........................................................21
1.3. Android Virtual Device (Emulator)..........................................................23
II. Mt s chc nng cn bit khi lp trnh Android vi Eclipse:...................24
2.1. Debug cho ng dng Android:.............................................................24
2.2. File Explorer ca Android:...................................................................25
3.3. Device ca Android:.............................................................................26
3.4. To project Android trong Eclipse.......................................................26
3.5. Vng i ca Activity..........................................................................30
3.6. XML trong Android:............................................................................30
C. XY DNG NG DNG ANDROID........................................................32
I. Gii thiu v ng dng.................................................................................32
II. Mt s hnh nh demo ng dng................................................................32
D. Kt lun..........................................................................................................36

GVHD : PGS.TS. Nguyn Duy Phng

-4-

Bo co thc tp tt nghip

Li m u

Ngy nay in thoi di ng, ngy cng tr thnh mt vt khng th thiu. Nhc n
in thoi di ng l nhc n mt trong nhng pht minh hon ho nht ca nhn loi t
trc n nay. Nh c in thoi di ng, nn kinh t - x hi ngy cng pht trin, i sng
tinh thn ca con ngi ngy cng phong ph. Cho n hm nay, vai tr ca in thoi di
ng trong i sng vt qua ro cn ca mt cng c, m tr thnh mt biu tng, c
trng mi ca x hi hi loi ngi trong th k 21. in thoi cho php ch nhn lin lc,
nhn tin v lm nhiu th khc. N theo st ngi dng mi ni mi lc.
Trc thc t mt h iu hnh di ng mi c google mua li nm 2005 v trnh
lng vo nm 2007 hin ang lm ma lm gi trn th trng in thoi thng minh th gii
h iu hnh ANDROID (robot c hnh dng con ngi).
H IU HNH ANDROID TRN IN THOI DI NG
Android l h iu hnh trn in thoi di ng (v hin nay l c trn mt s u pht
HD, HD Player, TV) pht trin bi Google v da trn nn tng Linux.
Cc nh pht trin vit ng dng cho Android da trn ngn ng Java. S ra mt ca
Android vo ngy 5 thng 11 nm 2007 gn vi s thnh lp ca lin minh thit b cm tay m
ngun m, bao gm 78 cng ty phn cng, phn mm v vin thng nhm mc nh to nn
mt chun m cho in thoi di ng trong tng lai.

GVHD : PGS.TS. Nguyn Duy Phng

-5-

Bo co thc tp tt nghip

Trc khi tm hiu v h iu hnh Android ta tm hiu s qua v ngn ng java, ngn
ng chnh s dng vit chng trnh chy trn h iu hnh Android.

A : TM HIU V JAVA
CHNG I : TNG QUAN V JAVA
1.1. Gii thiu v java
Java l mt ngn ng lp trnh do cng ty Sun Microsystems pht trin vo u thp k
1990. Xut pht im ca ngn ng ny l mt d n nghin cu ca cng ty trong lc u
cc nh nghin cu dng C++. Ngay sau nhm nghin cu cho rng h cn mt th g
tt hn th. Cc nh nghin cu ny u l cc lp trnh vin xut sc, do vy h bt tay
ngay vo vic xy dng mt ngn ng lp trnh mi bng cch tn dng cc tng hay ca
cc ngn ng lp trnh khc. Ngi thit k Java l ng James Gosling cn nh lp trnh trin
khai phin bn chng trnh dch Java cng cng u tin l ng Arthur van Hoff. Tn gi
"Java" l do nh tip th Kim Polese t cho ngn ng ny hm nhng c im phong ph v
th v ca n.
1.2. Java l g?
Java l ngn ng lp trnh hng i tng, do vy khng dng Java vit mt
chng trnh hng chc nng. Java c th gii quyt hu ht cc cng vic m cc ngn ng
khc c th lm c.
Java l ngn ng va bin dch va thng dch. Java c s dng rng ri vit
chng trnh chy trn Internet. N l ngn ng lp trnh hng i tng c lp thit b,
khng ph thuc vo h iu hnh. N khng ch dung vit cc ng dng chy n l hay
trong mng m cn xy dng cc trnh iu khin thit b cho in thoi di ng, PDA
1.3. Cc c trng ca Java.
n gin
Hng i tng
c lp phn cng v h iu hnh
Mnh m
Bo mt

Phn tn

a lung
ng

1.4. Cc loi chng trnh Java


Applets
ng dng c lp (console Application)
ng dng giao din (GUI Application)
Servlet
ng dng c s d liu

GVHD : PGS.TS. Nguyn Duy Phng

-6-

Bo co thc tp tt nghip

1.5. My o Java
L mt phn mm da trn c s my tnh o
L tp hp cc mnh logic xc nh hot ng ca my tnh
c xem nh l mt h iu hnh thu nh
N thit lp lp tr tng cho:
Phn cng bn di
H iu hnh
M bin dch
1.6. Cc gi chun ca Java
Java.lang
Java.applet
Java.awt
Java.io
Java.util
Java.net
Java.awt.event
Java.rmi
Java.security
Java.sql
1.7. c trng mi ca java2
Swing
Ko v th
Java 2D API
m thanh
RMI
1.8. L do chng ta chn Java.
Ngy nay ngnh cng nghip sn xut thit b di ng trn th gii ang pht trin nhanh
chng. Cc thit b di ng ngy cng hin i hn, thng minh hn v ngy cng c nhiu
tnh nng ni bt. Chic in thoi di ng ngy nay chng khng ch n thun m nhn
chc nng hi thoi m cn c thm nhiu tnh nng khc nh chp nh, nghe nhc, xem phim,
lt web nh mt trung tm gii tr thu nh. Nhu cu pht trin phn mm cho cc thit b di
ng ngy cng tng cao v c nh gi l mt ngnh cng ngh y tim nng. Mt thc t
t ra cho cc nh pht trin phn mm trn cc thit b ny l hin nay khng c mt chun
ha no dnh cho cc nh sn xut phn cng. Cc thit b trn th trng hin nay rt a dng
v mang nhiu c im cng nh cu hnh khc nhau. Trc thc t , vic c th chy trn
nhiu mi trng l mt li th rt ln ca ngn ng Java.

GVHD : PGS.TS. Nguyn Duy Phng

-7-

Bo co thc tp tt nghip

CHNG 2 : NN TNG CA NGN NG JAVA


2.1. Cu trc mt chng trnh Java
Phn u ca mt chng trnh Java xc nh thng tin mi trng. lm c vic
ny, chng trnh c chia thnh cc lp hoc cc gi ring bit. Nhng gi ny s c ch
dn trong chng trnh. Thng tin ny c ch ra vi s tr gip ca lnh nhp import. Mi
chng trnh c th c nhiu hn mt lnh nhp. Di y l mt v d v lnh nhp:
2.2. Chng trnh Java u tin
// This is a simple program called First.java
import java.io.*;
class First
{
public static void main(String args[])
{
System.out.println(My first program in Java);
}
}
bin dch m ngun, ta s dng trnh bin dch java. Trnh bin dch xc nh tn ca file
ngun ti dng lnh nh m t di y:
C:\jdk1.2.1\bin>javac First.java
2.3. Cc bin
Cc ng dng s dng cc bin luu tr cc d liu cn thit hoc cc d liu c
to ra trong qu trnh thc thi chng trnh. Cc bin c xc nh bi mt tn bin v c mt
phm vi tc ng. Phm vi tc ng ca bin c xc nh mt cch r rng trong chng
trnh. Mi bin c khai bo trong mt khi chng trnh ch c tc ng trong phm vi khi
, khng c ngha v khng c php truy nhp t bn ngoi khi.
C php:
Datatype indentifier [=value] [, indentifier[=value] ];
2.4. Cc ton t s hc
Mt chng trnh thc t bao hm vic to ra cc bin. Cc ton t kt hp cc gi tr
n hoc cc biu thc con thnh nhng biu thc mi, phc tp hn v c th tr v gi tr.
iu ny lin quan n vic thc hin cc php ton logic, s hc, quan h v so snh trn cc
biu thc.
Java cung cp nhiu dng ton t sau:
2.4.1. Cc ton t s hc
Cc ton hng ca cc ton t s hc phi dng s. Cc ton hng kiu boolean khng
s dng c, song cc ton hng k t cho php s dng loi ton t ny.
2.4.2. Ton t Bit
Cc ton t dang Bit cho php ta thao tc trn tng Bit ring bit trong cc kiu d liu
nguyn thu. Ton t Bit da trn c s i s Boolean. N thc hin php tnh trn hai bit c
v tr tng ng trn hai ton hng to ra mt kt qa mi.
GVHD : PGS.TS. Nguyn Duy Phng

-8-

Bo co thc tp tt nghip

2.4.3. Cc ton t quan h


Cc ton t quan h kim tra mi quan h gia hai ton hng. Kt qu ca mt biu
thc c dng cc ton t quan h l nhng gi tr Boolean (logic ng hoc sai). Cc ton
t quan h c s dng trong cc cu trc iu khin.
2.4.4. Cc ton t logic
Cc ton t logic lm vic vi cc ton hng Boolean.
2.4.5. Cc ton t iu kin
Ton t iu kin l mt loi ton t c bit v n gm ba thnh phn cu thnh biu
thc iu kin
2.4.6. Ton t gn
Ton t gn (=) dng gn mt gi tr vo mt bin. Bn c th gn nhiu gi tr n
nhiu bin cng mt lc.
2.4.7. Th t u tin ca cc ton t
Cc biu thc c vit ra ni chung gm nhiu ton t. Th t u tin quyt nh trt
t thc hin cc ton t trn cc biu thc. Bng di y lit k th t thc hin cc ton t
trong Java
Th t
Ton t
1.
Cc ton t n nh +,-,++,-2.
Cc ton t s hc v cc ton t dch nh *,/,+,-,<<,>>
3.
Cc ton t quan h nh >,<,>=,<=,= =,!=
4.
Cc ton t logic v Bit nh &&,II,&,I,^
5.
Cc ton t gn nh =,*=,/=,+=,-=
Bng 2.1 Th t u tin
2.4.8. Thay i th t u tin
thay i th t u tin trn mt biu thc, bn c th s dng du ngoc n ().
Tng phn ca biu thc c gii hn trong ngoc n c thc hin trc tin. Nu bn s
dng nhiu ngoc n lng nhau th ton t nm trong ngoc n pha trong s thc thi trc,
sau n cc vng pha ngoi. Nhng trong phm vi mt ngoc n th quy tc th t u tin
vn gi nguyn tc dng.
2.5. iu khin lung
Tt c cc mi trng pht trin ng dng u cung cp mt quy trnh ra quyt nh
(decision-making) c gi l iu khin lung, n iu khin vic thc thi ng dng. iu
khin lung cho php ngi pht trin phn mm kim tra s tn ti ca mt iu kin no
v ra quyt nh ph hp vi iu kin .
Vng lp l mt cu trc chng trnh gip bn c th dng thc hin vic lp li cc
hnh ng khi thc thi chng trnh m khng cn vit li cc on chng trnh nhiu ln.
iu khin r nhnh
Mnh if-else
Mnh swich-case
Vng lp (Loops)
Vng lp while
GVHD : PGS.TS. Nguyn Duy Phng

-9-

Bo co thc tp tt nghip

Vng lp do-while
Vng lp for
Cu lnh if-else
Cu lnh if-else kim tra kt qu ca mt iu kin v thc thi mt thao tc ph hp
trn c s kt qu .
C php:
If (condition)
{action 1 statements;}
Else
{action 2 statements;}
Cu lnh switch-case
Pht biu switch-case c th c s dng ti cu lnh if-else. N c s dng trong
tnh hung mt biu thc cho ra nhiu kt qu.
C php
swich (expression)
{
case value:action 1 statement;
break;
case value:action 2 statement;
break;
:
:
case valueN: actionN statement; break;
default: default_action statement;
}
Vng lp While
Vng lp while c s dng khi vng lp c thc hin mi cho n khi iu kin
thc thi vn l True.
C php
while(condition)
{
action statement;
:
:
}
Vng lp do..while
C php
Do
{
Action statements;
:
:
}while(condition);
Vng lp for
C php

GVHD : PGS.TS. Nguyn Duy Phng

- 10 -

Bo co thc tp tt nghip

For(initialization statements; condition; increment statements)


{
Action statements;
:
:
}
Mng-Array
Mng=Nhm tr cng kiu, k nhau, cng tn gi, truy xut phn t bng ch s i t 0.
nh ngha c ch nh s phn t (size) cp b nh
char ch[] = new char [5];
nh ngha mng tc thi (in-line initialization):
int a[]={1,4,2,7,8}; //hoc
int [] a={1,4,2,7,8};
Phn t c tham kho qua ch s bt u t 0
Ch khai bo: khng ch nh size: long a[];
Khi s dng phi cp b nh: a=new long [20];
Tr mc nh: ton t new s xa b nh, cc bit=0.
CHNG 3 : CC GI V GIAO DIN
3.1. Gii thiu
Gi v giao din l hai thnh phn chnh ca chng trnh Java. Cc gi c lu tr
theo kiu phn cp, v c nhp (import) mt cch tng minh vo nhng lp mi c nh
ngha. Cc giao din c th c s dng ch nh mt tp cc phng thc. Cc phng
thc ny c th c hin thc bi mt hay nhiu lp.
3.2. Lp String (lp chui)
Chui l mt dy cc k t. Lp String cung cp cc phng thc thao tc vi cc
chui. N cung cp cc phng thc khi to (constructor) khc nhau:
String str1 = new String( );
String str2 = new String(Hello World);
char ch[] = {A,B,C,D,E};
String str3 = new String(ch);
String str4 = new String(ch,0,2);
Cc phng thc ca lp String
Trong phn ny, ta s xt cc phng thc ca lp String.
charAt( )
startsWith( )
endsWith( )
copyValueOf( )
toCharArray( )
indexOf( )
toUpperCase( )
toLowerCase( )
trim()
GVHD : PGS.TS. Nguyn Duy Phng

- 11 -

Bo co thc tp tt nghip

equals()

3.3. Lp Hashtable (bng bm)


Lp Hashtable m rng lp tru tng Dictionary, lp ny cng c nh ngha trong
gi java.util. Hashtable c s dng nh x kho (key) n gi tr (value). V d, n c th
c s dng nh x cc tn n tui, nhng ngi lp trnh n nhng d n, chc danh
cng vic n lng, v c nh vy.
Hashtable m rng kch thc khi cc phn t c thm vo. Khi mt Hashtable mi,
bn c th ch nh dung lng ban u v yu t np (load factor). iu ny s lm cho
hashtable tng kch thc ln, bt c lc no vic thm vo mt phn t mi lm vt qua gii
hn hin hnh ca Hashtable. Gii hn ca Hashtable l dung lng nhn ln bi cc yu t
c np
Phng thc khi to Hashtable:
Hashtable(int)
Hashtable(int,float)
Hashtable()
Nhng phng thc ca lp Hashtable

clear()

done()
contains(Object)
containsKey(Object)
elements()
get(Object key)
isEmpty()
keys()
put(Object,Object)
rehash()
remove(Object key)
size()
toString()

3.4. Lp random
Lp ny l mt b to s gi ngu nhin (pseudo-random). C hai phng thc xy
dng c nh ngha. Mt trong nhng phng thc xy dng ny ly gi tr khi u (seed)
nh mt tham s. Phng thc xy dng khc khng c tham s, v s dng thi gian hin ti
nh mt gi tr khi u.
nextDouble()
nextFloat()
nextGaussian()

nextInt()
nextLong()
Phng thc khi to (Constructors):
Random()
Random(long)
GVHD : PGS.TS. Nguyn Duy Phng

- 12 -

Bo co thc tp tt nghip

Nhng phng thc ca lp Random


nextDouble()
nextFloat()
nextInt()
nextLong()
setSeed(long)
3.5. Lp Vector
Mt trong cc vn vi mt mng l chng ta phi bit n ln nh th no khi chng
ta to n. Trong thc t c nhiu trng hp khng th xc nh kch thc ca mng trc khi
to n.
Lp Vector ca Java gii quyt vn ny. N cung cp mt dng mng vi kch thc
ban u, mng ny c th tng thm khi nhiu phn t c thm vo.
Ti bt k thi im, mt lp Vector c dung lng lu tr mt s lng no cc
phn t. Khi mt lp Vector dng ht dung lng ca n, th dung lng ca n c gia tng
bi mt s lng ring cho Vector . Lp Vector cung cp ba phng thc xy dng khc
nhau m c th chng ta ch nh dung lng khi to, v tng s lng ca mt Vector, khi n
c to ra.
Mt phn c thm vo mt lp Vector bng cch s dng phng thc
addElement(). Tng t, mt phn t c th c thay th bng cch s dng phng thc
setElementAt().
Phng thc khi to (Constructors):
Vector(int)
Vector(int,int)
Vector()
Nhng phng thc ca lp Vector
addElement(Object)
capacity()
clone()
contains(Object)
copyInto(Object [])
elementAt(int)
elements()

ensureCapacity(int)

firstElement()
indexOf(Object)
indexOf(Object, int)
insertElementAt(Object, int)
isEmpty()
lastElement()
lastIndexOf(Object)
lastIndexOf(Object, int)
removeAllElements()

GVHD : PGS.TS. Nguyn Duy Phng

- 13 -

Bo co thc tp tt nghip

removeElement(Object)
removeElementAt(int)
setElementAt(Object, int)
setSize(int)
Size()
toString()
trimToSize()
Lp StringTokenizer
Mt lp StringTokenizer c th s dng tch mt chui thnh cc phn t (token)
nh hn. V d, mi t trong mt cu c th coi nh l mt token. Tuy nhin, lp
StringTokenizer i xa hn vic phn tch cc t trong cu. tch ra cc thnh token ta c
th tu bin ch ra mt tp du phn cch cc token khi khi to i tng StringTokenizer.
Nu ta khng ch ra tp du phn cch th mc nh l du trng (space, tab, ...). Ta cng c th
s dng tp cc ton t ton hc (+, *, /, v -) trong khi phn tch mt biu thc.
Phng thc khi to (Constructors):
StringTokenizer(String)
StringTokenizer(String, String)
StringTokenizer(String, String, Boolean)
Nhng phng thc ca lp StringTokenizer
countTokens()
hasMoreElements()

hasMoreTokens()

nextElement()
nextToken()
nextToken(String)

CHNG 4 : X L NGOI L (Exception Handling)


4.1. Gii thiu
Exception l mt loi li c bit. Li ny xut hin vo lc thc thi chng trnh. Cc
trng thi khng bnh thng xy ra trong khi thi hnh chng trnh to ra cc exception.
Nhng trng thi ny khng c bit trc trong khi ta ang xy dng chng trnh. Nu bn
khng x l cc trng thi ny th chng trnh c th b kt thc t ngt. Ngn ng Java
cung cp c ch dng x l ngoi l rt hiu qu. Vic x l ny lm hn ch ti a trng
hp h thng b hng (crash) hay h thng b ngt t ngt. Tnh nng ny lm cho Java tr
thnh mt ngn ng lp trnh mnh.
4.2. Mc ch ca vic x l ngoi l
Mt chng trnh nn c c ch x l ngoi l thch hp. Nu khng, chng trnh s b
ngt khi mt ngoi l xy ra. Trong trng hp , tt c cc ngun ti nguyn m h thng
cp khng c gii phng. iu ny gy lng ph ti nguyn. trnh trng hp ny, tt c
cc ngun ti nguyn m h thng cp nn c thu hi li. Tin trnh ny i hi c ch x l
ngoi l thch hp.

GVHD : PGS.TS. Nguyn Duy Phng

- 14 -

Bo co thc tp tt nghip

4.3. X l ngoi l
Khi mt ngoi l xy ra, i tng (object) tng ng vi ngoi l c to ra. i
tng ny sau c truyn cho phng thc l ni m ngoi l xy ra. i tng ny cha
thng tin chi tit v ngoi l. Thng tin ny c th c nhn v v c x l. Cc mi trng
runtime nh IllegalAccessException, EmptyStackException v.v c th to ra ngoi l.
Chng trnh i khi c th t to ra ngoi l. Lp Throwable c Java cung cp l lp trn
cng ca lp Exception (lp u tin trong cy tha k), lp ny l lp cha ca tt c cc ngoi
l khc.
4.4. M hnh x l ngoi l
Trong Java, m hnh x l ngoi l gim st vic thc thi m pht hin ngoi l. M
hnh x l ngoi l ca Java c gi l catch and throw. Trong m hnh ny, khi mt ngoi
l xy ra, ngoi l s b chn v chng trnh chuyn n mt khi x l ngoi l.
Ngn ng Java cung cp 5 t kho sau x l cc ngoi l:
try
catch
throw
throws
finally
Di y l cu trc ca m hnh x l ngoi l:
try
{
// on m c kh nng gy ra ngoi l
}
catch(Exception e1)
{
// Nu cc lnh trong khi try to ra ngoi l c loi e1, th thc hin //x l ngoi l
nu khng chuyn xung khi 'catch' tip theo
}
catch(Exception e2)
{
// Nu cc lnh trong khi try to ra ngoi l c loi e2, th thc hin //x l ngoi l
nu khng chuyn xung khi 'catch' tip theo
}
catch(Exception eN)
{
// Nu cc lnh trong khi try to ra ngoi l c loi eN, th thc hin //x l ngoi l
nu khng chuyn xung khi 'catch' tip theo
}
finally
{
// khi lnh nay lun c thc hin cho d ngoi l c xy ra hay khng.
}
GVHD : PGS.TS. Nguyn Duy Phng

- 15 -

Bo co thc tp tt nghip

4.4.1. Cc u im ca m hnh catch v throw


M hnh catch v throw c hai u im:
Ngi lp trnh ch phi x l ngoi l khi cn thit. Khng cn phi thc hin ti mi mc.
Thng bo li c th c hin ra khi tin hnh x l ngoi l.
4.4.2. Cc khi try v catch
Khi try-catch c s dng thi hnh m hnh catch v throw ca vic x l
ngoi l. Khi try cha mt tp lnh c th thi hnh c. Cc ngoi l c th b chn khi thi
hnh tp lnh ny. Phng thc c kh nng to ra ngoi l c th c khai bo trong khi
try. Mt hay nhiu khi catch c th theo sau mt khi try. Cc khi catch ny bt cc
ngoi l c kh nng to ra trong trong khi try.
4.4.3. Cc khi cha nhiu Catch
Nhiu khi catch x l cc loi ngoi l khc nhau mt cch c lp. Chng c lit
k trong on m sau:
try{
doFileProcessing();
displayResults();
}catch(LookupException e){
// e LookupException object
handleLookupException(e); // phng thc x l li do ngi s dng
//nh ngha
}
catch(Exception e)
{
System.err.println(Error: + e.printStackTrace());
}
4.4.4. Khi finally
Khi mt ngoi l xut hin, phng thc ang c thc thi c th b dng m khng
c hon thnh. Nu iu ny xy ra, th cc on m pha sau (v d nh on m c chc
nng thu hi ti nguyn, nh cc lnh ng tp vit cui phng thc) s khng bao gi c
gi. Java cung cp khi finally gii quyt vic ny. Khi finally thc hin tt c cc vic
thu dn khi mt ngoi l xy ra.
Cc lnh ny bao gm:
ng tp tin.
ng ResultSet (c s dng trong chng trnh c s d liu).
ng li cc kt ni c to trong c s d liu.
4.4.5. Cc ngoi l c nh ngha vi lnh throw v throws
Cc ngoi l c th c to ra bng cch s dng t kho throw. T kha throw ch
ra mt ngoi l va xy ra. Ton hng ca throw l mt i tng thuc lp c tha k t
Throwable.
on lnh sau ch ra cch s dng ca lnh throw:
try
{
GVHD : PGS.TS. Nguyn Duy Phng

- 16 -

Bo co thc tp tt nghip

if (flag<0)
{
throw new MyException(); // user-defined
}
}
Mt phng thc c th to ra nhiu ngoi l. lm c iu ny ny, ta ch cn lit
k danh sch cc ngoi l m phng thc c th to ra trong phn nh ngha phng thc.
Gi s rng phng thc x() gi phng thc y(). Phng thc y() to ra mt ngoi l
nhng khng c x l. Trong trng hp ny, phng thc gi x() nn khai bo rng n c
kh nng to ra ngoi l nh ngoi l ca phng thc c gi y(). Ta nn khai bo khi try
catch trong phng thc x() m bo rng ngoi l khng c truyn cho cc phng thc
m gi phng thc ny (phng thc gi x()).
4.4.6. Danh sch cc ngoi l
Ngoi l
RuntimeException
ArthmeticException
IllegalAccessException
IllegalArgumentException
ArrayIndexOutOfBoundsExeption
NullPointerException
SecurityException
ClassNotFoundException
NumberFormatException
AWTException
IOException
FileNotFoundException
EOFException
NoSuchMethodException
InterruptedException

Lp cha ca th t phn cp ngoi l


Lp c s cho nhiu ngoi l java.lang
Li v s hc, v d nh chia cho 0.
Lp khng th truy cp.
i s khng hp l.
Li trn mng.
Khi truy cp i tng null.
C ch bo mt khng cho php thc hin.
Khng th np lp yu cu.
Vic chuyn i t chui sang s thc khng thnh
cng.
Ngoi l v AWT
Lp cha ca cc lp ngoi l I/O
Khng th nh v tp tin
Kt thc mt tp tin.
Phng thc yu cu khng tn ti.
Khi mt lung b ngt.

Bng 4.1 Danh sch mt s ngoi l

GVHD : PGS.TS. Nguyn Duy Phng

- 17 -

Bo co thc tp tt nghip

CHNG 5 : CU TRC CA GOOGLE ANDROID


Cch d dng nht c th hnh dung cu trc ca Android ging nh mt ngi nh c 5
cn phng. Tn nhng cn phng ny ging nh trong hnh v: Applications, Application
Framework, Libraries, Android Runtime, Linux Kernel. By gi tip tc hnh dung, mi cn
phng cha mt s lng ngi nht nh. Mi ngi tng ng l nhng nt mu v tn
ca nhng ngi ny cng th hin trn cc nt mu . Nhng phng khc nhau s c s
lng ngi khc nhau. Mi ngi s c mt chc nng ring bit g trong cn nh mang
tn Android.

Hnh : Cu trc h iu hnh Android


5.1. Applications
Cn phng u tin bn bc vo khi n vi ngi nh Android. Tt c nhng ngi
trong cn phng ny chnh l cc ng dng m bn c trong thit b chy Android. L nhng
th m bn d dng thy c trn mn hnh: Phone, Contact, cc tr chi, chng trnh bn
ci vo v mt s ng dng chy ngm m bn khng thy c.
Hu ht ng dng c vit bng Java (hoc C). C th v cn phng ny ging nh cn
phng ring ca bn, bn c th cho ai vo (ci t ng dng) hay nu khng a ai
th c th tin h i (g b ng dng) ty thch. Nhng phng cn li bn khng c quyn
lm nh th.
5.2. The Application Framework

GVHD : PGS.TS. Nguyn Duy Phng

- 18 -

Bo co thc tp tt nghip

Cn phng ny gm nhng ngi c quyn cao hn nhng ngi trong phng


Applications. Nhng ngi ny s c nhng nhim v qun l nhng ngi phng
Applications.
V d: Content Provider cho php chia s thng tin gia cc ng dng, Resource Mananger:
qun l cc vn lin quan n ha, cc file layout.., Notification Manager qun l cc cnh
bo: ting bp, n led Activity Manager: qun l hot ng ca cc ng dng.
Cc framework ny ging nh l b ng dng sn ca Android. C rt nhiu nhn vt
trong cn phng ny cng vic ca h cc bn cng c th on ra thng qua tn gi ca h.
M a s h u l Qun L (Manager) hy Nh cung cp (Provider).

5.3. Libraries
Cn phng ny ging nh nh bp ca ngi nh. Tt c mi ngi phng Applications
hay Application Framework mun lm vic th phi ly mt th g trong phng ny cho vo
y th mi lm vic c. Ngha l khi ng dng chy s gi cc hm nm trong th vin ny.
Cc lp trnh vin cng s dng nhng hm trong cc th vin ny pht trin ng dng.
V d: Media Framework s c gi khi cc chng trnh c lin quan n Media nh nghe
nhc, xem nh. Hay WebKit s lin quan n Internet, SQLite lin quan n c s d liu
Mt chng trnh c th phi cn nhiu th vin trong cn phng ny.
5.4. Android Runtime
C th ni rng cn phng Android runtime l mt ni kh c bit. N ch c 2 th:
Dalvik Virtual Machine v nhng th vin nhn (core libraries). Nhng th vin c bn ca h
iu hnh, ch cung cp cho h iu hnh.
Trong Google Android, c mt cng c c gi l DX n s chuyn nhng ng dng
thnh dng Dalvik Executable (.dex). y chnh l nhng file c bit dng cho Dalvik Virtual
Machine. nh dng ny cng c to ra nhm lm ti thiu kch thc chng trnh, lm cho
n tng thch vi thit b di ng. Dalvik Virtual Machine l mt chng trnh c vit
Android c th chy a nhim nhanh v mt.
5.5. Linux Kernel
Cn phng nh ny cha nhng trnh iu khin (drivers) dng iu khin phn cng
nh Keypad, Wifi, Camera, Audio, Mn hnh Phng ny c th coi nh l Trung tm ch
huy ca ngi nh. Linux Kernel nm gi nhng g l ct li ca h iu hnh. Cc gii thut
qun l ti nguyn, chuyn i qua li gia cc tc v, phn chia quyn, gii quyt tranh chp
cng c th xem n nh l phng cng tc i ngoi v n m nhn vic tng tc vi thit
b ngoi vi. N chnh l nhn Linux 2.6.

GVHD : PGS.TS. Nguyn Duy Phng

- 19 -

Bo co thc tp tt nghip

B. LP TRNH C BN VI ANDROID
I. Ci t Eclipse v tch hp Android SDK vo Eclipse
1.1. Dowload Android SDK
Android SDK thc cht l tp hp cc cng c v th vin pht trin cc ng dng
trn nn tng h iu hnh Android.
Bc 1: Vo trang http://developer.android.com/sdk/index.html ti Android SDK
Starter. Ty thuc vo h iu hnh m chn bn Mac, Linux hay Window. y em chn ti
bn cho Window.

Bc 2: Gii nn file zip va ti v. Chy SDK Setup.exe. Ta c th gp thng bo li


Fetching https://dl-sl... Failed to fetch... Close thng bo ny li. Tip theo ca s Choose
Packages to Install xut hin. Nu ca s ny trng rng
Cancel.
Quay v ca s Android SDK and AVD manager
Chn Setting, nh du vo Force https://...
Chn Available Packages

Bc 3: nh du cc Packages bn mun ti:


Documents chnh l phn Javadoc m t hot ng ca cc phng thc v cc lp
(phn ny nh cun t in v cc lp, cc phng thc trong java), Sample l cc on code
mu, SDK Platform ng vi cc phin bn h iu hnh (2.2 - API level 8, 2.1 - API level 7,...),
v Google API pht trin cc phn mm lin quan n dch v ca Google (nh Google Map
nu mun lp trnh lin quan n GPS).
GVHD : PGS.TS. Nguyn Duy Phng

- 20 -

Bo co thc tp tt nghip

C th ti ht, cn mun ti u th c th nh du nh mnh (lu USB drivers ch


dnh cho ngi s dng Windows v mun pht trin ng dng test bng in thoi tht).
Install Selected
Install
Ca s Install hin ra
Ngi ch

1.2. Tch hp Android SDK vo Eclipse


Bc 1: Ti Eclipse nu bn cha c. Ti Eclipse for Java Developers, hoc Eclipse
for Java and Report Developers.
Bc 2: Khi chy Eclipse, vo Help Install new softwares. Chn Add, g vo
Name tn bn mun v Location g vo a ch ti v ADT
HTML Code:
https://dl-ssl.google.com/android/eclipse/
Nu https khng hot ng. Ngoi ra bn cng c th ti thng ADT v my theo link
http://dl.google.com/android/ADT-0.9.7.zip (bn mi nht 0.9.7 ng vi Android 2.2), chn
Archive v browse ti file ny (lu khng gii nn)
OK
Check vo phn di Name (s hin ra dng Developer Tools).

GVHD : PGS.TS. Nguyn Duy Phng

- 21 -

Bo co thc tp tt nghip

Bc 3: Next, next, Accept, next,...Finish (nh Install mi chng trnh bnh thng).
Bc 4: Eclipse Windows Preferences Android
Nhn nt Browse v chnh ng dn ti th mc ca Android SDK bn ti lc trc.
Apply
OK

GVHD : PGS.TS. Nguyn Duy Phng

- 22 -

Bo co thc tp tt nghip

1.3. Android Virtual Device (Emulator)


AVD l my o cho developer pht trin ng dng. to 1 AVD bn vo Windows
Android SDK and AVD Manager Virtual Devices chn New.
Ca s Create new AVD hin ra, bn in thng tin cho AVD bn mun:
Name: Ty (nhng ch c s dng cc k t "a-z", "A-Z", ".-_", ngha l c
khong trng cng khng c).
Target: Chn phin bn h iu hnh bn mun (thng mnh to mt Android 1.6 v
mt Android 2.2 test).
SD Card: g vo Size SD card o cho AVD, hoc chnh ti file c sn. Nhiu AVD
c th dng chung 1 Sdcard (ch cn to ln u, cc ln sau chnh ng dn ti file ).
Skin: c th Default (HVGA) hoc chn kch c mn hnh bn mun. Ch cn quan tm ti 3
option: HVGA (phn gii 320-480 nh G1, G2, i5700...), QVGA (240-320 nh HTC
Wildfire...), WVGA854 (480-854 nh Milestone, NexusOne...)
Create AVD

GVHD : PGS.TS. Nguyn Duy Phng

- 23 -

Bo co thc tp tt nghip

II. Mt s chc nng cn bit khi lp trnh Android vi Eclipse:


2.1. Debug cho ng dng Android:
Nhiu ngi chuyn t J2SE hoc J2ME sang Android s ngc nhin v cu lnh debug
System.out.println() khng cn in ra trn ca s Console na. Google thay th n bng
Logcat, mt ca s ghi li ton b hot ng ca h iu hnh. m Logcat, trc tin cc
bn chn Window Open Perspective Debug. Nu ko thy option Debug th chn Other v
tm Debug trong ca s mi hin ra. Sau chn tab Debug mi xut hin gc trn bn phi
ca Eclipse.

GVHD : PGS.TS. Nguyn Duy Phng

- 24 -

Bo co thc tp tt nghip

2.2. File Explorer ca Android:


File Explorer l mt chc nng hu ch Google a vo gip chng ta qun l file trong
sd card v c file system data (ch qun l c ca emulator, khng th truy nhp system data
ca thit b tht). FE gip bn d dng a file vo / ly file ra trong sdcard o ca emulator,
xa c s d liu ca chng trnh khi to li (only emulator)...
M FE bng cch vo:
Window Show View Others Android File Explorer.
FE trong ca s Debug cho tin qun l.

GVHD : PGS.TS. Nguyn Duy Phng

- 25 -

Bo co thc tp tt nghip

3.3. Device ca Android:


Device cng l mt chc nng hu ch na trong Android gip bn qun l thit b o cng
nh tht ca mnh. M Device bng cch vo Window Show View Device hoc vo:
Window Show View Others Android Device.

3.4. To project Android trong Eclipse


B1: Khi to 1 project
Vo th File New Android Project. Nu bn mi lp trnh Android ln u th c l dng
Android Project s khng hin ra, khi xung pha cui chn Other ri vo Android
Android Project.
B2: in thng tin cho project

GVHD : PGS.TS. Nguyn Duy Phng

- 26 -

Bo co thc tp tt nghip

Project name: Example


Kch next

GVHD : PGS.TS. Nguyn Duy Phng

- 27 -

Bo co thc tp tt nghip

Build Target: Chn Android 2.2 ty xem pht trin cho phin bn no, y em dng
bn 2.2 - v Next

GVHD : PGS.TS. Nguyn Duy Phng

- 28 -

Bo co thc tp tt nghip

Application name: Example


Package name: at.exam
Create Activity: ExampleActivity
=> Kch nt Finish.
Vy l bn to xong Project Android bt u pht trin ng dng.

GVHD : PGS.TS. Nguyn Duy Phng

- 29 -

Bo co thc tp tt nghip

3.5. Vng i ca Activity

- Entire lifetime: T phng thc onCreate( ) cho ti onDestroy( )


- Visible liftetime: T phng thc onStart( ) cho ti onStop( )
- Foreground lifetime: T phng thc onResume( ) cho ti onPause( )
Khi xy dng Actitvity cho ng dng cn phi vit li phng thc onCreate( ) thc
hin qu trnh khi to. Cc phng thc khc c cn vit li hay khng ty vo yu cu lp
trnh.
3.6. XML trong Android:
Khng ging nh lp trnh java thng thng, lp trnh android ngoi cc lp c vit
trong *.java cn s dng XML thit k giao din cho ng dng. Tt nhin bn hon ton c
GVHD : PGS.TS. Nguyn Duy Phng

- 30 -

Bo co thc tp tt nghip

th thit k 1 giao din nh mun m khng cn ti bt c 1 dng XML no, nhng s dng
XML s n gin cng vic i rt nhiu. ng thi s dng XML s gip vic chnh sa ng
dng sau ny tr nn d dng.
V nguyn tc, khi lp trnh ng dng ta thit k giao din bng XML v ci t cc x l khi
tng tc vi giao din trong code.
Mt s thnh phn c bn trong Android:
1. Cc layout:
+ Layout c dng qun l cc thnh phn giao din khc theo 1 trt t nht nh.
- FrameLayout: Layout n gin nht, thm cc thnh phn con vo gc trn bn tri ca mn
hnh.
- LinearLayout: thm cc thnh phn con theo 1 chiu nht nh (ngang hoc dc). y l
layout c s dng nhiu nht.
- RelativeLayout: thm cc thnh phn con da trn mi quan h vi cc thnh phn khc hoc
vi bin ca layout.
- TableLayout: thm cc thnh phn con da trn 1 li cc ngang v dc.
- AbsoluteLayout: thm cc thnh phn con da theo ta x, y.
Layout c s dng nhm mc ch thit k giao din cho nhiu phn gii. Thng
khi lp trnh nn kt hp nhiu layout vi nhau to ra giao din bn mong mun.
2. XML unit:
hiu c cc thnh phn c bn ca XML cng nh vic s dng XML kt hp vi
code, ta s i xy dng th mt chng trnh n gin.
Bn c th s dng chng trnh droiddraw thit k giao din v to ra file XML

Hnh nh v droiddraw

GVHD : PGS.TS. Nguyn Duy Phng

- 31 -

Bo co thc tp tt nghip

C. XY DNG NG DNG ANDROID


I. Gii thiu v ng dng
Hin nay tham gia giao thng l nhu cu thit yu ca mi con ngi. Khi chng ta
tham gia giao thng th khng trnh khi nhng lc s xut vi phm cc li c bn. Vic chnh
ph ban hnh cc vn bn quy nh v x pht vi phm hnh chnh trong lnh vc giao thng
ng b c a ln cc t bo cng nh ti ngi dn, nhng khng phi ai cng c th
nh ht c mc x pht ca cc li khi tham gia giao thng. Trong tnh hnh khi b c
quan thc thi php lut x pht th ai cng c nhu cu tm hiu v li m mnh vi phm th no,
quy nh ra sao v mc x pht hnh chnh cho li T thc t , cng nh nhu cu ca
mt s thnh vin trn cc din n cng ngh c mong mun c mt phn mm ch cn g li
vi phm vo s a ra mc x pht hnh chnh cho li . V phn mm ra i da trn
tng trn.
II. Mt s hnh nh demo ng dng
ng dng c ci t vo my Motorola Milestone

Khi chy ng dng

GVHD : PGS.TS. Nguyn Duy Phng

- 32 -

Bo co thc tp tt nghip

Chn tm li cho cc loi phng tin

GVHD : PGS.TS. Nguyn Duy Phng

- 33 -

Bo co thc tp tt nghip

G li vi phm vo : v d trong trng hp ny l giao thng. Chng trnh s t tm


kim tt c cc li c t giao thng a ln mn hnh.
Khi chng ta tm mt t di m chng trnh khng tm c kt qu no th s t ng
tch cc t ra v tm kim cc t n. Cc t tm kim c sp xp theo mc t cao xung
thp.

Chi tit cc li vi phm giao thng v mc tin pht.

GVHD : PGS.TS. Nguyn Duy Phng

- 34 -

Bo co thc tp tt nghip

Khi trong phn vi phm c cha cc im, cc khon th chng trnh s t ng tm kim cc
iu trong cc khon a ra bn di gip ngi dng bit c chnh xc cc im
nh th no.

GVHD : PGS.TS. Nguyn Duy Phng

- 35 -

Bo co thc tp tt nghip

D. Kt lun
Trn y l phn tm hiu ca chng em v H iu hnh Android v c xy dng mt
ng dng nhm kt li nhng g hc hi c.
Qua phn ny chng em bit cch xy dng nhng ng dng n gin cho Android
to mt bc m cho nhng phn mm hay v c ngha trong thc t.
Mc d c gng nhng do thi gian v kin thc cn c hn nn bi lm ch lm tng
qut cho nhng chc nng c bn, vi lng kin thc kh nhiu nn chng em cha nm bt
chi tit ht c. Trong thi gian ngn tm hiu v mt h iu hnh ln nn kh trnh khi
nhng sai st mong thy gio tn tnh ch bo cng nh gi v nhn xt.
Qua y xin chn thnh cm n thy gio PGS.TS. Nguyn Duy Phng tn tnh
hng dn chng em thi gian thc tp ny.
Chng em xin chn thnh cm n !

GVHD : PGS.TS. Nguyn Duy Phng

- 36 -