Giáo trình Lập trình di động trên môi trường Android - Lê Văn Hạnh

Tóm tắt Giáo trình Lập trình di động trên môi trường Android - Lê Văn Hạnh: ...xecute(): Sau khi tiến trình kết thúc thì hàm này sẽ tự động xảy ra. Các lệnh truy cập đến giao diện hoặc lấy kết quả trả về sau khi thực hiện tiến trình thường đặt trong phương thức này. Trong 4 hàm trên thì hàm doInBackground() bắt buộc phải tồn tại, còn các hàm khác có thể bỏ qua. Trong...iệu chỉnh nội dung file src\com.example.app_12\MainActivity.java để có như sau: package com.example.app_12; import android.app.ActionBar; import android.app.Activity; import android.app.FragmentTransaction; import android.app.ActionBar.Tab; import android.os.Bundle; import android.view...g lúc bạn cần cân nhắc khi sử dụng indexes trong các trường hợp sau: − Kích thước table nhỏ. − Table thường xuyên thực hiện hàng loạt thao tác cập nhật hoặc chèn dữ liệu. − Cột dự định sử dụng làm indexes có chứa một số lượng lớn các giá trị NULL. − Lập chỉ mục trên những cột thường xuyên ...

pdf334 trang | Chia sẻ: havih72 | Lượt xem: 279 | Lượt tải: 0download
Nội dung tài liệu Giáo trình Lập trình di động trên môi trường Android - Lê Văn Hạnh, để tải tài liệu về máy bạn click vào nút DOWNLOAD ở trên
ào ListAdapter. 
− Cách thực hiện trên áp dụng cho tất cả cá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ả 2 cùng 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 trong project App_17 (đã bổ sung lần 4). Yêu cầu bổ sung 1 button trên MainActivity. 
Khi button này được nhấn chọn sẽ hiển thị layout chứa tất cả các tin nhắn đã nhận được (inbox). 
Hình 4-15 Bổ sung button “Open SMS Inbox” trên activity chính Hình 4-16 AVD với 3 tin nhắn đã nhận 
 Thực hiện 
B25. Mở file activity.xml. Bổ sung mã để cĩ button thứ 3: 
<LinearLayout xmlns:android="" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:orientation="vertical" 
 android:background="#FFFFFF"> 
 <Button android:id="@+id/btnSMSManager" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:onClick="UsingSMSManager" 
Lập trình Android Phần 5: Maps  Location 
Lê Văn Hạnh Nov2014 321 
 android:text="Using SMSManager to send SMS"/> 
 <Button android:id="@+id/btnBuiltInIntent" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:onClick="UsingBuiltInIntent" 
 android:text="Using Built In Intent to send SMS"/> 
 <Button android:id="@+id/btnOpenSMSInbox" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:onClick="OpenSMSInbox" 
 android:text="Open SMS Inbox"/> 
 <TextView android:id="@+id/textView1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="SMS" 
 android:textSize="18sp" /> 
B26. Tạo mới file layout sms_inbox.xml để hiển thị nội dung các tin nhắn theo yêu cầu của đề bài. 
Như đã giới thiệu ở trên, TextView cĩ trong layout này cĩ nhiệm vụ giữ chỗ cho mỗi mục 
(list item) cĩ trong ListActivity: 
<LinearLayout xmlns:android="" 
 android:orientation="vertical" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" > 
 <TextView android:id="@+id/row" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent"/> 
B27. Tạo mới file SMS_Inbox.java và bổ sung mã lệnh để hiển thị nội dung các 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 trong file MainActivity.java để khi người dùng nhấn chọn button vừa thêm 
sẽ mở ra danh sách chứa nội dung các tin nhắn: 
package com.example.app_17; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
Lập trình Android Phần 5: Maps  Location 
Lê Văn Hạnh Nov2014 322 
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 ra một đối tượng BroadcastReceiver trong 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 trong 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 trong sự kiện onResume() sang sự kiện onCreate() */ 
 registerReceiver(intentReceiver, intentFilter) ; 
 } 
 //đăng ký BroadcastReceiver trong 2 sự kiện onResume và onPause () của activity 
 @Override 
 protected void onResume() 
 { //---register the receiver-- 
 // Do đã chuyển lệnh đăng ký này 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ý này 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 trong 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ập trình Android Phần 5: Maps  Location 
Lê Văn Hạnh Nov2014 323 
 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à đang ký 
activity SMS_Inbox. 
<manifest xmlns:android="" 
 package="com.example.app_17" 
 android:versionCode="1" 
 android:versionName="1.0" > 
<uses-sdk android:minSdkVersion="12" 
android:targetSdkVersion="21" /> 
 <application android:allowBackup="true" 
 android:icon="@drawable/ic_launcher" 
 android:label="@string/app_name" 
 android:launchMode="singleTask" > 
<activity android:name=".MainActivity" 
 android:label="@string/app_name" > 
<activity android:name=".UsingBuiltInIntent" 
android:label="Using Built-in Intent to send SMS" > 
<activity android:name=".UsingSmsManager" 
android:label="Using SmsManager to send SMS" > 
 <activity android:name=".SMS_Inbox" 
android:label="Open SMS Inbox" > 
B30. Trước khi chạy ứng dụng, bạn cần sử dụng DDMS để nhắn 1 số tin đến AVD 
B31. 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 trên 
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 trong ứng 
dụng Android của bạn để gửi tin e -mail. 
Thực ra, do Android khơng hỗ trợ gởi email trực tiếp từ ứng dụng nên ta sẽ phải tạo một đối 
tượng Intent để khởi động cửa sổ Activity dùng gởi mail 
Lập trình Android Phần 5: Maps  Location 
Lê Văn Hạnh Nov2014 324 
BÀI THỰC HÀNH App_17 (bổ sung lần 6) 
 Yêu cầu 
Vẫn trong project App_17 (đã bổ sung lần 5). Yêu cầu bổ sung 1 button trên MainActivity. 
Khi button này được nhấn chọn sẽ mở activity cho phép gởi tin nhắn. 
Sau khi người dùng điề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” trên activity chính 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: 
<LinearLayout xmlns:android="" 
 android:orientation="vertical" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:background="#FFFFFF"> 
 <TextView android:id="@+id/textView1" 
android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
android:text="SEND EMAIL" 
 android:layout_gravity="center" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:textColor="#888888" 
 android:textStyle="bold" /> 
 <LinearLayout android:orientation="horizontal" 
android:layout_width="fill_parent" 
 android:layout_height="wrap_content"> 
 <TextView android:id="@+id/tvTo" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="To (delimit by ';'):" 
 android:textAppearance="?android:attr/textAppearanceMedium" 
 android:textColor="#000000"/> 
 <EditText android:id="@+id/etTo" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:ems="10" 
 android:textColor="#000000"> 
Lập trình Android Phần 5: Maps  Location 
Lê Văn Hạnh Nov2014 325 
 <LinearLayout android:orientation="horizontal" 
android:layout_width="fill_parent" 
 android:layout_height="wrap_content"> 
 <TextView android:id="@+id/tvCC" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="CC (delimit by ';'):" 
 android:textAppearance="?android:attr/textAppearanceMedium" 
 android:textColor="#000000"/> 
 <EditText android:id="@+id/etCC" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:ems="10" 
 android:textColor="#000000"/> 
 <LinearLayout android:orientation="horizontal" 
android:layout_width="fill_parent" 
 android:layout_height="wrap_content"> 
<TextView android:id="@+id/tvSubject" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Subject:" 
 android:textAppearance="?android:attr/textAppearanceMedium" 
 android:textColor="#000000"/> 
 <EditText android:id="@+id/etSubject" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:ems="10" 
 android:textColor="#000000"/> 
<EditText android:id="@+id/etBody" 
 android:layout_width="match_parent" 
 android:layout_height="150dp" 
 android:ems="10" 
 android:textColor="#000000"/> 
 <Button android:id="@+id/btnSendEmail" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:text="Send Email" 
 android:onClick="Send"/> 
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) ; 
Lập trình Android Phần 5: Maps  Location 
Lê Văn Hạnh Nov2014 326 
 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 : 
<LinearLayout xmlns:android="" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:orientation="vertical" 
 android:background="#FFFFFF"> 
 <Button android:id="@+id/btnSMSManager" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:onClick="UsingSMSManager" 
 android:text="Using SMSManager to send SMS"/> 
 <Button android:id="@+id/btnBuiltInIntent" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:onClick="UsingBuiltInIntent" 
 android:text="Using Built In Intent to send SMS"/> 
 <Button android:id="@+id/btnOpenSMSInbox" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:onClick="OpenSMSInbox" 
 android:text="Open SMS Inbox"/> 
 <Button android:id="@+id/btnSendEmail" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:onClick="Send_Email" 
 android:text="Send Email"/> 
 <TextView android:id="@+id/textView1" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="SMS" 
 android:textSize="18sp" /> 
Lập trình Android Phần 5: Maps  Location 
Lê Văn Hạnh Nov2014 327 
B35. Bổ sung hàm xử lý sự kiện cho button vừa tạo trong 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 ra một đối tượng BroadcastReceiver trong 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 trong 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 trong sự kiện onResume()sang sự kiện onCreate() */ 
 registerReceiver(intentReceiver, intentFilter) ; 
 } 
 //đăng ký BroadcastReceiver trong 2 sự kiện onResume và onPause () của activity 
 @Override 
 protected void onResume() 
 { //---register the receiver-- 
 // Do đã chuyển lệnh đăng ký này 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ý này 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 trong 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ập trình Android Phần 5: Maps  Location 
Lê Văn Hạnh Nov2014 328 
 { 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 trong file AndroidManifest.xml. Do các 
lần bổ sung trước bạn đã bổ sung quyền được gởi email rồi (<uses-permission 
android:name="android.permission.SEND_SMS" />) nên ở đây chỉ thực hiện bổ sung activity 
mới mà thơi: 
<manifest xmlns:android="" 
 package="com.example.app_17" 
 android:versionCode="1" 
 android:versionName="1.0" > 
 <uses-sdk android:minSdkVersion="12" 
 android:targetSdkVersion="21" /> 
 <application android:allowBackup="true" 
android:icon="@drawable/ic_launcher" 
android:label="@string/app_name" 
android:launchMode="singleTask" > 
<activity android:name=".MainActivity" 
android:label="@string/app_name" > 
<activity android:name=".UsingBuiltInIntent" 
 android:label="Using Built-in Intent to send SMS" > 
<activity android:name=".UsingSmsManager" 
 android:label="Using SmsManager to send SMS" > 
<activity android:name=".SMS_Inbox" 
 android:label="Open SMS Inbox" > 
 <activity android:name=".SendEmail" 
 android:label="Send Email" > 
Lập trình Android Phần 5: Maps  Location 
Lê Văn Hạnh Nov2014 329 
B37. 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 khi 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 khi gởi email lần đầu 
Hình 4-19- Nhập địa chỉ và 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ỉ và 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 và yêu cầu 1 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 các nhiệm ý 
Hình 4-19- Xác nhận lại email của ngườ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 ( ) để gởi 
Hình 4-19- Sau khi gởi trở về màn hình ban đầu của activity SendEmail (chính là Hình 4-19-) 
Từ những lần gởi email sau, bạn chỉ cịn phải chuyển qua lại giữa 2 màn hình sau: 
Lập trình Android Phần 5: Maps  Location 
Lê Văn Hạnh Nov2014 330 
Hình 4-20 Các trạng thái của ứng dụng khi gởi email những lần sau 
4.4. BÀI TẬP TỔNG HỢP CHƯƠNG 4 
4.4.1. 
Thực hiện tương tự như yêu cầu của bài thực hành App_16 nhưng chỉ sử dụng chung 1 activity 
cho cả ứng dụng. 

File đính kèm:

  • pdfgiao_trinh_lap_trinh_di_dong_tren_moi_truong_android_le_van.pdf