Lập trình Android tiếng Việt - Chapter 19: Android Intent Filters

So sánh: Requesting Actions Using HTTP and Android

 

Giao thức HTTP1 dùng các cặp <Action, resource> để thực hiện các nhiệm vụ.

 

Một số action HTTP là các thao tác ít nhiều quen thuộc: POST, GET, PUT, DELETE, CONNECT, HEAD, OPTIONS.

 

Android dùng một cơ chế tương tự HTTP để kích hoạt các nhiệm vụ.

 

INTENT là tên của trừu tượng hóa mà Android dùng để yêu cầu các action.

 

Khác với HTTP, một INTENT của Android có thể được giải quyết bằng vài cách (ví dụ, ta có thể có vài ứng dụng SMS muốn xử lý một tin nhắn đến).

 

 

pptx17 trang | Chuyên mục: Android | Chia sẻ: dkS00TYs | Lượt xem: 1954 | Lượt tải: 4download
Tóm tắt nội dung Lập trình Android tiếng Việt - Chapter 19: Android Intent Filters, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 3/14/2012 ‹#› Android Intent Filters 19 Notes are based on: Android Developers  2 2 2 19. Android – Intent Filters Intent Filters 2 So sánh: Requesting Actions Using HTTP and Android Giao thức HTTP1 dùng các cặp để thực hiện các nhiệm vụ. Một số action HTTP là các thao tác ít nhiều quen thuộc: POST, GET, PUT, DELETE, CONNECT, HEAD, OPTIONS. Android dùng một cơ chế tương tự HTTP để kích hoạt các nhiệm vụ. INTENT là tên của trừu tượng hóa mà Android dùng để yêu cầu các action. Khác với HTTP, một INTENT của Android có thể được giải quyết bằng vài cách (ví dụ, ta có thể có vài ứng dụng SMS muốn xử lý một tin nhắn đến). ____ Source: Hypertext Transfer Protocol -- HTTP/1.1 (1999).  Source:  3 3 3 19. Android – Intent Filters Intent Filters 3 INTENTS An intent is an abstract description of an operation to be performed. Its most significant use is in the launching of activities. The primary pieces of information in an intent are: action & data. Source:     ACTION DATA Misc The general action to be performed, such as: ACTION_EDIT, ACTION_VIEW, ACTION_MAIN, ACTION_LAUNCHER etc. The data to operate on, such as a person record in the contacts database, expressed as a URI. I am good for editing a document I am good for viewing a document I am the first exec. Activ. of Application Put me on the phone’s Menu_Pad 4 4 4 19. Android – Intent Filters Intent Filters 4 Parts of a Typical Intent    ACTION DATA MISC Standard URI Category CATEGORY_DEFAULT CATEGORY_BROWSABLE CATEGORY_TAB CATEGORY_ALTERNATIVE CATEGORY_SELECTED_ALTERNATIVE CATEGORY_LAUNCHER CATEGORY_INFO CATEGORY_HOME CATEGORY_PREFERENCE CATEGORY_TEST ACTION_MAIN ACTION_VIEW ACTION_ATTACH_DATA ACTION_EDIT ACTION_PICK ACTION_CHOOSER ACTION_GET_CONTENT ACTION_DIAL ACTION_CALL ACTION_SEND ACTION_SENDTO ACTION_ANSWER ACTION_INSERT ACTION_DELETE ACTION_RUN ACTION_SYNC ACTION_PICK_ACTIVITY ACTION_SEARCH ACTION_WEB_SEARCH ACTION_FACTORY_TEST ACTION_TIME_TICK ACTION_TIME_CHANGED ACTION_TIMEZONE_CHANGED ACTION_BOOT_COMPLETED ACTION_PACKAGE_ADDED ACTION_PACKAGE_CHANGED ACTION_PACKAGE_REMOVED ACTION_PACKAGE_RESTARTED ACTION_PACKAGE_DATA_CLEARED ACTION_UID_REMOVED ACTION_BATTERY_CHANGED ACTION_POWER_CONNECTED ACTION_POWER_DISCONNECTED ACTION_SHUTDOWN CONTENTS such as: content://contacts/ content://contacts/1 SCHEME such as: tel:123  mailto://aa@bbb.ccc ftp://aaa.bbb.ccc . . . pop:// smtp:// ssl:// MIME Explicit type (a MIME type) of the intent data. Component Explicit name of a component class to use for the intent. Extras putExtra(String, Bundle) Flags 5 5 5 19. Android – Intent Filters Intent Filters 5 Aside: MIME “ … This set of documents, collectively called the Multipurpose Internet Mail Extensions, or MIME, redefines the format of messages to allow for textual message bodies in character sets other than 	US-ASCII, an extensible set of different formats for non-textual message bodies, multi-part message bodies, and textual header information in character sets other than US-ASCII.” ____ Source: Multipurpose Internet Mail Extensions. (MIME) Part Two: Media Types. Available at:     NOTE: Current usage of MIME describes content type in general. 6 6 6 6 19. Android – Intent Filters Intent Filters 6 Intent Resolution Khi các Intent được phát ra, Android tìm cách phù hợp nhất đáp ứng yêu cầu. Quyết định thực thi cái gì được dựa trên mức độ mô tả của lời gọi: Explicit Intent chỉ rõ một Component cụ thể ( bằng setComponent(ComponentName) hoặc setClass(Context, Class) ), Nó cho biết chính xác lớp nào cần chạy. Đây là cách thông thường mà một ứng dụng bật các activity nội tại của nó khi người dùng tương tác với ứng dụng. Implicit Intent không chỉ rõ component nào. Tuy nhiên, chúng chứa đủ thông tin để hệ thống xác định các component sẵn có nào là phù hợp nhất để chạy cho intent đó. 7 7 7 7 7 19. Android – Intent Filters Intent Filters 7 Intent Resolution “Cơ chế phân giải intent ( intent resolution) về cơ bản là so sánh intent với các mô tả trong các gói ứng dụng đã cài trong thiết bị.” Thông tin chi tiết:     8 8 8 8 19. Android – Intent Filters Intent Filters 8 Intent Resolution    9 9 9 9 19. Android – Intent Filters Intent Filters 9 Intent Resolution Như trong minh họa, Activity 3 đã phát một request chung chung để yêu cầu giúp xử lý một tin nhắn đến. Giả sử user đã cài ứng dụng “Fancy SMS” để dùng thay cho ứng dụng “HUMBLE SMS” vốn được cung cấp sẵn trong Android. Khi nhận được implicit Intent, Android sẽ (bằng cách nào đó) báo với user: Bạn có một tin nhắn mới. Tôi có các ứng dụng FANCY SMS và HUMBLE SMS – bạn muốn tôi chạy cái nào? Cho nó làm lựa chọn mặc định nhé? Chọn các ứng cử viên: Để được chọn thực thi, một activity phải: 1. Hỗ trợ action đã chỉ rõ 2. Hỗ trợ kiểu MIME được báo (nếu có) 3. Hỗ trợ tất cả các category được liệt kê trong intent. _____________ RULE OF THUMB: Intent càng cụ thể càng tốt 10 10 10 10 19. Android – Intent Filters Intent Filters 10 Example: Intent Filters Manifest nói rằng ứng dụng (FancySms) có thể nhận dữ liệu SMS bằng SMSReceiver của nó (lựa chọn thay thế tiềm năng cho ứng dụng SMS mặc định) 	 	 	 11 11 11 11 19. Android – Intent Filters Intent Filters 11 Comments on the example: Ứng dụng có 2 components: Một activity thường FancySms (đóng vai trò main routine) và Một Service (BroadcastReceiver) tên SMSService. Mệnh đề sau khai báo rằng ứng dụng được phép nhận SMS 	 Component SMSService có filter 	 Filter này làm cho SMSService được chạy mỗi khi có một tin nhắn mới đến Các ứng dụng khác với filter giống như trên cũng có thể được Android gọi khi một tin nhắn đến (cho đến khi một ứng dụng được chọn làm mặc định)  12 12 12 12 19. Android – Intent Filters Intent Filters 12 Example: Intercepting Incoming SMS    	 	 	 13 13 13 13 19. Android – Intent Filters Intent Filters 13 Example: Intercepting Incoming SMS    Note: Test the following application from the Eclipse’s DDMS perspective. Select “Emulator Control” > “Telephony Actions”. Set phone no. to 5554, type a message, click on Send. Alternatively you may start another emulator and send SMS to 5554 14 14 14 14 19. Android – Intent Filters Intent Filters 14 Example: Intercepting Incoming SMS    // FancySms: main screen - displays intercepted SMS package cis493.intentfilters; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class FancySms extends Activity { static TextView txtMsg; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); txtMsg = (TextView)findViewById(R.id.theMessage); } }// class FancySms 15 15 15 15 19. Android – Intent Filters Intent Filters 15 Example: Intercepting Incoming SMS    // SMSReceiver: listens to broadcasted SMS_RECEIVED signals package cis493.intentfilters; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.gsm.SmsMessage; import android.widget.Toast; public class SMSReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // Android saves in a bundle the current text-message // under name "pdus" and type: Object[]. Later we cast to // SmsMessage[]. Jargon pdu stands for "protocol data unit" Bundle bundle = intent.getExtras(); 16 16 16 16 19. Android – Intent Filters Intent Filters 16 Example: Intercepting Incoming SMS    Object messages[] = (Object[]) bundle.get("pdus"); SmsMessage smsMessage[] = new SmsMessage[messages.length]; // Note: long sms are broken and transmitted into various pieces String msg = ""; int smsPieces = messages.length; for (int n = 0; n >> Received SMS: " 	+ smsMessage[0].getMessageBody(), Toast.LENGTH_LONG); toast.show(); cis493.intentfilters.FancySms.txtMsg.setText(msg); } }// class SMSReceiver 17 17 	 19. Android – Intent Filters Intent Filters 17 	 	 Questions 18 18 	 19. Android – Intent Filters Intent Filters 18 JARGON: PDU 	is short for "Protocol Data Unit". This represents an amount of information delivered through a network layer. VND	 	virtual network data (today typically represents a business card with name, phone, email, etc). Originally registered as MIME vnd.abc intended for transmission of abc folk melodies in emails 	see: 

File đính kèm:

  • pptxLập trình Android tiếng Việt - Chapter 19 Android Intent Filters.pptx
Tài liệu liên quan