1. Phương thӭc ghi đè lӟp cha. Source ĺ Overwrite / Implement Method. 2. Ctr + Space không hoҥt đӝng: Window ĺ Preferences ĺ Java ĺ Editor ĺ Content Assist ĺ Advanced ĺVà Check chӑn Other Java Proposals. 3. ChӃ đӝ Auto Activation. Window ĺ Preferences ĺ Java ĺ Editor ĺ Content Assist. NӃu bӓ chӑn thì khi code không hӛ trӧ mà phҧi dùng Ctr + Space. 4. File Explorer Quҧn lý file trong sd card va file system data nhưng nó không truy nhұp đưӧc system data khi thiӃt bӏ thұt. Nó có thӇ Import / Export hay Delete các file trong sd card «. trong emulator. Mӣ File Explorer: Window ĺ Show View ĺ Others ĺ Android ĺ File Eplorer. 5. HiӇn thӏ tiӃng viӋt. Window ĺ Preferences ĺ General ĺ Workspace ĺ Text File En coding ĺ Other ĺ UTF-8. Understanding Android Application 1. Activity. Có thӇ hiӇu là nӅn cӫa ӭng dөng. Khi khӣi đӝng mӝt ӭng dөng Android nào cũng có 1 hàm main Activity đưӧc gӑi, hiӇn thӏ màn hình giao diӋn cӫa ӭng dөng và cho phép ngưӡi dùng tương tác. 2. Service. Là thành phҫn chҥy ҭn bên trong và không hiӋn thӏ cho ngưӡi dùng thҩy, dùng đӇ update dӳ liӋu và đưa ra các cҧnh báo (notification). 3. Content Provider. Quҧn lý và chia sҿ dӳ liӋu giӳa các ӭng dөng. 4. Intent. NӅn tҧn truyӅn tҧi các thông báo. Intent đưӧc sӱ dөng đӇ gӣi các thông báo đi nhҵm đӇ khӣi tҥo 1 Activity hay 1 Service đӇ thӵc hiӋn cong viӋc đưӧc yêu cҫu. Vd: Khi mӣ 1 trang web thì gӣi 1 Intent đi đӇ khӣi tҥo 1 Activity mӟi đӇ hiӇn thӏ trang web đó. 5. Broadcast Receiver. Là thành phҫn nhұn các Intent bên ngoài gӣi tӟi. 6. Notification. Đưa ra các cҧnh báo ma 2 không làm cho các Activity ngӯng hoҥt đӝng, Activity, Service, Content Provider, Intent và Broadsast Receiver là nhӳng thành phҫn chính tҥo nên ӭng dөng Android, bҳt buӝc phҧi khai báo trong AndroidManifest. Understanding Android Application Life Cycle Android có cơ chӃ quҧn lý các process theo chӃ đӝ ưu tiên. Các process có đӝ ưu tiên (priority) thҩp sӁ bӏ Android giҧi phóng mà không hӅ cҧnh báo nhҵm đҧm bҧo tài nguyên. 1. Foreground Process. Là process cӫa ӭng dөng hiӋn thӡi đang dưӧc ngưӡi dùng tuong tác. 2. Visible Process. Là process cӫa ӭng dөng mà Activity đang hiӇn thӏ đӕi vӟi ngưӡi dùng (onPause() cӫa Activity đưӧc gӑi). 3. Sevice Process. Là service đang running. 4. Background Process. Là process cӫa ӭng dөng mà các Activity không hiӇn thӏ đӕi vӟi ngưӡi dùng (onStop() cӫa Activity đưӧc gӑi). 5. Empty Process. Là proces không có bҩt cӭ 1 thành phҫn nào cӫa Active. Theo chӃ đӝ ưu tiên, khi cҫn tài nguyên Android sӁ tӵ đӝng gӑi process, trưӟc tiên là các Empty Process. Android Activity Life Cycle Activity đóng vai trò chính trong viӋc xây dӵng ӭng dөng Android. HӋ điӅu hành Android quҧn lý Activity theo dҥng Stack. Khi 1 Activity đưӧc khӣi tҥo, nó sӁ đưӧc xӃp lên đҫu cӫa Satck và trӣ thành running activity, các Activity trưӟc đó sӁ tҥm dӯng và chӍ hoҥt dӝng trӣ lҥi khi Activity mӟi đưӧc giҧi phóng. Activity bao gӗm 4 state. 1. Active (running). Là Activity đang hiӇn thӏ trên màn hình (foreground). 2. Paused. Activity vүn hiӇn thӏ (visible) nhưng không thӇ tương tác (lost focus). 3. Stoped. Activity bi thay thӃ hoàn toàn bӣi 1 Activity moi sӁ tiӃn tӟi trҥng thái stop. 4. Killed. Khi hӋ thӕng thiӃu bӝ nhӟ nó sӁ giҧi phóng các tiӃn trình theo nguyên tҳc ưu tiên. Các Activity ӣ trҥng thái stop hay paused cũng có thӇ bӏ giҧi phóng, và khi nó đưӧc hiӇn thӏ lҥi thì các Activity này phҧi khӣi đӝng lҥi hoàn toàn và phөc hӗi trҥng thài trưӟc đó. Android Activity Life Cycle Android Activity Life Cycle Vòng đӡi cӫa Activity State. 1. Entire lifetime. Tӯ phương thӭc (method) onCreate() ĺ onDestroy(). 2. Visible lifetime. Tӯ phương thӭc (method) onStart() ĺ onStop(). 3. Foreground lifetime. Tӯ phương thӭc (method) onResume() ĺ onPause(). Khi xây dӱng Activity cho ӭng dөng cҫn phҧi viӃt lҥi phương thӭc onCreate() đӇ thӵc hiӋn quá trình khӣi tҥo. Các phương thӭc khác có cҫn viӃt lҥi hay không thì tùy thuӝc vào lұp trình. ]ML Android Thành phҫn cơ bҧn trong Android. 1. Các Layout. Dùng đӇ quҧn lý các thành phҫn giao diӋn khác theo mӝt trình tӵ nhҩt đӏnh. - FrameLayout: Là layout đơn giҧn nhҩt, thêm các thành phҫn con vào góc trên bên trái màn hình. - LinearLayout: Thêm các thành phҫn con theo mӝt chiӅu nhҩt đӏnh (ngang hoһc dӑc). Đây là layout đưӧc sӱ dөng nhiӅu nhҩt. - RelativeLayout: Thêm các thành phҫn con dӵa trên mӕi quan hӋ vӟi các thành phҫn khác hoһc vӟi biên cӫa layout. - TableLayout: Thêm các thành phҫn con dӵa trên mӝt lưӟi các ô ngang và dӑc. - AbsoluteLayout: Thêm các thành phҫn con dӵa theo tӑa đӝ x,y. Layout đưӧc sӱ dөng nhҵm mөc đích thiӃt kӃ giao diӋn cho nhiӅu đӝ phân giҧi. Khi lұp trình thưӡng kӃt hӧp nhiӅu layout đӇ tҥo ra giao diӋn minh mong muӕn. 2. ]ML unit. Bưӟc 1: Khӣi tҥo 1 project. File ĺ New ĺ Other ĺ Android ĺ Android ĺ Project. Bưӟc 2: ĐiӅn thông tin Project. - Project name. - BuilTarget. - Application name. - Package. - Create Activity - Finish. ]ML Android 2. ]ML unit (tiӃp). Bưӟc 3: Nhұn diӋn Package Explorer tӟi thư mөc res gӗm 3 thư mөc con. - drawable: chӭa các hình ҧnh đӇ làm icon hoһc tài nguyên cho giao diӋn «.. - layout: chӭa các file xml đӇ thiӃt kӃ giao diӋn. - values: chӭa các giá trӏ sӱ dөng trong ӭng dөng đưӧc mình đӏnh nghĩa như các dòng ký tӵ (string), màu (color), các themes«.. Bưӟc 4. ThiӃt lұp file xml. Vào thư mөc layout chon file main.xml và edit code. Vd: Trong file xml này ta khai báo 1 Linear Layout vӟi 2 thành phҫn con là 1 Edit Text (dùng đӇ gõ ký tӵ) và 1 Text Views (hiӇn thӏ ký tӵ). Linear Layout đưӧc khai báo tӯ khóa V Vnhҵm chӍ ra chiӅu sҳp xӃp cӫa 2 thành phҫn con là chiӅu dӑc. Vӟi layout_widht và layout_height có thӅ cho giá trӏ là hoһc V đӇ thông báo thành phҫn này sӁ có chiӅu rӝng (dài) phӫ đҫy thành phҫn cha hoһc chӍ vӯa bao đӫ nӝi dung. Trong Edit Text và Text View có tӯ khóa
, tӯ khóa này cho phép khai báo id cӫa các thành phҫn đӇ lҩy vӅ trong khi lұp trình. Tӯ khóa trong Edit Text cho phép hiӋn ra nӝi dung mӡ khi Edit Text vүn chưa có ký tӵ nào như ³@string/edit_hint´ là thông báo lҩy tӯ file string.xml chuӛi cò tên là edit_hint. VV cӫa Text View thì thông báo chuӛi sӁ đưӧc hiӇn thӏ vӟi màu lҩy trong file colors.xml, chӍ ra cӥ chӳ bҵng 28pixel, chӍ ra kiӇu chӳ là monospace. ]ML Android 2. ]ML unit (tiӃp) File main.xml. <? xml version=³1.0´ encoding=³utf-8´?> <LinearLayout xmlns:android=³ http://schemas.android.com/apk/res/android ´ android: orientation=³vertical´ android: layout_width=³fill_parent´ android: layout_height=³fill_parent´ > <EditText android: id=³@+id/edit_text´ android: layout_width=³fill_parent´ android: layout_height=³wrap_content´ androi: hint=³@string/edit_hint´ /> <TextView android: id=³@+id/text_view´ android: layout_width=³fill_parent´ android: layout_height=³wrap_content´ android: textColor=³@color/text_colors´ android: textSize=³28px´ android: typeface=³monospace´ /> </LinearLayout> ]ML Android 2. ]ML unit (tiӃp) Bưӟc 5. Trong thư mөc res vào values chӑn file string.xml và bә xung thêm dòng đӏnh nghĩa cho edit_hint như sau: <?xml version=³1.0´ encoding=³utf-8´?> <resources> <string name=³hello´> Hello Word. Example! </string> <string name=³app´> Example! </string> <string name=³edit_hint´> Enter the word here! </string> </resources> Bưӟc 6. Trong thư mөc values tҥo file colors.xml. RightClick thư mөc values ĺ New ĺ Android ]ML File. <?xml version=³1.0´ encoding=³utf-8´?> <resources> <color name=³text_color´> #fffssd </color> </resources> ]ML Android 2. ]ML unit (tiӃp) Bưӟc 7. Vào src viӃt code xӱ lý các thành phҫn giao diӋn. Vào src ĺ at.exam ĺ Example.java. Example.java. package at.exam; public class Example extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //ThiӃt lұp giao diӋn lҩy tӯ file main.xml //Lҩy vӅ các thành phҫn trong main.xml thông qua id final EditText edit = (EditText) findViewById(R.id.edit_text); final TextView text = (TextView) findViewById(R.id.text_view); //ThiӃt lұp xӱ lý cho sӵ kiӋn nhҩn nút giӳa cӫa điӋn thoҥi edit.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { text.setText(edit.getText().toString()); edit.setText(""); return true; } else { return false; } } }); } } Notes - ĐӇ truy cұp các thành phҫn đӏnh nghĩa trong ]ML thì dùng lӋnh: ² Vd: R.layout.main, R.id.edit_text«« - LӋnh findViewById: trҧ vӅ 1 View có thiӃt lұp id trong ]ML. Còn ép kiӇu la do View là lӟp cha cӫa EditText và TextView. - Đӕi vӟi string hay color«thi dùng lӋnh ²V . Vd: getResource().getColor(R.color.text_color). Android Manifest Mӛi hӋ thӕng đӅu có file AndroidManifest.xml nó dùng đӇ mô tҧ nhӳng thông tin quan trong cho hӋ thӕng Android biӃt. Công viӋc cӫa AndroidManifest.xml. - Đһt tên Java packpage cho ӭng dөng. - Mô tҧ các thành phҫn (component) cӫa ӭng dөng: activity, service, broadcast reciever, content provider. - Thông báo nhӳng permission ma ӭng dөng cҫn có đӇ truy cұp các protected API và tương tác vӟi các ӭng dөng khác. - Thông báo nhӳng permission mà các ӭng dөng khác cҫn có đӇ tương tác vӟi ӭng dөng hiӋn thӡi. - Thông báo level thҩp nhҩt mà Android API mà ӭng dөng cҫn đӇ chҥy. Flie AndroidManifest.xml. <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="at.exam" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Example" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="3" /> </manifest> Android Manifest Example AndroidManifest.xml. <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="android.at³ android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".TooDo" android:screenOrientation="landscape" //cho phép thiӃt lұp giao diӋn khi vào ӭng dөng theo chiӅu dӑc(portrait-mһc đӏnh) hay chiӅu ngang (landscape) android:theme="@android:style/Theme.NoTitleBar.Fullscreen" //cho phép sӱ dөng stype có sҹn cӫa android là full-screen. 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=".WorkEnter"> // cho phép nhұp thӡi gian và nӝi dung công viӋc. </activity> <receiver android:name=".AlarmReceiver"> //nhұn alarm gӣi tӟi trong intent. Khi alarm đưӧc nhұn sӁ có âm thanh </receiver> // và rung (vibration) </application> <uses-sdk android:minSdkVersion="3" /> <uses-permission android:name="android.permission.VIBRATE"/> </manifest> Android Manifest Intent filter là bӝ lӑc dùng đӇ giӟi hҥn các intent đưӧc sӱ dөng trong Activity hay Receiver« Vd: <intent-filter> <action android:name="android.intent.action.VIEW"/> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> <data android:scheme="http" android:host="www.google.com" android:path="/m/products/scan"/> </intent-filter> Trong đó: Bӝ lӑc chӍ cho phép intent mӣ internet vӟi đưӡng dүn đӏnh nghĩa sҹn là: ³www.google.com/m/products/scan´ Working with View Ӭng dөng xây dӵng chương trinh nhұp time va công viӋc rӗi in ra. Bưӟc 1: Tao 1 procject. Bưӟc 2. Vào main.xml xây dӵng giao diӋn chương trình. Bao gӗm 1 Linear Layout, 1 Edit Text, 1 List View, 1 Button. Tӯ khóa đưӧc dùng đӇ cӕ đӏnh sӕ dòng và nên sӱ dөng trong Edit Text thay vì chӍ dòng mӛi wrap_content nӃu sӱ dөng wrap_content thì Edit Text nó sӁ tӵ giãn ra nӃu dòng nhұp vào vưӧt giӟi hҥn bao và nó sӁ làm hӓng gaio diӋn đã thiӃt kӃ. Tӯ khóa thông báo các thành phҫn con sӁ đưӧc sҳp xӃp như thӃ nào ӣ thành phҫn cha. Tӯ khóa
dùng đӇ cách 1 khoҧng cách cho thành phҫn. NӃu khong so padding thi 2 thành phҫn con sӱ dөng 1 Linear Layout sӁ đưӧc xҩp sát nhau, nӃu 1 thành phҫn con có dùng padding thì nó sӁ tҥo khoҧng cách vӟi thành phҫn còn lai như mong muӕn. Ngoài ra còn có
²
V
VV Tӯ khóa dùng đӇ giӟi hҥn dҥng ký tӵ nhұp vào. Tӯ khóa dùng đӇ giӟi hҥn sӕ ký tӵ nhұp vào. Working with View Bưӟc 2. (tiӃp) android:padding="5px³ <?xml version="1.0" encoding="utf-8"?> android:numeric="integer³ android:maxLength="2" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <TextView android:layout_width="65px" android:orientation="vertical" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/minute_edit" android:layout_height="fill_parent" > android:typeface="normal" <EditText android:id="@+id/work_enter" android:textSize="15px³ android:layout_width="fill_parent" android:textStyle="bold³ android:layout_height="wrap_content³ android:padding="5px" /> android:hint="@string/work_hint" <EditText android:id="@+id/minute_edit³ android:lines="1" android:layout_width="45px³ android:textSize="24px" /> android:layout_height="wrap_content³ <LinearLayout android:layout_width="wrap_content" android:hint="00" android:layout_height="wrap_content" android:textColorHint="@color/hint_color" android:orientation="horizontal" > android:textSize="20px" <TextView android:layout_width="50px" android:gravity="center" android:layout_height="wrap_content" android:padding="5px" android:text="@string/hour_edit" android:numeric="integer" android:typeface="normal" android:maxLength="2" /> android:textSize="15px" </LinearLayout> android:textStyle="bold" <Button android:id="@+id/button" android:padding="5px" /> android:layout_width="wrap_content" <EditText android:id="@+id/hour_edit" android:layout_height="wrap_content" android:layout_width="45px" android:gravity="center" android:layout_height="wrap_content³ android:text="@string/button_content" /> android:hint="12³ <ListView android:id="@+id/list" android:textColorHint="@color/hint_color³ android:layout_width="fill_parent" android:textSize="20px³ android:layout_height="wrap_content" /> android:gravity="center³ </LinearLayout> Working with View Bưӟc 3: ThiӃt lұp lҥi trong string.xml. <?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Example 2</string> <string name="work_hint">Enter the work here</string> <string name="hour_edit">Hour</string> <string name="minute_edit">Minute</string> <string name="button_content">Add work</string> </resources> Bưӟc 4: tao mӟi file colors.xml trong foder values. <?xml version="1.0" encoding="utf-8"?> <resources> <color name="hint_color">#cccccc</color> </resources> Bưӟc 5: ThiӃt lұp lҥi file Example.java trong foder src. - Khӣi tҥo đӕi tưӧng arrayAdapter vӟi các đӕi sӕ truyӅn vào là (this, android.R.layout.simple_list_item_1, arrayWork), this là đӕi sӕ cӫa class context. - Context thuӝc class android.content (android.content.Context). + Là mӝt interface chӭa hҫu hӃt thông tin vӅ môi trưӡng ӭng dөng cӫa android, có nghĩa là mӑi thao tác tương tác vӟi hӋ điӅu hành điӅu phҧi qua lӟp này. +Nó là 1 class abstract cung cҩp cho nhӳng lӟp khác các phương thӭc đӇ tương tác vӟi hӋ thӕng android. +Nó cho phép truy cұp tӟi các nguӗn tài nguyên(resource) đã đưӧc đӏnh nghĩa và các lӟp khác. Ví dө như có thӇ khӣi tҥo và chҥy các activities các broadcast, các intents«Chúng ta hãy coi content là mӝt lӟp ӣ mӭc ӭng dөng (Application level ± liên quan tӟi hӋ thӕng) . + Tóm lҥi là content giúp ta dӉ dàng truy cұp và tương tác tӟi các tài nguyên cӫa hӋ thӕng, các thông tin, các dӏch vө, các thông sӕ cҩu hình, database,wallpaper, danh ba, cuӝc gӑi, kӃt nӕi, chӃ đӝ rung«. Working with View Bưӟc 5. (tiӃp) + sӣ dĩ hҫu hӃt các lӟp có liên quan tӟi UI (layout, button, textview, imageview, listview,...) đӅu pҧi super tӟi Context vì bҧn thân nó đҧm nhiӋm viӋc truy cұp resource (R.id, R.layout,....). NӃu chúng ta không tham chiӃu tӟi Context class thì đương nhiên không thӇ dùng tӟi các resources mà chúng ta đã tҥo ra. - android.R.layout.simple_list_item_1 là đӏnh nghĩa cách thӇ hiӋn items trong ListView. LӋnh android.R* là các tài nguyên (resource) có sҹn cӫa android cho phép truy cұp và sӱ dөng. - arrayWork chính là mҧng cҫn đưӧc bind cӫa adapter. - AlertDialog là lӟp cho phép đưa ra 1 hӝp thoҥi. - Thӫ thuұt sӱ dөng Listener có 3 cách. + Cách 1: class A implement Listener b.addListerner(this); + Cách 2: Listerner a= new Listener(){}; b.addListener(a); + Cách 3: b.addListener(new Listener(){}); Working with View Bưӟc 5: (tiӃp) File Example.java OnClickListener add = new OnClickListener() { @Override package at.exam; public void onClick(View v) { import java.util.ArrayList; //NӃu 1 trong 3 Edit Text không có nӝi dung thì hiӋn lên cҧnh báo import android.app.Activity; if (workEnter.getText().toString().equals("") || import android.app.AlertDialog; hourEdit.getText().toString().equals("") || import android.content.DialogInterface; minuteEdit.getText().toString().equals("")) { import android.os.Bundle; AlertDialog.Builder builder = new AlertDialog.Builder(Example.this); import android.view.View; builder.setTitle("Info missing"); import android.view.View.OnClickListener; builder.setMessage("Please enter all information of the work"); import android.widget.ArrayAdapter; builder.setPositiveButton("Continue", new import android.widget.Button; DialogInterface.OnClickListener() { import android.widget.EditText; public void onClick(DialogInterface dialog, int which) { import android.widget.ListView; // TODO Auto-generated method stub public class Example extends Activity { } /** Called when the activity is first created. */ }); @Override builder.show(); public void onCreate(Bundle savedInstanceState) { } super.onCreate(savedInstanceState); //Lҩy nӝi dung công viӋc và thӡi gian ra tӯ Edit Text và đưa vào list setContentView(R.layout.main); else { //Tҥo mҧng đӇ chӭa String nӝi dung công viӋc và giӡ String str = workEnter.getText().toString() + " - " final ArrayList<String> arrayWork = new ArrayList<String>(); + hourEdit.getText().toString() + ":" //Adapter dùng đӇ kӃt nӕi mҧng vӟi List View + minuteEdit.getText().toString(); final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, arrayWork.add(0,str); android.R.layout.simple_list_item_1, arrayWork); arrayAdapter.notifyDataSetChanged(); //Các EditText đӇ vào nӝi dung công viӋc đưӧc lҩy vӅ tӯ ]ML workEnter.setText(""); final EditText workEnter = (EditText) findViewById(R.id.work_enter); hourEdit.setText(""); final EditText hourEdit = (EditText) findViewById(R.id.hour_edit); minuteEdit.setText(""); final EditText minuteEdit = (EditText) findViewById(R.id.minute_edit); } //Button khi nhҩn sӁ thêm công viӋc vào ListView } final Button button = (Button) findViewById(R.id.button); //ListView chӭa danh sách công viӋc }; final ListView list = (ListView) findViewById(R.id.list); //set Listener cho button //Cҫn set Adapter cho list đӇ biӃt sӁ lҩy nӝi dung tӯ mҧng arrayWork button.setOnClickListener(add); list.setAdapter(arrayAdapter); } //Đӏnh nghĩa Listener xӱ lý sӵ kiӋn nhҩn vào button } Custom ViewGroup ]ây dӵng ViewGroup cho phép ta xây dӵng 1 tұp các widget đưӧc sҳp xӃp theo ý muӕn rӗi đưa vào sӱ dөng.