Professional Documents
Culture Documents
BO CO N 2
ti:
H NI 11/2012
MC LC
I-
M U .......................................................................................................................................... 4
2.
III1.
2.
2.1.
2.2.
2.3.
2.4.
2.5.
2.6.
2.7.
2.8.
2.8.1.
2.8.2.
2.8.3.
2.8.4.
3.
3.1.
3.2.
3.2.1.
View: ....................................................................................................................................... 30
3.2.2.
ViewGroup : ............................................................................................................................ 30
3.2.3.
3.2.3.1.
Button:................................................................................................................................. 30
3.2.3.2.
ImageButton:....................................................................................................................... 31
3.2.3.3.
ImageView: ......................................................................................................................... 31
3.2.3.4.
TextView: ............................................................................................................................ 31
3.2.3.5.
EditText: .............................................................................................................................. 32
3.2.3.6.
CheckBox: ........................................................................................................................... 32
3.2.3.7.
3.2.3.8.
ListView: ............................................................................................................................. 34
3.2.3.9.
AutoCompleteTextView:...................................................................................................... 35
3.2.3.10.
3.2.3.11.
ProgressBar: ....................................................................................................................... 37
3.3.
3.4.
3.5.
3.6.
B nh chia s ............................................................................................................................. 45
3.6.1.
3.6.2.
3.6.3.
External Storeage.................................................................................................................... 48
IV-
V-
KT LUN................................................................................................................................. 55
VI-
I-
M U
Hin nay, nhu cu s dng cc thit b di ng ngy cng ln, ko theo s pht trin ca cc
nn tng di ng. Trn th trng c rt nhiu h iu hnh di ng nh iOS, Window
Phone, BlackBerry, Symbian, LiMo, ... Trong , ni bt phi k n l h iu hnh
Android. Vi c tnh l nn tng m ngun m v hon ton min ph, Android to nhiu
thun li cho cc nh pht trin thit k ng dng di ng ca mnh, gip nn tng ny ngy
cng pht trin hn na..
II-
H IU HNH ANDROID.
c h tr mnh m t Google..
Notification manager: qun l thng bo n ngi dng (v d nh: c tin nhn, lch,
cnh bo...)
III-
- Android SDK
2.2.Ci t plugin ADT cho Eclipse
Cch 1:
1. M Eclipse, vo help > Install New Software > chn Add v in https://dlssl.google.com/android/eclipse/ vo Location
10
11
DDMS cho php theo di hot ng trong Emulator, h tr sa li, xem, dng cc tin trnh, truy
cp h thng file, gi lp gi tin nhn, gi in, GPS,
12
14
Cng c ng gi aapt (Android Asset Packaging Tool) thu thp cc file ti nguyn
(nh l cc file .xml), bin dch chng v ng thi to ra file R.java (dng qun l
cc thuc tnh c khai bo trong file xml).
15
Cng c aidl (Android Interface Definition Language) chuyn i .aidl interfaces (nu
c) vo trong Java interfaces.
R.java, m ngun, cng vi file aidl c bin dch bi Java complier cho ra .class file
Dex tools s chuyn cc file class (k c ca bn th 3) thnh Dalvik bytecode
Cc ti nguyn khng c bin dch (v d: hnh nh), cc ti nguyn c bin dch,
cng vi .dex file c ng gi thnh file apk s dng apkbuilder
File apk c ng k dng Debug hoc Release, nu ng dng c ng k dng
Release cn qua b zipalign gip gim b nh s dng khi chy trn thit b.
2.8.2. Lm vic vi Disk images trong Emulator
Phn vng /cache c khi to rng s dng bi trnh duyt lu trang web v nh. Emulator
cung cp ty chn cache <file> lu tr /cache khi tt my.
2.8.3. Emulator Networking
++Khng gian a ch:
Mi thc th Emulator chy sau mt router/firewall o, c lp vi giao din mng ca my pht
trin v vi internet; n khng th nhn thy my ca nh pht trin v cc Emulator khc. N
ch nhn thy cc kt ni thng qua router/firewall.
16
Router o ca mi thc th qun l khng gian a ch 10.0.2/24. a ch trong khng gian nay
c t trc bi Emulator/router nh sau:
a ch
M t
10.0.2.1
Router/gateway address
10.0.2.2
10.0.2.3
10.0.2.4/ 10.0.2.5/10.0.2.6
10.0.2.15
127.0.0.1
17
Nu khi ng t dng lnh, ta c th thm ty chn dns-server <danh sch a ch ngn cch
bi du phy> thit lp DNS cho Emulator.
++Kt ni gia cc Emulator:
lm c iu ny, chng ta s s dng network redirection
Gi s hai Emulator A v B cng chy trn my nh pht trin, gi s A l server, B kt ni n
A, ta s lm nh sau:
1. A s nghe ti 10.0.2.15:<server_port>
2. redir add <protocol> : <local_port> : <server_port>
3. B s kt ni n 10.0.2.2:<local_port>
++Gi in, gi tin SMS n Emulator khc:
Emulator t ng chuyn tip cuc gi hay SMS bng s hiu cng. gi in hay gi SMS
n Emulator khc, ta bt ng dng gi in hoc nhn tin ln, thay s my tht bng s hiu
cng ca Emulator cn kt ni n.
2.8.4. S dng Emulator Console
Mi thc th Emulator ang hot ng cung cp mt Console cho php truy vn, iu khin mi
trng thit b m phng. V d: dng Console qun l port redirection ( ni n phn
trc), cc s kin, khi ng dng chy trn Emulator. truy cp Console ta dng telnet
kt ni ti cng ca Emulator
C php: telnet localhost <console-port>
Minh ha:
18
Mt thc th Emulator c mt cp cng k nhau: console port v adb port, adb port s ln hn.
V d console port l 5554 th adb port l 5555
19
M t
list
Xa mt port redirection
Chi tit:
Lnh con
M t
M t
display
ac <on|off>
status <unknown|charging|discharging|notcharging|full>
present <true|false>
health
<unknown|good|overheat|dead|overvoltage|failure>
capacity <percent>
Gi tr
M t (n v mili giy)
gprs
edge
umts
none
(min 0, max 0)
<num>
S chnh xc
<min> : <max>
Khong t t
M t (n v kilobits/giy)
gsm
hscsd
gprs
edge
umts
hsdpa
full
<num>
Up = down = number
<up>:<down>
S chnh xc
M t
call <phonenumber>
Cuc gi n t phonenumber
22
accept <phonenumber>
busy <phonenumber>
cancel <phonenumber>
data <state>
hold
list
voice <state>
status
24
25
Cc th mc:
Src: cha ton b m ngun chng trnh
Gen: cha file R.java khi bin dch
Assets: cha cc file m thanh streaming
Res: cha cc ti nguyn ca chng trnh
Drawable: cc file nh
Layout: cha file xml, quyt nh giao din chng
trnh
Menu: cha file xml thit k cho menu
Values: cha cc ti nguyn khc nh xu k t,
theme style
File Manifest: nh ngha cu trc, cc thnh phn
yu cu ca ng dng
26
V d file manifest:
27
File HelloWorld.java
28
29
30
{
// lam viec gi do
}
}
});
3.2.3.2.ImageButton:
Ging nh Button, ta ch thay tn th l <ImageButton> v khai thm trong file XML ngun nh
v khng c phn text, nh c cha trong th mc res/drawable:
android:src="@drawable/icon"
3.2.3.3.ImageView:
Dng hin th nh, v d khai bo trong file XML:
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/image"/>
31
android:text="@string/hello_world"/>
3.2.3.6.CheckBox:
Hin th chn, v d:
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="unChecked"/>
CheckBox cb = (CheckBox)findViewById(R.id.check);
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
32
Cc nt chn, v d:
<RadioGroup
android:id="@+id/group"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<RadioButton
android:id="@+id/radio1"
android:text="radio1"
/>
<RadioButton
android:id="@+id/radio2"
android:text="radio2"
/>
</RadioGroup>
case R.id.radio2:
// lm g
break;
33
3.2.3.8.ListView:
Hin th danh sch, v d:
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
list.setAdapter(adapter);
adapter.add(data[0]);
adapter.add(data[1]);
adapter.add(data[2]);
adapter.add(data[3]);
u tin ta ly gn ListView vo bin list sau to i tng thuc kiu List<kiu d liu ca
List> v d l model, khi to mt i tng ArrayAdapter (v d l adapter) lm cu ni
chuyn d liu vo list, dng cu lnh list.setAdapter(adapter) ni. thm d liu vo list ta
dng cu lnh adapter.add(d liu).
Kt qu minh ha:
34
}
});
3.2.3.9.AutoCompleteTextView:
V d trong file XML:
<AutoCompleteTextView
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:completionThreshold="3" />
Trong m ngun:
String[] data = {"Dai hoc BKHN", "Dai hoc KTQD"};
35
AutoCompleteTextView a = (AutoCompleteTextView)findViewById(R.id.edit);
a.setAdapter(new ArrayAdapter<String>
(this, android.R.layout.simple_dropdown_item_1line, data));
Kt qu minh ha:
3.2.3.10.
Menu Options:
to menu option ta khai bo file XML trong th mc res > menu. V d my_menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/abc"
android:title="My Menu"
/>
</menu>
Trong m ngun:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu, menu);
return true;
}
36
Kt qu minh ha:
3.2.3.11.
ProgressBar:
Minh ha:
38
3.3.Thao tc vi lung
Trong Android c lung chnh cp nht giao din l UI Thread v cc lung ph Background
to mt lung Background ta lm nh sau:
Khai bo lp lung vi Runable:
private Runnable MyTask = new Runnable() {
}
};
To i tng lung:
Thread my_thread = new Thread(MyTask);
39
V d:
runOnUiThread(new Runnable() {
}
});
40
Trong kiu d liu trng vi kiu ca d liu trong publishProgress(), phng thc c th
dng values cp nht UI Thread.
Ngoi ra cn c hai phng thc l: onPreExecute() thc hin vi d liu trn UI Thread, c
gi trc khi thc hin doInBackground() v phng thc onPostExecute() thc hin vi d
liu trn UI Thread sau khi thc hin doInBackground().
V d:
To lp MyAsyncTask k tha t AsyncTask < ? ,? , ? > ? tng ng cc kiu d liu trong cc
phng thc
class MyAsyncTask extends AsyncTask<Void, Integer, Void> {
int progress = 0;
@Override
protected Void doInBackground(Void... params) {
// lm g
publishProgress(Kiu Integer);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// lm g
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(Integer... values) {
// lm g vi values
super.onProgressUpdate(values);
}
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CU LNH TO BNG D LIU);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// TODO Auto-generated method stub
db = helper.getWritableDatabase();
M CSDL c: db = helper.getReadableDatabase();
ng c s d liu dng: helper.close();
++ chn d liu vo bng ta lm nh sau:
To mt i tng ContentValues v d: ContentValues cv = new ContentValues();
Sau dng lnh put a d liu vo cv, v d: cv.put(tn ct, d liu);
42
nullColumnHack c th l null, do SQL khng cho php chn mt dng trng hon ton nn nu
l null m cv cng null th khng c, cn cung cp mt tn ct c php null l
nullColumnHack (c thay bi tn ct no trong bng).
++ xa d liu trong bng ta dng lnh:
public int delete (String table, String whereClause, String[] whereArgs)
table l tn bng
whereClause: mnh WHERE
whereArgs: cc tham s cho whereClause
++ xa database ta dng lnh:
public static boolean deleteDatabase (File file)
3.5.Activity v Intent
Trong mt ng dng Android s bao gm nhiu Activity, mi Activity s dng cu lnh
setContentView() hin th ra mt giao din. t mt Activity ny gi n Activity khc ta
s dng Intent. Intent bao gm cc thng s chnh l Action (hnh ng m Activity s thc
hin) v Data (d liu cho vic thc thi Action).
khi ng Activity mi ta dng lnh:
Intent i = new Intent (action, data);
startActivity(i);
v d mt s action:
ACTION_CALL (gi in) s dng data l tel:s in thoi
ACTION_VIEW (hin th ni dung, c th l hnh nh, m thanh, web, bn ..) vi data l
ng dn n ni dung cn hin th.
Chng trnh c th yu cu cn phi khai bo quyn trong file Android Manifest
V d data xem ton b danh b: content://contacts/people/
43
Minh ha xem bn
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:21.007599,105.842929"));
startActivity(i);
Ngoi cch cung cp Data nh trn c th cn c cch truyn d liu gia cc Activity:
44
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ducanh);
text = (TextView)findViewById(R.id.text);
46
File demo.xml c lu trong th mc Data > data > ng dng > shared_prefs
Ni dung file demo.xml
48
49
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/welcome"/>
<Button
android:id="@+id/home_add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Thm t mi"
android:textColor="#0000FF"
android:onClick="clickAdd"/>
<Button
android:id="@+id/home_search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Tm kim"
android:textColor="#0000FF"
android:onClick="clickSearch"/>
<Button
android:id="@+id/home_test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Bi kim tra"
android:textColor="#0000FF"
android:onClick="clickTest"/>
<Button
android:id="@+id/home_help"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Tr gip"
android:textColor="#0000FF"
android:onClick="clickHelp"/>
<Button
android:id="@+id/home_quit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Thot"
android:textColor="#0000FF"
android:onClick="clickQuit"/>
</LinearLayout>
android.os.Bundle;
android.app.Activity;
android.content.Intent;
android.view.View;
50
51
52
Phng thc xa t:
public void delete(String word) {
getWritableDatabase().delete(TABLE1, COL1_TABLE1+ " =?",
new String[] { word });
updateTotal(false);
}
53
Vi chc nng to mt bi kim tra c gii hn thi gian, cp nht thi gian trn mn hnh ta
s to lung mi m thi gian. Trong bi bo co ny minh ha s dng lp AsyncTask
to lung Background v thao tc vi giao din chnh nh sau:
class MyAsyncTask extends AsyncTask<Void, Integer, Void>
{
int time_update = 0;
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
while(time_update > 0)
{
SystemClock.sleep(1000);
time_update--;
publishProgress(time_update);
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
time_view.setText("Time "+ second2minute(values[0]));
}
54
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
time_update = TIME_PER_TASK*numberTask;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
flag = false;
task.setText("Ht gi ! n kt thc xem kt qu !");
answer.setEnabled(false);
btn.setText("Kt thc");
}
}
Hm onPreExecute() thc hin cc cng vic trc khi chy lung background v c th thao tc
vi d liu trn lung chnh, trong trng hp ny l gn gi tr cho bin time_update.
Hm doInBackground() lm cc cng vic ca lung Background v gi d liu n giao din
bng phng thc publishProgress(), trong v d ny l gi bin m n giao din.
Hm onProgressUpdate() nhn d liu t lung background v cp nht ln giao din, trong
trng hp v d l cp nht thi gian ln giao din.
Hm onPostExecute() thc hin cc cng vic sau khi lung background thc hin xong, trong v
d th n a ra thng bo ht gi v kha cc giao din khng cho ngi dng nhp cu tr li.
55
V-
KT LUN
56