1 / 18

Android Intent Filters

19. Android Intent Filters. Notes are based on: Android Developers http://developer.android.com/index.html. 19. Android – Intent Filters Intent Filters. So sánh: Requesting Actions Using HTTP and Android

karsen
Download Presentation

Android Intent Filters

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 19 Android Intent Filters Notes are based on: Android Developers http://developer.android.com/index.html

  2. 19. Android – Intent Filters 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). • ____ • Source: Hypertext Transfer Protocol -- HTTP/1.1 (1999). http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html • Source: http://developer.android.com/reference/android/content/Intent.html 2 2 2

  3. 19. Android – Intent Filters Intent Filters • 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: http://developer.android.com/reference/android/content/Intent.html 3 3 3

  4. 19. Android – Intent Filters Intent Filters Parts of a Typical Intent 4 4 4

  5. 19. Android – Intent Filters Intent Filters • 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: http://tools.ietf.org/html/rfc2046 NOTE: Current usage of MIME describes content type in general. 5 5 5

  6. 19. Android – Intent Filters Intent Filters • 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 đó. 6 6 6 6

  7. 19. Android – Intent Filters Intent Filters • 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ả <intent-filter> trong các gói ứng dụng đã cài trong thiết bị.” • Thông tin chi tiết: • http://developer.android.com/guide/topics/intents/intents-filters.html 7 7 7 7 7

  8. 19. Android – Intent Filters Intent Filters Intent Resolution 8 8 8 8

  9. 19. Android – Intent Filters Intent Filters • 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 9 9 9 9

  10. 19. Android – Intent Filters Intent Filters 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) <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cis493.intentfilters" android:versionCode="1" android:versionName="1.0.0"> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <application android:icon="@drawable/icon" > <activity android:name=".FancySms" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="SMSReceiver" android:enabled="true" > <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> </application> </manifest> 10 10 10 10

  11. 19. Android – Intent Filters Intent Filters • 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 • <uses-permission android:name="android.permission.RECEIVE_SMS" /> • Component SMSService có filter • <intent-filter> • <action android:name="android.provider.Telephony.SMS_RECEIVED" /> • </intent-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)  11 11 11 11

  12. 19. Android – Intent Filters Intent Filters Example: Intercepting Incoming SMS • <?xml version="1.0" encoding="utf-8"?> • <LinearLayout • android:id="@+id/mainLayout" • android:layout_width="fill_parent“ android:layout_height="fill_parent" • android:orientation="vertical" • xmlns:android="http://schemas.android.com/apk/res/android" • > • <TextView • android:layout_width="fill_parent" android:layout_height="wrap_content" • android:textSize="20px" android:textStyle="bold“ ndroid:background="#ff0000ff" android:text="Intercepting SMS messages" • /> • <ScrollView • android:id="@+id/myScroller1" • android:layout_width="fill_parent" • android:layout_height="fill_parent" • > • <TextView • android:id="@+id/theMessage" • android:layout_width="fill_parent" android:layout_height="fill_parent" • android:background="#ffffffff" android:padding="4px" android:textSize="14px" android:textColor="#ff000000" • /> • </ScrollView> • </LinearLayout> 12 12 12 12

  13. 19. Android – Intent Filters Intent Filters 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 13 13 13 13

  14. 19. Android – Intent Filters Intent Filters Example: Intercepting Incoming SMS // FancySms: main screen - displays intercepted SMS package cis493.intentfilters; importandroid.app.Activity; importandroid.os.Bundle; importandroid.widget.TextView; publicclassFancySmsextends Activity { staticTextViewtxtMsg; @Override publicvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); txtMsg = (TextView)findViewById(R.id.theMessage); } }// class FancySms 14 14 14 14

  15. 19. Android – Intent Filters Intent Filters Example: Intercepting Incoming SMS // SMSReceiver: listens to broadcasted SMS_RECEIVED signals package cis493.intentfilters; importandroid.content.BroadcastReceiver; importandroid.content.Context; importandroid.content.Intent; importandroid.os.Bundle; importandroid.telephony.gsm.SmsMessage; importandroid.widget.Toast; publicclassSMSReceiverextendsBroadcastReceiver { @Override publicvoidonReceive(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(); 15 15 15 15

  16. 19. Android – Intent Filters Intent Filters Example: Intercepting Incoming SMS • Object messages[] = (Object[]) bundle.get("pdus"); • SmsMessagesmsMessage[] = newSmsMessage[messages.length]; • // Note: long sms are broken and transmitted into various pieces • String msg = ""; • intsmsPieces = messages.length; • for (int n = 0; n < smsPieces; n++) { • smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]); • // grab all pieces of the intercepted sms • msg += "\n" + (n + 1) + " -of- " + smsPieces + "\n" • + "Sender:\t" + smsMessage[n].getOriginatingAddress() + "\n" • + "Body: \n " + smsMessage[n].getMessageBody(); • } • // show first part of intercepted (current) message • Toast toast = Toast.makeText(context, "FANCY >>> Received SMS: " • + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG); • toast.show(); • cis493.intentfilters.FancySms.txtMsg.setText(msg); • } • }// class SMSReceiver 16 16 16 16

  17. 19. Android – Intent Filters Intent Filters Questions 17 17

  18. 19. Android – Intent Filters Intent Filters 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:http://www.iana.org/assignments/media-types/text/vnd.abc 18 18

More Related