Lập trình Android tiếng Việt - Chapter 1: Introduction

IntentFilter định nghĩa quan hệ giữa Intent và ứng dụng.

IntentFilter có thể quy định cụ thể đến phần data, hoặc phần action của intent, hoặc cả hai.

IntentFilter còn chứa một trường được gọi là một category. Category giúp phân loại các action.

Ví dụ, category có tên CATEGORY_LAUNCHER

 chỉ dẫn Android rằng Activity chứa IntentFilter này cần được nhìn thấy tại home screen.

 

pptx84 trang | Chuyên mục: Android | Chia sẻ: dkS00TYs | Lượt xem: 1876 | Lượt tải: 5download
Tóm tắt nội dung Lập trình Android tiếng Việt - Chapter 1: Introduction, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
tive, and its hosting process is only as important as any other application components that are running in it. This means that for longer-running operations you will often use a Service in conjunction with a BroadcastReceiver to keep the containing process active for the entire time of your operation. 57 Android Broadcast Receiver Broadcast Receiver Example (1/5). Intercept arriving SMS package matos.broadcastreceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.util.Log; import android.app.Activity; import android.os.Bundle; public class MySMSMailBox extends Activity { // intercepts reception of new text-messages 58 Android Broadcast Receiver Broadcast Receiver Example (2/5). Intercept arriving SMS @Override public void onCreate(Bundle savedInstanceState) { 	super.onCreate(savedInstanceState); 	setContentView(R.layout.main); 	// define instance of local broadcast receiver 	MySMSMailBoxReceiver mySmsReceiver = new MySMSMailBoxReceiver(); 	// receiver's filter will accept event: ...SMS_RECEIVED 	IntentFilter filter = new IntentFilter( 	 "android.provider.Telephony.SMS_RECEIVED"); 	// tell Android OS this receiver is ready to go 	registerReceiver(mySmsReceiver, filter); } 59 Android Broadcast Receiver Broadcast Receiver Example (3/5). Intercept arriving SMS 	// this is the custom made broadcast receiver. Its onReceive method 	// is fired when the filter matches the SMS_RECEIVED event 	public class MySMSMailBoxReceiver extends BroadcastReceiver { 	public static final String tag = ">>"; 	@Override 	public void onReceive(Context context, Intent intent) { 	 Log.i(tag, "onReceive"); 	 // checking global event signaling arrival of text-message 	 if (intent.getAction().equals( 	"android.provider.Telephony.SMS_RECEIVED")) { 	 	Log.i(tag, "Found our SMS Event!"); 	 	// you have intercepted the SMS 	 	// do something interesting with it. Bye! 	 } 	}// onReceive 	} // BroadcastReceiver } 60 Android Broadcast Receiver Broadcast Receiver Example (4/5). Intercept arriving SMS 61 Android Broadcast Receiver Broadcast Receiver Example (5/5). Intercept arriving SMS 62 Android Content Provider Content provider ghi (store) và đọc (retrieve) dữ liệu và cho phép tất cả các ứng dụng truy nhập dữ liệu. Đó là cách duy nhất để các ứng dụng Android dùng chung dữ liệu. Không có nơi lưu trữ dữ liệu chung mà tất cả các package có thể truy nhập. Android có sẵn các content provider cho các kiểu dữ liệu thông dụng (audio, video, images, personal contact information, v.v..). 63 Android Content Provider ContentProvider là tầng dữ liệu cung cấp trừu tượng dữ liệu cho các client và tập trung các thủ tục lưu trữ và đọc dữ liệu tại một chỗ. Một ContentProvider có thể cung cấp dữ liệu cho một Activity hay Service trong cùng không gian ứng dụng, cũng như các Activity hay Service nằm trong các ứng dụng khác. Một ContentProvider có thể dùng cơ chế lưu trữ dữ liệu bất kì mà Android platform hỗ trợ, trong đó có file, cơ sở dữ liệu SQLite, hay kể cả một bảng băm nằm trong bộ nhớ nếu không đòi hỏi dữ liệu phải được duy trì. 64 Android Content Provider 65 Android Content Provider The data model Content providers expose their data as a simple table on a database model, where each row is a record and each column is data of a particular type and meaning. For example, information about people and their phone numbers might be exposed as follows: 66 Android Content Provider URIs Each content provider exposes a public URI that uniquely identifies its data set. A content provider that controls multiple data sets (multiple tables) exposes a separate URI for each one. All URIs for providers begin with the string "content://". Android defines CONTENT_URI constants for all the providers that come with the platform. For example android.provider.Contacts.Phones.CONTENT_URI android.provider.Contacts.Photos.CONTENT_URI android.provider.CallLog.Calls.CONTENT_URI android.provider.Calendar.CONTENT_URI The ContentResolver method takes an URI as its first argument. It's what identifies which provider the ContentResolver should talk to and which table of the provider is being targeted. 67 Android Content Provider Querying a Content Provider You need three pieces of information to query a content provider: The URI that identifies the provider The names of the data fields you want to receive The data types for those fields If you're querying a particular record, you also need the ID for that record. A query returns a Cursor object that can move from record to record and column to column to read the contents of each field. It has specialized methods for reading each type of data. 68 Android Content Provider Example: Posting a query to the Contact list (1/2) package matos.cis493; import android.app.Activity; import android.net.Uri; import android.os.Bundle; import android.widget.EditText; import android.widget.Toast; import android.provider.Contacts.People; import android.content.ContentUris; import android.database.Cursor; public class AndDemo1 extends Activity { /** queries contact list */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Use the ContentUris method to produce the base URI for the contact with _ID == 23. Uri myPerson1 = ContentUris.withAppendedId(People.CONTENT_URI, 23); // use the "people" content provider to explore all your contacts Uri myPerson2 = Uri.parse("content://contacts/people"); // Then query for this specific record using method: managedQuery // args: (Uri uri, String[] projection, String selection, // String[] selectionArgs, String sortOrder) Cursor cur = managedQuery(myPerson2, null, null, null, null); // do something with the cursor here } } 69 Android Content Provider Example: Posting a query to the Contact list (2/2) 70 Android Manifest xml File Every application must have an AndroidManifest.xml file 	(with precisely that name) in its root directory. The manifest presents essential information about the application to the Android system, information the system must have before it can run any of the application's code. 71 Android Manifest xml File 72 These are the only legal elements; you cannot add your own elements or attributes. Android Manifest xml File Các tác dụng quan trọng của manifest: Đặt tên cho Java package của ứng dụng. Tên package đóng vai trò định danh cho ứng dụng. Mô tả các component của ứng dụng – các activity, service, broadcast receiver, và content provider của ứng dụng. Gọi tên các class cài đặt các component và khai báo năng lực của chúng (capability) (ví dụ, chúng có thể xử lý các Intent nào). Các khai báo này cho phép hệ thống Android biết có những component nào và có thể chạy chúng trong những điều kiện nào. Quyết định các tiến trình nào sẽ chứa các component nào của ứng dụng. Khai báo các permission mà ứng dụng phải được cấp để có thể truy nhập các phần được bảo vệ của API và tương tác với các ứng dụng khác. Khai báo các permission mà các ứng dụng khác phải có để có thể tương tác với các component của ứng dụng hiện hành. Liệt kê các lớp Instrumentation cung cấp các thông tin khác khi ứng dụng đang chạy. Các khai báo này chỉ có ở manifest trong khi ứng đụng đang được phát triển và test; chúng sẽ bị xóa đi trước khi ứng dụng được phát hành. Khai báo minimum level của Android API mà ứng dụng yêu cầu. Liệt kê các library mà ứng dụng cần được link với. 73 Android Manifest xml File 74 Example. Currency converter Implementing a simple currency converter: USD – Euro – Colon (CR) Note. Naive implementation using the rates 1 Costa Rican Colon = 0.001736 U.S. dollars 1 Euro = 1.39900 U.S. dollars 75 Example. Currency converter 76 Example. Currency converter 77 package matos.currencyconvereter; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Currency1 extends Activity { // naive currency converter from USD to Euros & Colones final double EURO2USD = 1.399; final double COLON2USD = 0.001736; // GUI widgets Button btnConvert; Button btnClear; EditText txtUSDollars; EditText txtEuros; EditText txtColones; Example. Currency converter 78 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // bind local controls to GUI widgets txtUSDollars = (EditText)findViewById(R.id.txtUSDollars); txtUSDollars.setHint("Enter US dollars"); txtEuros = (EditText)findViewById(R.id.txtEuros); txtColones = (EditText)findViewById(R.id.txtColones); 	 	 // attach click behavior to buttons btnClear = (Button)findViewById(R.id.btnClear); btnClear.setOnClickListener(new OnClickListener() { // clear the text boxes 	@Override 	public void onClick(View v) { 	txtColones.setText(""); 	txtEuros.setText(""); 	txtUSDollars.setText(""); 	} }); Example. Currency converter 79 	 // do the conversion from USD to Euros and Colones btnConvert = (Button) findViewById(R.id.btnConvert); btnConvert.setOnClickListener(new OnClickListener() { 	 @Override 	 public void onClick(View v) { 	 try { 	 	String usdStr = txtUSDollars.getText().toString(); 	 	double usd = Double.parseDouble( usdStr ); 	 	String euros = String.valueOf( usd / EURO2USD ); 	 	String colones = String.valueOf( usd / COLON2USD ); 	 	txtEuros.setText(euros); 	 	txtColones.setText(colones); 	 } catch (Exception e) { 	 	Toast.makeText(v.getContext(), "Invalid data - try again" , 	 Toast.LENGTH_SHORT).show(); 	 } } });// setOnClick... }// onCreate }// class Example. Currency converter 80 	 Example. Currency converter 81 Resource: res/ layout/main.xml (1/2) Example. Currency converter 82 Resource: res/ layout/main.xml (2/2) Example. Currency converter 83 Additional Resources Google Developer Conference San Francisco – 2009 Web page:  84 Questions ???? 85 

File đính kèm:

  • pptxLập trình Android tiếng Việt - Chapter 1 Introduction.pptx