You are on page 1of 20

n tt nghip

Tm hiu v xy dng ng dng Phone


Book Safe Manager trn Android

Tm hiu v xy dng ng dng Phone Book Safe Manager trn Android

MC LC
CHNG 1: C S L THUYT..................................................................... 4
1. LCH S ANDROID .................................................................................. 4
2. DELVING VI MY O DALVIK ........................................................... 5
3. KIN TRC CA ANDROID .................................................................... 5
3.1. Tng ng dng .................................................................................... 6
3.2. Application framework........................................................................ 6
3.3. Library ................................................................................................ 7
3.4. Android Runtime................................................................................. 7
3.5. Linux kernel ........................................................................................ 8
4. ANDROID EMULATOR ........................................................................... 8
5. CC THNH PHN TRONG MT ANDROID PROJECT ....................... 9
5.1. AndroidManifest.xml .......................................................................... 9
5.2. File R.java ......................................................................................... 10
6. CHU K NG DNG ANDROID ........................................................... 11
6.1. Chu k sng thnh phn .................................................................... 12
6.2. Activity Stack .................................................................................... 12
6.3. Cc trng thi ca chu k sng .......................................................... 13
6.4. Chu k sng ca ng dng ................................................................ 13
6.5. Cc s kin trong chu k sng ca ng dng ..................................... 14
6.6. Thi gian sng ca ng dng............................................................. 14
6.7. Thi gian hin th ca Activity .......................................................... 14
6.8. Cc phng thc ca chu k sng ..................................................... 14
7. CC THNH PHN GIAO DIN TRONG ANDROID ........................... 16
7.1. View.................................................................................................. 16
7.2. ViewGroup........................................................................................ 16
7.2.1. LinearLayout ............................................................................. 16
7.2.2. FrameLayout ............................................................................. 17
7.2.3. AbsoluteLayout ......................................................................... 17
7.2.4. RetaliveLayout .......................................................................... 18
7.2.5. TableLayout .............................................................................. 18
7.3. Button ............................................................................................... 18
7.4. ImageButton ...................................................................................... 19
7.5. ImageView ........................................................................................ 20
7.6. ListView ........................................................................................... 20
7.7. TextView .......................................................................................... 21
Page | 1
Sinh vin: Trn Hu Phc

sinhvienit.net

Tm hiu v xy dng ng dng Phone Book Safe Manager trn Android

7.8. EditText ............................................................................................ 21


7.9. CheckBox.......................................................................................... 22
7.10.
MenuOptions................................................................................. 22
7.11.
ContextMenu ................................................................................. 24
7.12.
Quick Search Box.......................................................................... 24
7.13.
Activity & Intend........................................................................... 25
7.13.1. Activity...................................................................................... 25
7.13.2. Intent ......................................................................................... 26
8. CONTENT PROVIDER V URI .............................................................. 28
9. BACKGROUND SERVICE ..................................................................... 29
10. TELEPHONY ........................................................................................... 32
11. SQLITE .................................................................................................... 32
12. ANDROID & WEBSERVICE .................................................................. 33
12.1.
Khi nim Web service v SOAP .................................................. 33
12.2.
Gii thiu v XStream ................................................................... 34
12.3.
Thao tc vi web service trong Android ........................................ 36
CHNG 2: PHN TCH V THIT K H THNG ............................... 38
13. C T BI TON................................................................................. 38
14. C T CHC NNG ............................................................................ 38
14.1.
Upload phone book........................................................................ 38
14.2.
Revert phone book......................................................................... 39
14.3.
Send contact .................................................................................. 40
14.4.
Reveive contact ............................................................................. 40
14.5.
Export contact ti SD Card ............................................................ 40
14.6.
Import contact t SD Card ............................................................. 41
14.7.
Account Manager .......................................................................... 41
14.7.1. Login ......................................................................................... 41
14.7.2. Change password ....................................................................... 41
14.7.3. Restore account ......................................................................... 41
14.7.4. Create account ........................................................................... 42
15. CC BIU H THNG ..................................................................... 42
15.1.
Biu Usercase ........................................................................... 42
15.1.1. Restore account ......................................................................... 43
15.1.2. Export Contact vo SD Card ...................................................... 43
15.1.3. Import Contact t SD Card ........................................................ 43
15.1.4. Login ......................................................................................... 44
15.2.
Biu Class................................................................................. 44
Page | 2
Sinh vin: Trn Hu Phc

sinhvienit.net

Tm hiu v xy dng ng dng Phone Book Safe Manager trn Android

15.2.1. Biu Class gia cc thnh phn giao din ............................. 44


15.3.
Biu Class Contact.................................................................... 44
15.4.
Biu Class ContactProvider ...................................................... 44
15.5.
Class Webservice .......................................................................... 45
15.6.
Biu Sequence .......................................................................... 45
15.6.1. Login ......................................................................................... 45
15.6.2. Create new account.................................................................... 46
15.6.3. Restore account ......................................................................... 47
15.6.4. Upload phone book.................................................................... 48
15.6.5. Revert phone book ..................................................................... 48
15.6.6. Export Contact to SD Card ........................................................ 49
15.6.7. Import Contact from SD Card .................................................... 49
15.6.8. Send Contact ............................................................................. 49
15.7.
Biu Activity ............................................................................ 51
15.7.1. Change password ....................................................................... 51
15.7.2. Export Contact to SD Card ........................................................ 52
15.7.3. Upload phone book.................................................................... 52
15.7.4. Send Contact ............................................................................. 53
15.7.5. Revert phone book ..................................................................... 53
15.7.6. Import Contact from SD Card .................................................... 54
16. C S D LIU WEB SERVICE ............................................................ 54
CHNG 3: DEMO NG DNG ................................................................... 57
17. MT VI HNH NH NG DNG ........................................................ 57
18. S LIN KT CC MN HNH V DIALOG ................................. 58
18.1.
Danh sch cc lin kt mn hnh mc 1&2 .................................... 58
18.2.
Danh sch lin kt cc mn hnh mc 3 ......................................... 58
18.2.1. Mn hnh View Contact ............................................................. 58
18.2.2. Mn hnh danh sch cc Contact mi......................................... 58
18.2.3. Mn hnh danh sch phone bool upload................................. 58
19. NH GI NG DNG ......................................................................... 59
20. HNG PHT TRIN TRONG TNG LAI......................................... 59
21. KT LUN............................................................................................... 60
NH GI CA GIO VIN
TI LIU THAM KHO

Page | 3
Sinh vin: Trn Hu Phc

sinhvienit.net

Tm hiu v xy dng ng dng Phone Book Safe Manager trn Android

CHNG 1: C S L THUYT
1. LCH S ANDROID
Ban u, Android l h iu hnh cho cc thit b cm tay da trn li Linux do
cng ty Android Inc. (California, M) thit k. Cng ty ny sau c Google mua
li vo nm 2005 v bt u xy dng Android Platform. Cc thnh vin ch cht ti
Android Inc. gm c: Andy Rubin, Rich Miner, Nick Sears, and Chris White.

Hnh 1-1 Android timeline

V sau tip, vo cui nm 2007, thuc v Lin minh Thit b Cm tay M Ngun
m (Open Handset Alliance) gm cc thnh vin ni bt trong ngnh vin thng v
thit b cm tay nh:
Texas Instruments, Broadcom Corporation, Google, HTC, Intel, LG, Marvell
Technology Group, Motorola, Nvidia, Qualcomm, Samsung Electronics, Sprint
Nextel, T-Mobile, ARM Holdings, Atheros Communications, Asustek Computer Inc,
Garmin Ltd, Softbank, Sony Ericsson, Toshiba Corp, and Vodafone Group,
Mc tiu ca Lin minh ny l nhanh chng i mi p ng tt hn cho nhu
cu ngi tiu dng v kt qu u tin ca n chnh l nn tng Android. Android
c thit k phc v nhu cu ca cc nh sn xut thit, cc nh khai thc v cc
lp trnh vin thit b cm tay.
Phin bn SDK ln u tin pht hnh vo thng 11 nm 2007, hng T-Mobile
cng cng b chic in thoi Android u tin l chic T-Mobile G1, chic
smartphone u tin da trn nn tng Android. Mt vi ngy sau , Google li tip
tc cng b s ra mt phin bn Android SDK release Candidate 1.0. Trong thng 10
nm 2008, Google c cp giy php m ngun m cho Android Platform.
Khi Android c pht hnh th mt trong s cc mc tiu trong kin trc ca n
l cho php cc ng dng c th tng tc c vi nhau v c th s dng li cc
thnh phn t nhng ng dng khc. Vic ti s dng khng ch c p dng cho
Page | 4
Sinh vin: Trn Hu Phc

sinhvienit.net

Tm hiu v xy dng ng dng Phone Book Safe Manager trn Android

cho cc dch v m n cn c p dng cho c cc thnh phn d liu v giao din


ngi dng.
Vo cui nm 2008, Google cho pht hnh mt thit b cm tay c gi l
Android Dev Phone 1 c th chy c cc ng dng Android m khng b rng buc
vo cc nh cung cp mng in thoi di ng. Mc tiu ca thit b ny l cho php
cc nh pht trin thc hin cc cuc th nghim trn mt thit b thc c th chy h
iu hnh Android m khng phi k mt bn hp ng no. Vo khong cng thi
gian th Google cng cho pht hnh mt phin vn v li 1.1 ca h iu hnh ny.
c hai phin bn 1.0 v 1.1 Android cha h tr soft-keyboard m i hi cc thit
b phi s dng bn phm vt l. Android c nh vn ny bng cch pht hnh
SDK 1.5 vo thng T nm 2009, cng vi mt s tnh nng khc. Chng hn nh
nng cao kh nng ghi m truyn thng, vt dng, v cc live folder.
2. DELVING VI MY O DALVIK
Dalvik l my o gip cc ng dng java chy c trn cc thit b ng
Android. N chy cc ng dng c chuyn i thnh mt file thc thi Dalvik
(dex). nh dng ph hp cho cc h thng m thng b hn ch v b nh v tc
x l. Dalvik c thit k v vit bi Dan Bornstein, ngi t tn cho n sau
khi n thm mt ngi lng nh c nh c tn l Dalvk o Eyjafjrur, ni m
mt s t tin ca ng sinh sng.
T gc nhn ca mt nh pht trin, Dalvik trng ging nh my o Java (Java
Virtual Machine) nhng thc t th hon ton khc. Khi nh pht trin vit mt ng
dng dnh cho Android, anh ta thc hin cc on m trong mi trng Java. Sau ,
n s c bin dch sang cc bytecode ca Java, tuy nhin thc thi c ng dng
ny trn Android th nh pht trin phi thc thi mt cng c c tn l dx. y l cng
c dng chuyn i bytecode sang mt dng gi l dex bytecode. "Dex" l t vit
tt ca "Dalvik executable" ng vai tr nh c ch o thc thi cc ng dng Android.
3. KIN TRC CA ANDROID
M hnh sau th hin mt cch tng qut cc thnh phn ca h iu hnh
Android. Mi mt phn s c c t mt cch chi tit di y.

Page | 5
Sinh vin: Trn Hu Phc

sinhvienit.net

Tm hiu v xy dng ng dng Phone Book Safe Manager trn Android

Hnh 1-2 Cu trc stack h thng Android

3.1.

Tng ng dng

Android c tch hp sn mt s ng dng cn thit c bn nh: contacts,


browser, camera, Phone, Tt c cc ng dng chy trn h iu hnh Android u
c vit bng Java.
3.2.

Application framework

Bng cch cung cp mt nn tng pht trin m, Android cung cp cho cc nh


pht trin kh nng xy dng cc ng dng cc k phong ph v sng to. Nh pht
trin c t do tn dng cc thit b phn cng, thng tin a im truy cp, cc dch
v chy nn, thit lp h thng bo ng, thm cc thng bo cc thanh trng thi,
v nhiu, nhiu hn na.
Nh pht trin c th truy cp vo cc API cng mt khun kh c s dng bi
cc ng dng li. Cc kin trc ng dng c thit k n gin ha vic s dng
li cc thnh phn; bt k ng dng c th xut bn kh nng ca mnh v ng dng
no khc sau c th s dng nhng kh nng (c th hn ch bo mt c thc thi
bi khun kh). C ch ny cho php cc thnh phn tng t s c thay th bi
ngi s dng.
C bn tt c cc ng dng l mt b cc dch v v cc h thng, bao gm:
Mt tp hp rt nhiu cc View c kh nng k tha ln nhau dng thit
k phn giao din ng dng nh: gridview, tableview, linearlayout,

Page | 6
Sinh vin: Trn Hu Phc

sinhvienit.net

Tm hiu v xy dng ng dng Phone Book Safe Manager trn Android

Mt Content Provider cho php cc ng dng c th truy xut d liu t


cc ng dng khc (chng hn nh Contacts) hoc l chia s d liu gia
cc ng dng .
Mt Resource Manager cung cp truy xut ti cc ti nguyn khng phi
l m ngun, chng hn nh: localized strings, graphics, and layout files.
Mt Notifycation Manager cho php tt c cc ng dng hin th cc
custom alerts trong status bar.
Activity Maanager c dng qun l chu trnh sng ca ng dng v iu
hng cc activity.
3.3.

Library

Android bao gm mt tp hp cc th vin C/C++ c s dng bi nhiu thnh


phn khc nhau trong h thng Android. iu ny c th hin thng qua nn tng
ng dng Android. Mt s cc th vin c bn c lit k di y:
System C library: a BSD-derived implementation of the standard C system
library (libc), tuned for embedded Linux-based devices.
Media Libraries - based on PacketVideo's OpenCORE; the libraries support
playback and recording of many popular audio and video formats, as well as
static image files, including MPEG4, H.264, MP3, AAC, AMR, JPG, and PNG
Surface Manager Qun l vic truy xut vo h thng hin th
LibWebCore - a modern web browser engine which powers both the Android
browser and an embeddable web view.
SGL - the underlying 2D graphics engine.
3D libraries - an implementation based on OpenGL ES 1.0 APIs; the libraries
use either hardware 3D acceleration (where available) or the included, highly
optimized 3D software rasterizer.
FreeType - bitmap and vector font rendering.
SQLite - a powerful and lightweight relational database engine available to all
applications.
3.4.

Android Runtime

Android bao gm mt tp hp cc th vin c bn m cung cp hu ht cc chc


nng c sn trong cc th vin li ca ngn ng lp trnh Java. Tt c cc ng dng
Android u chy trong tin trnh ring. My o Dalvik c vit cho mt thit
b c th chy nhiu my o hiu qu. Cc VM Dalvik thc thi cc tp tin thc thi
Page | 7
Sinh vin: Trn Hu Phc

sinhvienit.net

Tm hiu v xy dng ng dng Phone Book Safe Manager trn Android

Dalvik (dex). nh dng c ti u ha cho b nh ti thiu. VM l da trn


register-based, v chy cc lp c bin dch bi mt trnh bin dch Java
chuyn i thnh cc nh dng dex. Cc VM Dalvik da vo nhn Linux cho cc
chc nng c bn nh lung v qun l b nh thp.
3.5.

Linux kernel

Android da trn Linux phin bn 2.6 cho h thng dch v ct li nh security,


memory management, process management, network stack, and driver model. Kernel
Linux hot ng nh mt lp tru tng ha gia phn cng v phn cn li ca phn
mm stack.
4. ANDROID EMULATOR
Android SDK v Plugin Eclipse c gi l mt Android Deverloper Tool (ADT).
Cc Android coder s cn phi s dng cng c IDE (Integrated Development
Enveronment) ny pht trin, debugging v testing cho ng dng. Tuy nhin, cc
coder cng c th khng cn phi s dng IDE m thay vo l s dng command
line bin dch v tt nhin l vn c Emulator nh thng.
Android Emulator c trang b y hu ht cc tnh nng ca mt thit b tht.
Tuy nhin, mt s b gii hn nh l kt ni qua cng USB, camera v video, nghe
phone, ngun in gi lp v bluetooth.
Android Emulator thc hin cc cng vic thng qua mt b x l m ngun m,
cng ngh ny c gi l QEMU (http://bellard.org/qemu/) c pht trin bi
Fabrice Bellard.

Hnh 1-3 Android emulator

Page | 8
Sinh vin: Trn Hu Phc

sinhvienit.net

Tm hiu v xy dng ng dng Phone Book Safe Manager trn Android

5. CC THNH PHN TRONG MT ANDROID PROJECT


5.1.

AndroidManifest.xml

Trong bt k mt project Android no khi to ra u c mt file


AndroidManifest.xml, file ny c dng nh ngha cc screen s dng, cc
permission cng nh cc theme cho ng dng. ng thi n cng cha thng tin v
phin bn SDK cng nh main activity s chy u tin.
File ny c t ng sinh ra khi to mt Android project. Trong file manifest bao
gi cng c 3 thnh phn chnh l: application, permission v version.
Di y l ni dung ca mt file AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dtu.k12tpm.pbs.activity"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".LoginActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainContactListActivity" />
<activity android:name=".RestoreContactActivity" />
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
</manifest>

Application
Th <application>, bn trong th ny cha cc thuc tnh c nh ngha cho
ng dng Android nh:
Page | 9
Sinh vin: Trn Hu Phc

sinhvienit.net

Tm hiu v xy dng ng dng Phone Book Safe Manager trn Android

android:icon = drawable resource y t ng dn n file icon


ca ng dng khi ci t. VD: android:icon = @drawable/icon.
android:name = string thuc tnh ny t tn cho ng dng
Android. Tn ny s c hin th ln mn hnh sau khi ci t ng dng.
android:theme = drawable theme thuc tnh ny t theme cho
ng dng. Cc theme l cc cch hin th giao din ng dng.
Ngoi ra cn nhiu thuc tnh khc
Permission
Bao gm cc thuc tnh ch nh quyn truy xut v s dng ti nguyn ca
ng dng. Khi cn s dng mt loi ti nguyn no th trong file manifest ca
ng dng cn phi khai bo cc quyn truy xut nh sau:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCOUNT_MANAGER"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.CALL_PHONE"/>

SDK version
Th xc nh phin bn SDK c khai bo nh sau:
<uses-sdk android:minSdkVersion="7" />.
y ch ra phin bn SDK nh nht m ng dng hin ang s dng.
5.2.

File R.java

File R.java l mt file t ng sinh ra ngay khi to ng dng, file ny c s


dng qun l cc thuc tnh c khai bo trong file XML ca ng dng v cc ti
nguyn hnh nh.
M ngun ca file R.java c t ng sinh khi c bt k mt s kin no xy xa
lm thay i cc thuc tnh trong ng dng. Chng hn nh, bn ko v th mt file
hnh nh t bn ngoi vo project th ngay lp tc thuc tnh ng dn n file
cng s c hnh thnh trong file R.java hoc xo mt file hnh nh th ng dn
tng ng n hnh nh cng t ng b xo.
C th ni file R.java hon ton khng cn phi ng chm g n trong c qu
trnh xy dng ng dng.
Di y l ni dung ca mt file R.java:
/*
*
*
*
*

AUTO-GENERATED FILE.

DO NOT MODIFY.

This class was automatically generated by the


aapt tool from the resource data it found. It
should not be modified by hand.

Page | 10
Sinh vin: Trn Hu Phc

sinhvienit.net

Tm hiu v xy dng ng dng Phone Book Safe Manager trn Android


*/
package dtu.k12tpm.pbs.activity;
public final class R {
public static final class array {
public static final int array_timeout=0x7f050000;
}
public static final class attr {
}
public static final class drawable {
public static final int add=0x7f020000;
public static final int backup_icon=0x7f020001;
public static final int checkall=0x7f020002;
}
public static final class id {
public static final int Button01=0x7f070006;
public static final int Button02=0x7f070007;
public static final int CheckBox01=0x7f070017;
}
public static final class layout {
public static final int contact_list=0x7f030000;
public static final int content_sender=0x7f030001;
public static final int friend_list=0x7f030002;
}
public static final class menu {
public static final int context_menu=0x7f060000;
public static final int menu_options=0x7f060001;
public static final int options_menu=0x7f060002;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int context_menu_item_delete=0x7f04000b;
public static final int context_menu_item_edit=0x7f04000a;
}
}

6. CHU K NG DNG ANDROID


Mt tin trnh Linux gi gn mt ng dng Android c to ra cho ng dng
khi codes cn c run v s cn chy cho n khi:
N khng ph thuc.
H thng cn ly li b nh m n chim gi cho cc ng dng khc
Mt s khc thng v c tnh c bn ca Android l thi gian sng ca tin trnh
ng dng khng c iu khin trc tip bi chnh n. Thay vo , n c xc
nh bi h thng qua mt kt hp ca:
Nhng phn ca ng dng m h thng bit ang chy
Nhng phn quan trng nh th no i vi ngi dng
Bao nhiu vng nh chim lnh trong h thng.
Page | 11
Sinh vin: Trn Hu Phc

sinhvienit.net

Tm hiu v xy dng ng dng Phone Book Safe Manager trn Android

6.1.

Chu k sng thnh phn

Cc thnh phn ng dng c mt chu k sng, tc l mi thnh phn t lc bt u


khi to v n thi im kt thc. Gia , i lc chng c th l active hoc
inactive, hoc l trong trng hp activies n c th visible hoc invisible

Hnh 1-4

6.2.

Activity Stack

Bn trong h thng cc activity c qun l nh mt activity stack. Khi mt


Activity mi c start, n c t nh ca stack v tr thnh activity ang chy
activity trc s bn di activity mi v s khng thy trong sut qu trnh activity
mi tn ti.
Nu ngi dng nhn nt Back th activity kt tip ca stack s di duyn ln v tr
thnh active.

Hnh 1-5 Activity stack

Page | 12
Sinh vin: Trn Hu Phc

sinhvienit.net

Tm hiu v xy dng ng dng Phone Book Safe Manager trn Android

6.3.

Cc trng thi ca chu k sng

Hnh 1-6 Chu k sng ca Activity

Mt Activity ch yu c 3 chu k chnh sau:


Active hoc running: Khi Activity l c chy trn mn hnh. Activity ny tp
trung vo nhng thao tc ca ngi dng trn ng dng.
Paused: Activity l c tm dng (paused) khi mt focus nhng ngi dng
vn trng thy. C ngha l mt Activity mi trn n nhng khng bao ph
y mn hnh. Mt Activity tm dng l cn sng nhng c th b kt thc bi
h thng trong trng hp thiu vng nh.
Stopped: Nu n hon ton bao ph bi Activity khc. N vn cn trng thi v
thng tin thnh vin trong n. Ngi dng khng thy n v thng b loi b trong
trng hp h thng cn vng nh cho tc v khc.
6.4.

Chu k sng ca ng dng

Trong mt ng dng Android c cha nhiu thnh phn v mi thnh phn u c


mt chu trnh sng ring. V ng dng ch c gi l kt thc khi tt c cc thnh
phn trong ng dng kt thc. Activity l mt thnh phn cho php ngi dng giao
tip vi ng dng. Tuy nhin, khi tt c cc Activity kt thc v ngi dng khng
cn giao tip c vi ng dng na nhng khng c ngha l ng dng kt thc.
Bi v ngoi Activity l thnh phn c kh nng tng tc ngi dng th cn c cc
thnh phn khng c kh nng tng tc vi ngi dng nh l Service, Broadcast
Page | 13
Sinh vin: Trn Hu Phc

sinhvienit.net

Tm hiu v xy dng ng dng Phone Book Safe Manager trn Android

receiver. C ngha l nhng thnh phn khng tng tc ngi dng c th chy
background di s gim st ca h iu hnh cho n khi ngi dng t tt chng.
6.5.

Cc s kin trong chu k sng ca ng dng

Nu mt Activity c tm dng hoc dng hn, h thng c th b thng tin khc


ca n t vng nh bi vic finish() (gi hm finish() ca n), hoc n gin git tin
trnh ca n. Khi n c hin th ln na vi ngi dng, n phi c hon ton
restart v phc hi li trng thi trc. Khi mt Activity chuyn qua chuyn li gia
cc trng thi, n phi bo vic chuyn ca n bng vic gi hm transition.

Hnh 1-7

Tt c cc phng thc l nhng mc ni m bn c th override lm tng


thich cng vic trong ng dng khi thay i trng thi. Tt c cc Activity bt buc
phi c onCreate() khi to ng dng. Nhiu Activity s cng hin thc onPause()
xc nhn vic thay i d liu v mt khc chun b dng hot ng vi ngi
dng.
6.6.

Thi gian sng ca ng dng

By phng thc chuyn tip nh ngha trong chu k sng ca mt Activity. Thi
gian sng ca mt Activity din ra gia ln u tin gi onCreate() n trng thi cui
cng gi onDestroy(). Mt Activity khi to ton b trng thi ton cc trong
onCreate(), v gii phng cc ti nguyn ang tn ti trong onDestroy().
6.7.

Thi gian hin th ca Activity

Visible lifetime ca mt activity din ra gia ln gi mt onStart() cho n khi gi


onStop(). Trong sut khong thi gian ny ngi dng c th thy activity trn mn
hnh, c ngha l n khng b foreground hoc ang tng tc vi ngi dng. Gia 2
phng thc ngi dng c th duy tr ti nguyn hin th activity n ngi dng.
6.8.

Cc phng thc ca chu k sng

Phng thc: onCreate()


c gi khi activity ln u tin c to
y bn lm tt c cc ci t tnh -- to cc view, kt ni d liu n list
v .v.v
Phng thc ny gi qua mt i tngBundle cha ng t trng thi
trc ca Activity
Page | 14
Sinh vin: Trn Hu Phc

sinhvienit.net

Tm hiu v xy dng ng dng Phone Book Safe Manager trn Android

Lun theo sau bi onStart()


Phng thc: onRestart()
c gi sau khi activity c dng, ch mt khong ang khi ng
ln na (stared again)
Lun theo sau bi onStart()
Phng thc: onStart()
c gi trc khi mt activity visible vi ngi dng.
Theo sau bi onResume() nu activity n trng thi foreground hoc
onStop() n n tr nn n.
Phng thc: onResume()
c gi trc khi activity bt u tng tc vi ngi dng
Ti thi im ny activity trn dnh ca stack activity.
Lun theo sau bi onPause()
Phng thc: onPause()
c gi khi h thng ang resuming activity khc.
Phng thc ny l in hnh vic gi li khng i d liu.
N nn c din ra mt cch nhanh chng bi v activity k tip s khng
c resumed ngay cho n khi n tr li.
Theo sau bi onResume nu activity tr v t trc, hoc bi onStop nu
n tr nn visible vi ngi dng.
Trng thi ca activity c th b git bi h thng.
Phng thc: onStop()
c gi khi activity khng thuc tm nhn ca ngi dng.
N c th din ra bi v n ang b hy, hoc bi v activity khc va c
resumed v bao ph n.
c theo sau bi onRestart() nu activity ang li tng tc vi
ngi dng, hoc onDestroy() nu activity ang b.
Trng thi ca activity c th b git bi h thng.
Phng thc: onDestroy()
c gi trc khi activity b hy.
l ln gi cui cng m activity ny c nhn.
N c gi khc bi v activity ang hon thnh, hoc bi v h thng tm
thi b hy dit tit kim vng nh.
Bn c th phn bit gia 2 kch bn vi phng isFinshing().
Trng thi ca activity c th c git bi h thng.
Page | 15
Sinh vin: Trn Hu Phc

sinhvienit.net

Tm hiu v xy dng ng dng Phone Book Safe Manager trn Android

7. CC THNH PHN GIAO DIN TRONG ANDROID


7.1.

View

Trong mt ng dng Android, giao din ngi dng c xy dng t cc i


tng View v ViewGroup. C nhiu kiu View v ViewGroup. Mi mt kiu l mt
hu du ca class View v tt c cc kiu c gi l cc Widget.
Tt c mi widget u c chung cc thuc tnh c bn nh l cch trnh by v tr,
background, kch thc, l, Tt c nhng thuc tnh chung ny c th hin ht
trong i tng View.
Trong Android Platform, cc screen lun c b tr theo mt kiu cu trc phn
cp nh hnh di. Mt screen l mt tp hp cc Layout v cc widget c b tr c
th t. th hin mt screen th trong hm onCreate ca mi Activity cn phi c
gi mt hm l setContentView(R.layout.main); hm ny s load giao din t file

XML ln phn tch thnh m bytecode.


Hnh 1-8 Cu trc mt giao din ng dng Android

7.2.

ViewGroup

ViewGroup thc ra chnh l View hay ni ng hn th ViewGroup chnh l cc


widget Layout c dng b tr cc i tng khc trong mt screen. C mt s
loi ViewGroup nh sau:
7.2.1. LinearLayout
LinearLayout c dng b tr cc thnh phn giao din theo chiu ngang hoc
chiu dc nhng trn mt line duy nht m khng c xung dng.
LinearLayout lm cho cc thnh phn trong n khng b ph thuc vo kch thc
ca mn hnh. Cc thnh phn trong LinearLayout c dn theo nhng t l cn
xng da vo cc rng buc gia cc thnh phn.

Page | 16
Sinh vin: Trn Hu Phc

sinhvienit.net

Tm hiu v xy dng ng dng Phone Book Safe Manager trn Android

Hnh 1-9 B tr cc widget s dng LinearLayout

7.2.2. FrameLayout
FrameLayout c dng b tr cc i tng theo kiu ging nh l cc Layer
trong Photoshop. Nhng i tng no thuc Layer bn di th s b che khut bi
cc i tng thuc Layer nm trn. FrameLayer thng c s dng khi mun to
ra cc i tng c khung hnh bn ngoi chng hn nh contact image button.

ImageV
LearnAndroid.co
m

TextVi

Hnh 1-10 B tr cc widget trong FrameLayout

7.2.3. AbsoluteLayout
Layout ny c s dng b tr cc widget vo mt v tr bt k trong layout
da vo 2 thuc tnh to x, y. Tuy nhin, kiu layout ny rt t khi c dng bi v
to ca cc i tng lun c nh v s khng t iu chnh c t l khong
cch gia cc i tng. Khi chuyn ng dng sang mt mn hnh c kch thc vi
mn hnh thit k ban u th v tr ca cc i tng s khng cn c chnh xc
nh ban u.

Page | 17
Sinh vin: Trn Hu Phc

sinhvienit.net

Tm hiu v xy dng ng dng Phone Book Safe Manager trn Android

7.2.4. RetaliveLayout
Layout ny cho php b tr cc widget theo mt trc i xng ngang hoc dc.
t c ng v tr th cc widget cn c xc nh mt mi rng buc no vi
cc widget khc. Cc rng buc ny l cc rng buc tri, phi, trn, di so vi mt
widget hoc so vi layout parent. Da vo nhng mi rng buc m
RetaliveLayout cng khng ph thuc vo kch thc ca screen thit b. Ngoi ra, n
cn c u im l gip tit kim layout s dng nhm mc ch gim lng ti
nguyn s dng khi load ng thi y nhanh qu trnh x l.

Hnh 1-11 B tr widget trong RetaliveLayout

7.2.5. TableLayout
Layout ny c s dng khi cn thit k mt table cha d liu hoc cn b tr
cc widget theo cc row v column. Chng hn nh, giao din ca mt chic my tnh
n gin hoc mt danh sch d liu.

Hnh 1-12 B tr widget trong TableLayout

7.3.

Button

S d widget button c gii thiu u tin trong s cc widget khc l v y l


i tng c th ni l c dng nhiu nht trong hu ht cc ng dng Android.
Page | 18
Sinh vin: Trn Hu Phc

sinhvienit.net

Tm hiu v xy dng ng dng Phone Book Safe Manager trn Android

thit k giao din vi mt button ta c 2 cch nh sau:


Thit k bng XML
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cmdButton1"
android:text="Touch me!"
android:onClick="touchMe"/>

Thuc tnh android:onClick="touchMe" c dng nm bt s kin


click vo button. Khi s kin click button xy ra th phng thc touchMe
c khai bo trong th thuc tnh s c gi. Nu trng hp phng thc
touchMe cha c khai bo trong file m ngun tng ng th s pht sinh
mt exception. Ngc li, phng thc touchMe s nhn c mt i tham
bin l i tng View ni pht sinh ra s kin. i tng View ny c th
p kiu trc tip sang kiu Button v thc cht n l mt button.
VD: trong file m ngun khai bo mt hm nh sau:
public void touchMe(View v){
Button me = (Button) v;
Me.setText(Touched);
}

Thit k bng code


Thc ra m ni th nu khng phi i hi phi custom li mt widget th
khng cn phi s dng ti code. Trong mt s trng hp bt buc chng ta
phi custom cc widget cho ph hp vi hon cnh. Chng hn nh trong
game, cc menu hay cc nt iu khin,
khai bo mt Button trong code ta lm nh sau:
Button cmdButton = new Button(this);
cmdButton.setText(Touch Me!);
cmdButon.setOnClickListener();

custom mt widget no ta phi to mt class k tha t class Widget


mun custom, sau s dng hm draw v li widget nh mt Canvas.
VD: canvas.drawPicture(Picture.createFromStream(...));
7.4.

ImageButton

Cng tng t nh Button, ImageButton ch c thm mt thuc tnh


Page | 19
Sinh vin: Trn Hu Phc

sinhvienit.net