1. Trang chủ
  2. » Thể loại khác

LẬP TRÌNH DI ĐỘNG TRÊN MÔI TRƯỜNG ANDROID

334 94 0

Đ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

TR NG I H C QU C T H NG BÀNG KHOA CỌNG NGH THỌNG TIN _oOo_ LẬP TRÌNH DI ĐỘNG TRÊN MÔI TRƯỜNG ANDROID (TÀI LI U THAM KH O) LÊ VĂN HẠNH 2014 M CL C Ph n I: KH I U 1.1 C U HÌNH MỌI TR NG PHÁT TRI N Android (Android Integrated Development Environment ( Android IDE)) 1.1.1 H u hƠnh h tr 1.1.2 Môi tr ng phát tri n h tr 1.1.3 CƠi đ t ADT Bundle - “b thu g n” 1.1.4 T o Android Emulator (AVD) 1.2 T O NG D NG N GI N 1.2.1 T o m i Project 1.2.2 C u trúc m t project 12 1.2.3 Tìm hi u v XML Layout vƠ Java Code 13 1.2.4 Ch y ng d ng 14 1.2.5 Tùy bi n ng d ng đ u tiên v a xơy d ng 16 1.3 TÌM HI U V Android Project  Android Activity 18 1.3.1 Application 18 1.3.2 Gi i thi u activity 19 1.3.3 T o m i activity 20 1.3.4 Khai báo activity file AndroidManifest.xml 20 1.3.5 Kh i ch y m t activity 20 1.3.6 Chuy n thông tin gi a hai Activities 28 1.3.7 Intents 35 1.3.8 V n d ng ki u m Activity m i l p trình 41 1.3.9 Vòng đ i c a Activity 41 1.4 S D NG TÀI NGUYểN TRONG Android 44 1.4.1 Các file v tƠi nguyên Android Project 44 1.4.2 Các tƠi nguyên th ng dùng 45 1.5 BÀI T P T NG H P PH N I 55 1.5.1 M r ng t nh ng bƠi th c hƠnh đư th c hi n 55 1.5.2 BƠi t p 55 Ph n II: GIAO DI N NG I DÙNG 56 2.1 S D NG Layouts 56 2.1.1 M t s khái ni m 56 2.1.2 M t s v n đ c n quan tơm thi t k giao di n vƠ vi t mư l nh 57 2.1.3 Xơy d ng ng d ng 59 2.1.4 ViewGroup 63 2.2 M T S CONTROL C B N 81 2.2.1 S d ng u n nh p li u c b n 81 2.2.2 S d ng u n v i Adapters 89 2.2.3 ProgressBars and SeekBars 94 2.2.4 ImageViews 105 2.3 ActionBar  Menu Navigation 108 2.3.1 Tìm hi u v Options Menu 108 2.3.2 S d ng Action Bar 114 2.4 Activities  Fragments 122 2.4.1 Fragments 122 2.4.2 Di chuy n gi a Fragments 128 2.4.3 T ng tác gi a Fragment vƠ Activity 134 2.5 CÁC D NG Dialogs 137 2.5.1 Dialog Fragment 137 i 2.5.2 Dialogs dùng cho vi c ch n ngƠy vƠ gi 144 2.5.3 S d ng Dialogs d ng c nh báo (Alert dialog) 147 2.6 LISTS, GRIDS, GALLERIES,  FLIPPERS 152 2.6.1 ListFragments 152 2.6.2 Grids Galleries 158 2.6.3 S d ng AdapterViewFlipper 164 2.6.4 M t s control dùng vi c phơn trang d ng cu n ngang 167 2.7 BÀI T P T NG H P PH N II 167 2.7.1 Thi t k giao di n 167 2.7.2 BƠi t p v fragment 169 2.7.3 BƠi t p v x lỦ c b n 170 2.7.4 ListView k t h p v i c u trúc d li u 176 2.7.5 T o ng d ng đ nh d ng file ch 178 2.7.6 ActionBar 178 2.7.7 TimePickerDialog k t h p v i dialog fragment 178 Ph n III: L U TR D LI U 179 3.1 QU N Lụ CÁC TỐY CH N (Preferences) 179 3.1.1 S d ng SharedPreferences 179 3.1.2 Thi t l p tùy ch n c a ng i dùng (User Preferences) 181 3.2 SQLite 193 3.2.1 Gi i thi u 193 3.2.2 CƠi đ t SQLite Windows 195 3.2.3 Ki u d li u SQLite 197 3.2.4 L nh SQLite 197 3.2.5 Toán t SQLite(SQLite Operators) 200 3.2.6 Bi u th c SQLite (SQLite Expressions) 203 3.2.7 Các l nh liên quan đ n CSDL 203 3.2.8 Các l nh liên quan đ n c u trúc c a TABLE 205 3.2.9 L nh Insert Into 209 3.2.10 L nh truy v n d li u 209 3.2.11 Update 218 3.2.12 Delete 218 3.2.13 L nh VACUUM 219 3.2.14 Sub Queries 219 3.2.15 Views 221 3.2.16 Trigger 222 3.2.17 Transactions 224 3.2.18 Indexes 225 3.2.19 Các hƠm th ng dùng SQLite 227 3.2.20 SQLite PRAGMA 230 3.3 THAO TÁC V I FILE C A NG D NG C T O RA TRểN AVD 234 3.4 S D NG CSDL SQLite TRONG Android 236 3.4.1 Package 236 3.4.2 Class 236 3.4.3 Cursor 236 3.4.4 SQLiteOpenHelper class 240 3.4.5 SQLiteDatabase class 241 3.5 Content Provider 252 3.5.1 Gi i thi u 252 3.5.2 Truy c p Content Provider 252 3.5.3 Custom content provider 252 3.5.4 UriMatcher 253 ii 3.5.5 Thread Safety 253 3.6 L U TR D LI U D I D NG FILE 264 3.6.1 S d ng b nh 264 3.6.2 S d ng cache file 266 3.6.3 S d ng b nh 266 3.6.4 M t s ph ng th c h u d ng đ i v i file: 268 3.7 BÀI T P T NG H P CH NG 277 3.7.1 Game 277 Ph n IV: NETWORK & TELEPHONY 290 4.1 NETWORK 290 4.1.1 Giao th c HTTP 290 4.1.2 S d ng k t n i m ng Android 290 4.1.3 WebView 295 4.2 Telephony 300 4.2.1 SMS 300 4.3 Sending e-mail 323 4.4 BÀI T P T NG H P CH NG 330 4.4.1 330 iii L p trình Android Ph n 1: Kh i đ u Ph n I: KH I U (i).C u hình mơi tr ng phát tri n Android (ii).T o ng d ng đ n gi n (iii).Tìm hi u v Android Project Android Activity (iv).S d ng tài nguyên (Resources) Android 1.1 C Ạ HÌNH MƠI TR NG PHÁT TRI N Android (Android Integrated Development Environment ( Android IDE)) Do Android đ c h tr đáng k môi tr môi tr ng phát tri n lƠ Eclipse đ minh h a ng Eclipse, nên tƠi li u nƠy s s d ng 1.1.1 H u hành h tr ứ Windows XP (32-bit), Vista (32- 64-bit), Windows (32- 64-bit) ứ Mac OS X 10.4.8 tr lên ứ Linux (Ubuntu Linux, Lucid Lynx) 1.1.2 Môi tr ng phát tri n h tr ứ Mơi tr ng Android có s phơn bi t gi a Android SDK tools platform tools Tools thƠnh ph n trung tơm c a Software Development Kit (SDK) Chúng đ c s d ng t t c version c a Android Platform tools đ c k t h p v i t ng version riêng bi t c a Android ứ phát tri n ng d ng Android c n cƠi đ t software sau: [1] JDK: (java development kit) 1.7 ho c l n h n [2] IDE (môi tr ng phát tri n): Eclipse (Eclipse 3.5 - Galileo ho c l n h n) [3] ADT (Android Development Tools plugin) [4] SDK (Android Software Development Kit) ứ Yêu c u máy cƠi ph i k t n i m ng q trình cƠi đ t ứ Có th xem h ng d n cƠi đ t t i: http://developer.android.com/sdk/installing.html 1.1.2.1 JDK 1.1.2.1.1 Download JDK ứ VƠo đ a ch sau đ download http://www.oracle.com/technetwork/java/javase/downloads/index.html Hình 1-1 download JDK Lê Văn Hạnh Nov2014 L p trình Android Ph n 1: Kh i đ u ứ Ch n Accept License Agreement Hình 1-2 Ch n Accept License Agreement ứ Ch n phiên b n phù h p v i h u hƠnh c n dùng Hình 1-3 Ch n phiên b n phù h p v i h u hành c n dùng 1.1.2.1.2 Cài đ t JDK ứ Run file v a download, ch n Next cho b c đ hoƠn t t Hình 1-4 Cài đ t JDK 1.1.2.2 Eclipse Lê Văn Hạnh Nov2014 L p trình Android Ph n 1: Kh i đ u 1.1.2.2.1 Download: ứ Download t i đ a ch http://www.eclipse.org/downloads/ ứ Phiên b n đ ngh download lƠ Eclipse Kepler SR2 (4.3.2) Hình 1-5 M t s hình gi i thi u c a eclipse (tùy thu c phiên b n cài đ t) 1.1.2.2.2 S d ng ứ Sau download hoàn t t, th c hi n gi i nén file v a có ứ Run file eclipse.exe ( ) folder v a gi i nén đ m ng d ng Eclipse 1.1.2.2.3 SDK Cài đ t ADT plugin: M eclipse Ch n Help  Install new software Hình 1-6 ch n menu đ cài đ t ADT ứ Ch n button Add h p tho i v a xu t hi n Hình 1-7 ch n button Add ứ m c Name, đ t tên cho ADT, ví d : ADT plugin ứ m c Location, nh p link: https://dl-ssl.google.com/android/eclipse/ ứ Click button OK đ hoƠn t t vƠ đóng h p tho i nƠy Lê Văn Hạnh Nov2014 L p trình Android Ph n 1: Kh i đ u Hình 1-8 nh p tên liên k t đ cài đ t ADT ứ Tr v h p tho i đ u tiên Ch n button Select All ứ B ch n checkbox Contact all … ứ Ch n button Next Hình 1-9 Ch n l a m c cài đ t ADT ứ Ch n button Next h p tho i k ti p Hình 1-10 Xác nh n công c s đ c cài đ t ứ Ch n I accept ầ ứ Ch n button Finish đ b t đ u cƠi đ t Lê Văn Hạnh Nov2014 L p trình Android Ph n 1: Kh i đ u Hình 1-11 Ch n Finish đ b t đ u cài đ t Trong trình cài:  Yêu c u máy ph i k t n i m ng  Có th xu t hi n m t s thơng báo, ch n OK (ho c nút c a thông báo) Sau cƠi đ t hoƠn t t, eclipse s yêu c u kh i đ ng l i 1.1.2.3 Android SDK 1.1.2.3.1 Download Android SDK Download t i link http://developer.android.com/sdk/index.html (chú Ủ đ n h u hƠnh t ng thích) Hình 1-12 CHú ý đ n h u hành t ng thích 1.1.2.3.2 Xác l p cho Eclipse đ ng d n đ n Android SDK ứ Gi i nén file SDK v a download ứ Trong Eclipse, ch n menu Window  Preferences ứ Trong h p tho i Preferences, ch n Android (bên trái) ứ Trong m c SDK location ch n Browse  đ a đ ng d n đ n th m c SDK (th m c ch a th m c v a m i gi i nén, bên folder nƠy có ch a folder TOOLS)  click Apply Lê Văn Hạnh Nov2014 L p trình Android Hình 1-13 ch n Android SDK Manager Ph n 1: Kh i đ u Hình 1-14 Xác l p đ ng d n c a folder SDK 1.1.2.3.3 Cài đ t Android SDK ứ Trong Eclipse, ch n menu Window  Android SDK Manager ứ Trong h p tho i Android SDK Manager, có th ch n h t (cho ti n s d ng sau nƠy) ho c ch ch n Android version c a Android (t 2.1 tr lên) ứ Click ch n button Install XXX packages ầ Hình 1-15 Ch n button XXX packages đ b t đ u cài đ t Hình 1-16 Ch n Accept License Install ứ Ch n Android SDK License vƠ Accept License ứ Click button Install đ b t đ u cƠi đ t 1.1.3 Cài đ t ADT Bundle - “b thu g n” NgoƠi cách cƠi đ t trên, ta có th ch n b cƠi đ t “thu g n” đ s d ng File ADT Bundle c ng cung c p t t c nh ng b n c n đ phát tri n ng d ng Android Eclipse IDE vƠ t t c công c cƠi đ t Eclipse vƠ Eclipse plugin for Android (Android Developer tools), b n download file cƠi đ t v i tên g i lƠ ADT Bundle t i m t s đ a ch nh : ứ http://developer.android.com/sdk/index.html ứ http://www.softpedia.com/get/Programming/SDK-DDK/ADT-Bundle.shtml Do ADT Bundle lƠ m t file nén nên sau download hoƠn t t, b n l n l t b c sau: B1: gi i nén file có tên lƠ adt-bundle-.zip vƠo v trí cho phù h p v i (ví d D:\LapTrinhDiDong) B2: Cho th c thi file Eclipse folder adt-bundle-/eclipse/ Lê Văn Haïnh Nov2014 L p ếrờnh Anắroiắ Ph n 5: Maps  Location • Back: h y (destroy) activity th c hi n Khi nh n đ c tin nh n, activity nƠy đ c đ a nh ng TextView không đ c c p nh t ứ chuy n activity ho t đ ng ch đ background sang ch đ foreground, b n c n b sung s vi c nh sau: • Trong class MainActivity:  Chuy n đ ng kỦ BroadcastReceiver s ki n onCreate(), thay s ki n onResume()  Chuy n h y đ ng kỦ c a BroadcastReceiver s ki n onPause() sang h y đ ng ký s ki n onDestroy() i u nƠy đ m b o r ng c ho t đ ng nƠy lƠ ch đ n n, v n s có th l ng nghe broadcast intent • Trong class SMSReceiver: b sung vƠo s ki n onReceive() m t intent đ đ a activity c a lên foreground Intent đ c đ a vƠo nƠy c n y u t :  chuy n activity ch đ background sang foreground, b n c n ph i thi t l p c Intent.FLAG_ACTIVITY_NEW_TASK  S d ng ph ng th c startActivity() đ th c thi activity mang activity n n tr c (foreground) c a mƠn hình • Trong file AndroidManifest.xml: b sung thi t l p cho thu c tính launchMode c a ph n t singleTask N u b n không th c hi n thi t l p nƠy, có th có nhi u th hi n c a activity s đ c th c hi n nh n đ c tin nh n SMS BÀI TH C HÀNH App_17 (b sung l n 4)  Yêu c u V n project App_17 (đư b sung l n 3) Yêu c u b sung mư l nh đ ng d ng ch y ch đ background s đ c chuy n sang foreground Hình 4-13 Các tr ng thái c a ng d ng nh n tin nh n Hình 4-13- ng d ng ch y background vƠ tin nh n g i đ n (phía c a mƠn hình) Hình 4-13- Toast giúp hi n th thơng báo Hình 4-13- ng d ng đ c đ a sang ch đ foreground vƠ n i dung tin nh n hi n th TextView  Th c hi n B21 M file MainActivity.java B sung mư đ chuy n l nh đ ng kỦ vƠ h y đ ng kỦ vƠo s ki n cho phù h p v i yêu c u: package com.example.app_17; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.content.BroadcastReceiver; import android.content.Context; Lê Văn Haïnh Nov2014 316 L p ếrờnh Anắroiắ Ph n 5: Maps  Location import android.content.IntentFilter; import android.widget.TextView; public class MainActivity extends Activity { IntentFilter intentFilter; TextView tvSMS ; //t o m t đ i t ng BroadcastReceiver activity đ l ng nghe broadcast intents private BroadcastReceiver intentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //Khi m t broadcast intent đ c nh n, b n c p nh t tin nh n SMS TextView tvSMS = (TextView) findViewById(R.id.textView1) ; tvSMS.setText(intent.getExtras().getString("sms")) ; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // -intent to filter for SMS messages received intentFilter = new IntentFilter(); intentFilter.addAction("SMS_RECEIVED_ACTION") ; /*chuy n đăng k BroadcastReceiver s ki n onResume registerReceiver(intentReceiver, intentFilter) ; } //đăng ký BroadcastReceiver s @Override protected void onResume() { // -register the receiver sang onCreate */ ki n onResume onPause () c a activity // Do chuy n l nh đăng ký sang s ki n onCreate() nên c n che l i //registerReceiver(intentReceiver, intentFilter) ; super.onResume(); } @Override protected void onPause() { // -unregister the receiver // Do chuy n l nh h y đăng ký sang s // unregisterReceiver(intentReceiver) ; ki n onDestroy() nên c n che l i super.onPause(); } @Override protected void onDestroy() { // -unregister the receiver /* chuy n h y đăng ký BroadcastReceiver s * sang s ki n onDestroy() */ unregisterReceiver(intentReceiver) ; super.onPause(); } ki n onPause() public void UsingSMSManager(View v) { Intent intent = new Intent(this, UsingSmsManager.class); startActivity(intent); } public void UsingBuiltInIntent(View v) { Intent intent = new Intent(this, UsingBuiltInIntent.class); startActivity(intent); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) return true; { } Lê Văn Hạnh Nov2014 317 L p ếrờnh Anắroiắ Ph n 5: Maps  Location return super.onOptionsItemSelected(item); } } B22 M file SMSReceiver.java B sung mư l nh, khai báo m t intent đ đ a activity c a b n lên foreground: package com.example.app_17; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsMessage; import android.widget.Toast; public class SMSReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // -get the SMS message passed in Bundle bundle = intent.getExtras(); SmsMessage[] msgs = null; String str = ""; if (bundle != null) { // -retrieve the SMS message received Object[] pdus = (Object[]) bundle.get("pdus") ; msgs = new SmsMessage[pdus.length] ; for (int i=0; i B24 Ch y ng d ng S d ng DDMS g i tin nh n đ xem k t qu th c hi n 4.2.1.4 C nh ậốo Kh n ng g i vƠ nh n tin nh n SMS làm cho Android tr thƠnh m t n n t ng r t h p d n đ phát tri n ng d ng ph c t p Tuy nhiên s linh ho t nƠy kèm v i m t m c giá mƠ b n c ng c n nh c M t ng d ng d ng nh vơ t i có th g i tin nh n SMS d a Android Trojan SMS (tham kh o http://forum.vodafone.co.nz/topic/5719-android-sms-trojan-warning/) t x ng lƠ m t ng d ng nghe nh c, sau cƠi đ t, ng d ng s s d ng s n tho i c a b n đ g i tin nh n SMS đ n m t s d ch v nh m toán ti n phí b o hi m, tốn hóa đ n n tho i, ầ Ngoài ra, ng d ng c ng có th "đánh h i" (sniff) cho tin nh n SMS Ví d , d a k thu t b n v a bi t, b n có th d dƠng vi t m t ng d ng đ ki m tra t khóa nh t đ nh tin nh n SMS Khi m t tin nh n SMS có ch a t khóa mƠ b n tìm ki m, sau b n có th s d ng ng d ng Qu n lỦ đ a m (Location Manager) đ có đ c v trí đ a lỦ c a b n sau g i t a đ l i cho ng i g i tin nh n SMS Ng i g i sau có th d dƠng theo dõi v trí c a b n T t c nh ng công vi c nƠy có th đ c th c hi n d dƠng mƠ ng i dùng không h bi t! i u nói r ng, ng i dùng nên c g ng tránh cƠi đ t ng d ng Android đ n t ngu n không rõ ràng, ch ng h n nh t trang web không rõ, ng i l , V ph n mình, t o ng d ng, b n c n minh b ch t t c yêu c u c a ng d ng ng i dùng chu n b cƠi đ t nh : ng d ng có tính phí, ng d ng có quy n xác đ nh v trí đ a lỦ c a ng i dùng, ầ Hình 4-14 C n minh b ch v i ng i dùng v yêu c u c a ng d ng 4.2.1.5 Làm vi c v i folder ch a SMS 4.2.1.5.1 Danh sách folder ch a tin nh n SMS URI cho m i folder  All : content://sms/all  Inbox : content://sms/inbox  Sent : content://sms/sent Lê Văn Hạnh Nov2014 319 L p ếrờnh Anắroiắ Ph n 5: Maps  Location  Draft : content://sms/draft Outbox : content://sms/outbox Failed : content://sms/failed Queued : content://sms/queued  Undelivered : content://sms/undelivered  Conversations : content://sms/conversations    4.2.1.5.2 Các b c c n th c hi n ứ Tùy vƠo ch c n ng đ nh xơy d ng c a ng d ng đ c p quy n Read/Write SMS file AndroidManifest.xml ứ li t kê tin nh n: thông th ng tin nh n đ c dùng kèm v i control List, v y b n c n th c hi n: • i v i file layout c a activity, ch c n dùng TextView v i nhi m v gi ch cho m i m c (list item) có ListActivity • i v i file java: s ki n onCreate(), th c hi n l n l t l nh sau:  T o URI ch đ n tên folder c n s d ng (ví d content://sms/inbox)  Th c thi truy v n URI  L c n i dung tin nh n đ đ a vƠo ListAdapter ứ Cách th c hi n áp d ng cho t t c folder khác (nh Sent, Draft, ầ) vƠ ch c n thay đ i URI t ng ng ứ Android k t h p MMS vƠ SMS vƠ cho phép b n truy c p vƠo content providers đ truy c p c m t lúc b ng cách s d ng Authority lƠ mms-sms Nh v y, b n có th truy c p vƠo m t URI nh sau: content://mms-sms/conversations BÀI TH C HÀNH App_17 (b sung l n 5)  Yêu c u V n project App_17 (đư b sung l n 4) Yêu c u b sung button MainActivity Khi button nƠy đ c nh n ch n s hi n th layout ch a t t c tin nh n đư nh n đ c (inbox) Hình 4-15 B sung button “Open SMS Inbox” activity Hình 4-16 AVD v i tin nh n nh n  Th c hi n B25 M file activity.xml B sung mư đ có button th 3: B26 T o m i file layout sms_inbox.xml đ hi n th n i dung tin nh n theo yêu c u c a đ bƠi Nh đư gi i thi u trên, TextView có layout nƠy có nhi m v gi ch cho m i m c (list item) có ListActivity: B27 T o m i file SMS_Inbox.java vƠ b sung mư l nh đ hi n th n i dung tin nh n theo yêu c u c a đ bƠi package com.example.app_17; import android.app.ListActivity; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.widget.ListAdapter; import android.widget.SimpleCursorAdapter; public class SMS_Inbox extends ListActivity { private ListAdapter adapter; private static final Uri SMS_INBOX = Uri.parse("content://sms/inbox"); @SuppressWarnings("deprecation") @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); Cursor cs = getContentResolver().query(SMS_INBOX, null, null, null, null); startManagingCursor(cs); String[] columns = new String[] { "body" }; int[] names = new int[] { R.id.row }; adapter = new SimpleCursorAdapter(this, R.layout.sms_inbox, cs, columns, names); setListAdapter(adapter); } } B28 B sung mư l nh file MainActivity.java đ ng i dùng nh n ch n button v a thêm s m danh sách ch a n i dung tin nh n: package com.example.app_17; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; Leâ Văn Hạnh Nov2014 321 L p ếrờnh Anắroiắ Ph n 5: Maps  Location import android.view.MenuItem; import android.view.View; import android.content.BroadcastReceiver; import android.content.Context; import android.content.IntentFilter; import android.widget.TextView; public class MainActivity extends Activity { IntentFilter intentFilter; TextView tvSMS ; //t o m t đ i t ng BroadcastReceiver activity đ l ng nghe broadcast intents private BroadcastReceiver intentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // -display the SMS received in the TextView //Khi m t broadcast intent đ c nh n, b n c p nh t tin nh n SMS TextView tvSMS = (TextView) findViewById(R.id.textView1) ; tvSMS.setText(intent.getExtras().getString("sms")) ; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // -intent to filter for SMS messages received intentFilter = new IntentFilter(); intentFilter.addAction("SMS_RECEIVED_ACTION") ; /* chuy n đăng ký BroadcastReceiver s ki n onResume() sang s ki n onCreate() */ registerReceiver(intentReceiver, intentFilter) ; } //đăng ký BroadcastReceiver s ki n onResume onPause () c a activity @Override protected void onResume() { // -register the receiver-// Do chuy n l nh đăng ký sang s ki n onCreate() nên c n che l i //registerReceiver(intentReceiver, intentFilter) ; super.onResume(); } @Override protected void onPause() { // -unregister the receiver-// Do chuy n l nh h y đăng ký sang s ki n onDestroy() nên c n che l i // unregisterReceiver(intentReceiver) ; super.onPause(); } @Override protected void onDestroy() { // -unregister the receiver /* chuy n h y đăng ký BroadcastReceiver s ki n onPause()sang s ki n onDestroy() */ unregisterReceiver(intentReceiver) ; super.onPause(); } public void UsingSMSManager(View v) { Intent intent = new Intent(this, UsingSmsManager.class); startActivity(intent); } public void UsingBuiltInIntent(View v) { Intent intent = new Intent(this, UsingBuiltInIntent.class); startActivity(intent); } public void OpenSMSInbox(View v) { Intent intent = new Intent(this, SMS_Inbox.class); startActivity(intent); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); Lê Văn Hạnh Nov2014 322 L p ếrờnh Anắroiắ Ph n 5: Maps  Location return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } B29 B sung quy n đ c (vƠ ghi n u ng d ng có cho phép) đ i v i tin nh n đư nh n vƠ kỦ activity SMS_Inbox B30 B31 Tr c ch y ng d ng, b n c n s d ng DDMS đ nh n s tin đ n AVD Ch y ng d ng đ xem k t qu 4.3 Sending e-mail Gi ng nh tin nh n SMS, Android c ng h tr e-mail Các ng d ng Gmail/Email Android cho phép b n c u hình m t tƠi kho n e -mail s d ng POP3 ho c IMAP Bên c nh vi c g i vƠ nh n e-mail b ng cách s d ng ng d ng Gmail/Email, b n c ng có th l p trình t bên ng d ng Android c a b n đ g i tin e -mail t Th c ra, Android không h tr g i email tr c ti p t ng Intent đ kh i đ ng c a s Activity dùng g i mail Lê Văn Hạnh Nov2014 ng d ng nên ta s ph i t o m t đ i 323 L p ếrờnh Anắroiắ BÀI TH C HÀNH Ph n 5: Maps  Location App_17 (b sung l n 6)  Yêu c u V n project App_17 (đư b sung l n 5) Yêu c u b sung button MainActivity Khi button nƠy đ c nh n ch n s m activity cho phép g i tin nh n Sau ng i dùng n đ y đ thông tin vƠ nh n button Send Email, ng d ng s th c hi n vi c chuy n n i dung email đ n đ a ch đư có Hình 4-17 B sung button “Send Email” activity Hình 4-18 Activity Send Email  Th c hi n B32 T o m i file layout send_email.xml v i n i dung nh sau: Lê Văn Haïnh Nov2014 324 L p ếrờnh Anắroiắ Ph n 5: Maps  Location B33 T o m i file src\com\example\SendEmail.java v i n i dung nh sau: package com.example.app_17; import android.app.Activity; import android.os.Bundle; import android.content.Intent; import android.net.Uri; import android.view.View; import android.widget.Button; import android.widget.EditText; public class SendEmail extends Activity { Button btnSendEmail; EditText etTo, etCC, etSubject, etBody; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.send_email) ; etTo = (EditText) findViewById(R.id.etTo) ; etCC = (EditText) findViewById(R.id.etCC) ; Leâ Văn Hạnh Nov2014 325 L p ếrờnh Anắroiắ Ph n 5: Maps  Location etSubject = (EditText) findViewById(R.id.etSubject) ; etBody = (EditText) findViewById(R.id.etBody) ; btnSendEmail = (Button) findViewById(R.id.btnSendEmail) ; } public void Send(View v) { String strTo=etTo.getText().toString(); String strCC=etTo.getText().toString(); String[] to =strTo.trim().split(";"); String[] cc =strCC.trim().split(";"); String subject = etSubject.getText().toString(); String body = etBody.getText().toString(); sendEmail(to, cc, subject, body) ; } // -sends an SMS message to another device-private void sendEmail(String[] emailAddresses, String[] carbonCopies, String subject, String message) { Intent emailIntent = new Intent(Intent.ACTION_SEND) ; emailIntent.setData(Uri.parse("mailto:")) ; String[] to = emailAddresses; String[] cc = carbonCopies; emailIntent.putExtra(Intent.EXTRA_EMAIL, to); emailIntent.putExtra(Intent.EXTRA_CC, cc); emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject); emailIntent.putExtra(Intent.EXTRA_TEXT, message); emailIntent.setType("message/rfc822") ; startActivity(Intent.createChooser(emailIntent, "Email")) ; } } B34 B sung vƠo file src\com\example\activity_main.xml mư l nh t o button cho button Send Email Trong button m i thêm có s d ng thu c tính android:onClick đ g i hƠm x lỦ tên Send_Email : Lê Văn Hạnh Nov2014 326 L p ếrờnh Anắroiắ Ph n 5: Maps  Location B35 B sung hƠm x lỦ s ki n cho button v a t o file layout (btnSendEmail): package com.example.app_17; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.content.BroadcastReceiver; import android.content.Context; import android.content.IntentFilter; import android.widget.TextView; public class MainActivity extends Activity { IntentFilter intentFilter; TextView tvSMS ; //t o m t đ i t ng BroadcastReceiver activity đ l ng nghe broadcast intents private BroadcastReceiver intentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //Khi m t broadcast intent đ c nh n, b n c p nh t tin nh n SMS TextView tvSMS = (TextView) findViewById(R.id.textView1) ; tvSMS.setText(intent.getExtras().getString("sms")) ; } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // -intent to filter for SMS messages received intentFilter = new IntentFilter(); intentFilter.addAction("SMS_RECEIVED_ACTION") ; /* chuy n đăng ký BroadcastReceiver s ki n onResume()sang s ki n onCreate() */ registerReceiver(intentReceiver, intentFilter) ; } //đăng ký BroadcastReceiver s ki n onResume onPause () c a activity @Override protected void onResume() { // -register the receiver-// Do chuy n l nh đăng ký sang s ki n onCreate() nên c n che l i //registerReceiver(intentReceiver, intentFilter) ; super.onResume(); } @Override protected void onPause() { // -unregister the receiver-// Do chuy n l nh h y đăng ký sang s ki n onDestroy() nên c n che l i // unregisterReceiver(intentReceiver) ; super.onPause(); } @Override protected void onDestroy() { // -unregister the receiver /* chuy n h y đăng ký BroadcastReceiver s ki n onPause() * sang s ki n onDestroy() */ unregisterReceiver(intentReceiver) ; super.onPause(); } public void UsingSMSManager(View v) { Intent intent = new Intent(this, UsingSmsManager.class); startActivity(intent); } public void UsingBuiltInIntent(View v) { Intent intent = new Intent(this, UsingBuiltInIntent.class); startActivity(intent); } public void OpenSMSInbox(View v) Lê Văn Haïnh Nov2014 327 L p ếrờnh Anắroiắ { Ph n 5: Maps  Location Intent intent = new Intent(this, SMS_Inbox.class); startActivity(intent); } public void Send_Email(View v) { Intent intent = new Intent(this, SendEmail.class); startActivity(intent); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } B36 B sung khai báo activity m i vƠ quy n g i SMS file AndroidManifest.xml Do l n b sung tr c b n đư b sung quy n đ c g i email r i () nên đơy ch th c hi n b sung activity m i mƠ thôi: Leâ Văn Hạnh Nov2014 328 L p ếrờnh Anắroiắ B37 Ph n 5: Maps  Location Ch y ng d ng vƠ th c hi n g i email Do m i th c hi n g i email l n đ u nên sau nh n button Send Email, Android s đ a b n đ n mƠn hình thi t l p Account Hình 4-19 Các tr ng thái c a ng d ng g i email l n đ u Hình 4-19- Nh p đ a ch n i dung c n g i r i nh n button Send Email Hình 4-19- Yêu c u b sung thông tin v đ a ch password c a ng i g i đ ch ng th c Hình 4-19- Quá trình ch ng th c Hình 4-19- Ch ng th c thành cơng u c u s nhi m ý t ng i dùng Nh n Next đ ti p t c Hình 4-19- Quá trình ghi nh n l i nhi m ý Hình 4-19- Xác nh n l i email c a ng i g i )đ g i Hình 4-19- Màn hình hi n th tồn b thơng tin v email s g i Nh n icon send ( Hình 4-19- Sau g i tr v hình ban đ u c a activity SendEmail (chính Hình 4-19-) T nh ng l n g i email sau, b n ch ph i chuy n qua l i gi a mƠn hình sau: Lê Văn Hạnh Nov2014 329 L p ếrờnh Anắroiắ Ph n 5: Maps  Location Hình 4-20 Các tr ng thái c a ng d ng g i email nh ng l n sau 4.4 BÀI T P T NG H P CH 4.4.1 Th c hi n t cho c ng d ng Lê Văn Hạnh NG ng t nh yêu c u c a bƠi th c hƠnh App_16 nh ng ch s d ng chung activity Nov2014 330

Ngày đăng: 07/08/2020, 13:04

Xem thêm:

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w