Hướng dẫn lập trình cơ bản với Android - Bài 4 doc

20 366 0
Hướng dẫn lập trình cơ bản với Android - Bài 4 doc

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Hư ng d n l p trình b n v i Android - Bài http://diendan.vietandroid.com/cac-bai-hoc-co-kem-ma-nguon/831-huongdan-lap-trinh-co-ban-voi-android-bai-4-a.html Reflink: List tutorial Bài Bài Bài Bài Bài Bài Bài Bài - Cài ñ t s d ng Android v i Eclipse Cơ b n Android Xây d ng giao di n ñơn gi n ViewGroup Custom Adapter Intent Broadcast Receiver Service SQLite Content Provider Sorry m i ngư i lâu m i ñư c ti p theo M t ph n cơng vi c b n b u, m t ph n lư i + ng i ^_^ mà ñã tr h n v i anh em Mình đốn nh ng newbie đ c c a t 1, 2, gi s p thành pro h t r i, v y ñành dành cho newbie m i Trong s sâu nói rõ v Intent, ph n b n đóng vai trị r t quan tr ng l p trình ng d ng Android Khái ni m v Intent: Theo ñ nh nghĩa c a Google, Intent m t miêu t v m t ho t ñ ng c n ñư c th c hi n Cịn nói m t cách ñơn gi n d hi u hơn, Intent m t c u cho phép truy n thông ñi p gi a thành ph n c a ng d ng gi a ng d ng v i Các thu c tính c a Intent: - action: hành ñ ng ñư c th c hi n, vd : ACTION_VIEW, ACTION_MAIN - data: d li u s ñư c x lý action, thư ng ñư c di n t m t Uri (Uniform Resource Identifier, tham kh ohttp://en.wikipedia.org/wiki/Uniform rce_Identifier ñ hi u rõ thêm chi ti t) VD: ACTION_VIEW content://contacts/people/1 - Hi n th thông tin v ngư i v i mã danh ACTION_DIAL content://contacts/people/1 - Hi n th hình g i đ n ngư i v i mã danh ACTION_DIAL tel:123 - Hi n th hình g i v i s g i 123 Ngoài cịn có s thu c tính mà ta có th b sung vào Intent: - category: b sung thêm thông tin cho action c a Intent VD: CATEGORY_LAUNCHER thông báo s thêm vào Launcher m t ng d ng top-level - type: ch rõ ki u c a data - component: ch rõ thành ph n s nh n x lý intent Khi thu c tính đư c xác đ nh thu c tính khác s tr thành thu c tính ph - extras: mang theo ñ i tư ng Bundle ch a giá tr b sung VD: ACTION_MAIN CATEGORY_HOME: tr v hình Home c a Android (khi b m nút Home c a di ñ ng) Phân lo i Intent: Intent ñư c chia làm lo i: www.Beenvn.com – T sách online - Explicit Intents: intent ñã ñư c xác ñ nh thu c tính component, nghĩa ñã ch rõ thành ph n s nh n x lý intent Thông thư ng intent d ng s không b sung thêm thu c tính khác action, data Explicit Intent thương đư c s d ng ñ kh i ch y activity ng d ng - Implicit Intents: Intent khơng ch rõ component x lý, thay vào b sung thơng tin thu c tính Khi intent ñư c g i ñi, h th ng s d a vào nh ng thơng tin đ quy t đ nh component thích h p nh t đ x lý VD: ACTION_DIAL tel:123 thơng thư ng s ñư c h th ng giao cho activity Phone Dialer m c ñ nh c a Android x lý M t s action thư ng s d ng Intent: ACTION_ANSWER - m Activity ñ x lý cu c g i t i, thư ng Phone Dialer c a Android ACTION_CALL - m Phone Dialer (m c ñ nh PD c a Android) l p t c th c hi n cu c g i d a vào thông tin data URI ACTION_DELETE - m Activity cho phép xóa d li u mà ñ a ch c a ch a data URI ACTION_DIAL - m Phone Dialer (m c ñ nh PD c a Android) n thơng tin l y t ñ a ch ch a data URI ACTION_EDIT - m Activity cho phép ch nh s a d li u mà ñ a ch l y t data URI ACTION_SEND - m Activity cho phép g i d li u l y t data URI, ki u c a d li u xác ñ nh thu c tính type ACTION_SENDTO - m Activity cho phép g i thơng p t i ñ a ch l y t data URI ACTION_VIEW - action thông d ng nh t, kh i ch y activity thích h p đ hi n th d li u data URI ACTION_MAIN - s d ng ñ kh i ch y Activity OK, lý thuy t th ñã t m n Gi s chuy n qua ph n th c hành ñ hi u rõ cách s d ng Intent Như ñã nêu trên, Intent chia làm lo i: explicit intent implicit intent M i lo i Intent s có cách cài đ t s d ng khác Using Explicit Intents Yêu c u: Xây d ng chương trình g m Activity Activity1 Activity ch y ban ñ u lúc kh i ñ ng ng d ng, cho phép nh p vào giá tr , cho phép kh i ch y Activity2 g i giá tr t i Activity2 Activity2 s nh n hi n th giá tr , r i l i g i giá tr t i BroadcastReceiver Cơ ch g i kh i ch y Activity s d ng thông qua Intent B1: Kh i t o project: File -> New -> Android Project Project name: Explicit Intent Example Build Target: Ch n Android 1.5 Application name: Explicit Intent Example Package name: at.exam Create Activity: Activity1 => Kích nút Finish B2: T o giao di n cho Activity1 -> res\layout\main.xml chuy n tên thành activity1_layout.xml Mã: Layout cho Activity1 bao g m LinearLayout ch a TextView, EditText ñ nh p giá tr (ñã gi i h n ki u nh p number), RelativeLayout có Button đ kh i ch y Activity2 Mình s d ng RelaytiveLayout đ có th x p Button xu ng phía cu i c a giao di n B3: T o giao di n cho Activity2 -> Chu t ph i vào folder res\layout -> New -> Android XML File ->Gõ tên activity2_layout.xml Mã: Layout c a Activity2 tương t Activity1, Button bây gi ñ g i BroadCast Receiver Ngồi dùng EditText đ hi n th value nh n đư c (do có đư ng bao ngồi đ p TextView ^_^) nên không cho phép nh p giá tr vào EditText Mã: android:enabled="false" B4:S a l i n i dung c a Activity1.java sau: Mã: package at.exam; import import import import import import import android.app.Activity; android.content.Intent; android.os.Bundle; android.view.View; android.view.View.OnClickListener; android.widget.Button; android.widget.EditText; public class Activity1 extends Activity { /** Called when the activity is first created */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity1_layout); final EditText editValue = (EditText) findViewById(R.id.value_edit); www.Beenvn.com – T sách online final Button sendButton = (Button) findViewById(R.id.send_button); sendButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { String valueString = editValue.getText().toString(); long value; if (valueString != null) { value = Long.parseLong(valueString); } else { value = 0; } //T o ñ i tư ng Bundle ñ g i ñi Intent Bundle sendBundle = new Bundle(); sendBundle.putLong("value", value); //T o Intent ñ kh i ch y Activity2 g n sendBundble vào Intent Intent i = new Intent(Activity1.this, Activity2.class); i.putExtras(sendBundle); startActivity(i); Stack ta s //Gi i phóng Activity1 kh i Activity ko quay l i n a finish(); } }); } } B5: T o m i Class Activity2.java package at.exam -> ch nh s a n i dung: Mã: package at.exam; import import import import import import import android.app.Activity; android.content.Intent; android.os.Bundle; android.view.View; android.view.View.OnClickListener; android.widget.Button; android.widget.EditText; public class Activity2 extends Activity { /** Called when the activity is first created */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity2_layout); www.Beenvn.com – T sách online final EditText receiveValueEdit = (EditText) findViewById(R.id.value_receive); final Button callReceiverButton = (Button) findViewById(R.id.call_button); //L y v Bundle ñư c g i kèm Intent r i l y giá tr Bundle receiveBundle = this.getIntent().getExtras(); final long receiveValue = receiveBundle.getLong("value"); receiveValueEdit.setText(String.valueOf(receiveValue)); callReceiverButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { //Kh i t o Intent ñ g i t i BroadCast Receiver //G n giá tr vào Intent, l n ko c n Bundle n a Intent i = new Intent(Activity2.this, Receiver.class); i.putExtra("new value", receiveValue 10); sendBroadcast(i); } }); } } B6: T o BroadCast Receiver ñ nh n Intent mà Activity2 g i t i -> T o file Receiver.java at.exam -> N i dung: Mã: package at.exam; import import import import android.content.BroadcastReceiver; android.content.Context; android.content.Intent; android.widget.Toast; public class Receiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { long value = intent.getLongExtra("new value", -10) + 10; Toast toast = Toast.makeText(context, "Broadcast Receiver catch an Intent" + " \n" + "The value is stored in the Intent is " + String.valueOf(value), Toast.LENGTH_LONG); toast.show(); } } Code khơng h khó hi u, add comment Ch c n lưu ý ñây Toast l p đ hi n th m t thơng báo ñơn gi n kho ng th i gian c ñ nh, ko th thay ñ i th i gian T_T (why???) ch có th ch n gi a LENGTH_SHORT v i LENGTH_LONG www.Beenvn.com – T sách online B7: B sung thêm thông tin v component m i vào AndroidManifest.xml: Mã: www.Beenvn.com – T sách online www.Beenvn.com – T sách online www.Beenvn.com – T sách online ðã xong s d ng Explicit, gi ñ n lư t Implicit Intent Trư c ñi vào ví d , d o qua chút ki n th c v Intent Filter vai trò c a Intent Filter Activity, Service BroadCast Receiver s d ng Intent Filter đ thơng báo cho h th ng bi t d ng Implicit Intent mà có th x lý Nói cách khác, Intent Filter b l c Intent, ch cho nh ng Intent đư c phép qua Intent Filter mơ t kh c a component đ nh nghĩa Khi h th ng b t ñư c Implicit Intent (ch ch a s thông tin chung chung v action, data category ), s s d ng nh ng thông tin Intent này, ki m tra ñ i chi u v i Intent Filter c a component ng d ng, sau quy t ñ nh kh i ch y ng d ng thích h p nh t đ x lý Intent b t đư c N u có hay nhi u ng d ng thích h p, ngư i dùng s ñư c l a ch n ng d ng mu n VD: Mã: www.Beenvn.com – T sách online Trên Activity v i b l c Intent cho phép b t x lý Intent g i SMS Hãy lưu ý t khóa Mã: andoid:scheme T khóa cho bi t protocol (lu t) ñ x lý d li u URI Nói cách đơn gi n ki u c a d li u s ki u khác http, https, fpt, content Using Implicit Intent: Yêu c u: Xây d ng chương trình nh p s g i Lưu ý chương trình c a ch xây d ng đ n m c nh n nút Call c a di ñ ng s ch y ng d ng hi n th giao di n cho phép nh p s Ph n g i dành cho yêu thích tìm hi u thêm ^_^ Ph n khơng h khó ch mu n minh h a Implicit Intent nên s khơng đưa vào B1: Kh i t o project: File -> New -> Android Project Project name: Implicit Intent Example Build Target: Ch n Android 1.5 Application name: Implicit Intent Example Package name: at.exam Create Activity: Example => Kích nút Finish B2: ðây bư c quan tr ng nh t bư c có ý nghĩa nh t c project này, bư c l i ch bư c râu ria mà thêm vào cho project h n chút Bư c s thêm b l c Intent Filter vào cho activity Example c a ñ b t s ki n nh n nút Call c a di ñ ng -> Vào AndroidManifest.xml ch nh s a sau: Mã: www.Beenvn.com – T sách online Th c ch t ch b sung thêm dịng ch đ đánh d u thơi ^_^ B3: Xây d ng giao di n main.xml, bư c ko quan tr ng, ch râu ria cho activity có giao di n: Mã: LinearLayout ch a TextView ñ hi n th s nh n, TableLayout có Button tương ng v i s Button ñ clear cho TextView B4: Code code code So tired Tutorial is really take time Ch nh Example.java: Mã: package at.exam; import import import import import import import import android.app.Activity; android.os.Bundle; android.view.Menu; android.view.MenuItem; android.view.View; android.view.View.OnClickListener; android.widget.Button; android.widget.TextView; www.Beenvn.com – T sách online public class Example extends Activity { Button button1, button2, button3; Button button4, button5, button6; Button button7, button8, button9; Button button0, buttonStar, buttonClear; TextView numberView; /** Called when the activity is first created */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); numberView = (TextView) findViewById(R.id.number_display); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button3 = (Button) findViewById(R.id.button3); button4 = (Button) findViewById(R.id.button4); button5 = (Button) findViewById(R.id.button5); button6 = (Button) findViewById(R.id.button6); button7 = (Button) findViewById(R.id.button7); button8 = (Button) findViewById(R.id.button8); button9 = (Button) findViewById(R.id.button9); button0 = (Button) findViewById(R.id.button0); buttonStar = (Button) findViewById(R.id.button_star); buttonClear = (Button) findViewById(R.id.button_clear); button1.setOnClickListener(this.appendString("1")); button2.setOnClickListener(this.appendString("2")); button3.setOnClickListener(this.appendString("3")); button4.setOnClickListener(this.appendString("4")); button5.setOnClickListener(this.appendString("5")); button6.setOnClickListener(this.appendString("6")); button7.setOnClickListener(this.appendString("7")); button8.setOnClickListener(this.appendString("8")); button9.setOnClickListener(this.appendString("9")); button0.setOnClickListener(this.appendString("0")); buttonStar.setOnClickListener(this.appendString("*")); buttonClear = (Button) findViewById(R.id.button_clear); buttonClear.setOnClickListener(new OnClickListener() { public void onClick(View v) { numberView.setText(""); } }); } public OnClickListener appendString(final String number) { return new OnClickListener() { public void onClick(View arg0) { www.Beenvn.com – T sách online numberView.append(number); } }; } public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(0, Menu.FIRST, 0,"Exit" ).setIcon(android.R.drawable.ic_delete); return true; } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case Menu.FIRST: { finish(); break; } } return false; } } Code ñơn gi n, cịn ko thèm comment n a Lưu ý có Option Menu đ đóng Activity đóng ng d ng B5: Time to test Kh i ch y project, r i s d ng Option Menu c a (b m nút Menu c a Emulator ho c di đ ng Android) đ kh i chương trình Ok, sau ch n Exit ta có th ch c ch n ng d ng đư c đóng hồn tồn, activity ko cịn t n t i stack c a Emulator/di ñ ng n a Gi nh n nút Call c a Emulator/di ñ ng, Tadaaaaaaaaa www.Beenvn.com – T sách online www.Beenvn.com – T sách online www.Beenvn.com – T sách online www.Beenvn.com – T sách online K t thúc www.Beenvn.com – T sách online ... android: text="Activity - Send value" android: typeface="normal" android: textSize="14px" android: textStyle="bold" android: textColor="#cccccc" android: background="#333333" />

Ngày đăng: 22/06/2014, 02:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan