Intent官方教程(5)在manifest中给组件添加<Intent-filter>

Receiving an Implicit Intent

  To advertise which implicit intents your app can receive, declare one or more intent filters for each of your app components with an <intent-filter> element in your manifest file. Each intent filter specifies the type of intents it accepts based on the intent's action, data, and category. The system will deliver an implicit intent to your app component only if the intent can pass through one of your intent filters.

 <Intent-filter>设置应用可收到的Intent。

  Note: An explicit intent is always delivered to its target, regardless of any intent filters the component declares.

  An app component should declare separate filters for each unique job it can do. For example, one activity in an image gallery app may have two filters: one filter to view an image, and another filter to edit an image. When the activity starts, it inspects the Intent and decides how to behave based on the information in the Intent (such as to show the editor controls or not).

  Each intent filter is defined by an <intent-filter> element in the app's manifest file, nested in the corresponding app component (such as an <activity> element). Inside the <intent-filter>, you can specify the type of intents to accept using one or more of these three elements:

 <intent-filter>的子元素如下:

  <action>
    Declares the intent action accepted, in the name attribute. The value must be the literal string value of an action, not the class constant.
  <data>
    Declares the type of data accepted, using one or more attributes that specify various aspects of the data URI (scheme, host, port, path, etc.) and MIME type.
  <category>
    Declares the intent category accepted, in the name attribute. The value must be the literal string value of an action, not the class constant.

  Note: In order to receive implicit intents, you must include the CATEGORY_DEFAULT category in the intent filter. The methods startActivity() and startActivityForResult() treat all intents as if they declared the CATEGORY_DEFAULT category. If you do not declare this category in your intent filter, no implicit intents will resolve to your activity.

 如果想让你的组件模糊匹配到更多的意图,必需在<category>添加默认选项: <category android:name="android.intent.category.DEFAULT"/>

  For example, here's an activity declaration with an intent filter to receive an ACTION_SEND intent when the data type is text:

1 <activity android:name="ShareActivity">
2   <intent-filter>
3     <action android:name="android.intent.action.SEND"/>
4     <category android:name="android.intent.category.DEFAULT"/>
5     <data android:mimeType="text/plain"/>
6   </intent-filter>
7 </activity>

  It's okay to create a filter that includes more than one instance of <action>, <data>, or <category>. If you do, you simply need to be certain that the component can handle any and all combinations of those filter elements.

 可以定义多个<action>,<data>,<category>,

  When you want to handle multiple kinds of intents, but only in specific combinations of action, data, and category type, then you need to create multiple intent filters.

Restricting access to components
  Using an intent filter is not a secure way to prevent other apps from starting your components. Although intent filters restrict a component to respond to only certain kinds of implicit intents, another app can potentially start your app component by using an explicit intent if the developer determines your component names. If it's important that only your own app is able to start one of your components, set the exported attribute to "false" for that component.

 组件的exported="false"属性可以让组件不被其它应用通过Intent启动。


  An implicit intent is tested against a filter by comparing the intent to each of the three elements. To be delivered to the component, the intent must pass all three tests. If it fails to match even one of them, the Android system won't deliver the intent to the component. However, because a component may have multiple intent filters, an intent that does not pass through one of a component's filters might make it through on another filter. More information about how the system resolves intents is provided in the section below about Intent Resolution.

 精确型的Intent,严格检查上面介绍的3个子元素,完全匹配才算精确。

  Caution: To avoid inadvertently running a different app's Service, always use an explicit intent to start your own service and do not declare intent filters for your service.

  Note: For all activities, you must declare your intent filters in the manifest file. However, filters for broadcast receivers can be registered dynamically by calling registerReceiver(). You can then unregister the receiver with unregisterReceiver(). Doing so allows your app to listen for specific broadcasts during only a specified period of time while your app is running.
Example filters
  To better understand some of the intent filter behaviors, look at the following snippet from the manifest file of a social-sharing app.

 1 <activity android:name="MainActivity">
 2     <!-- This activity is the main entry, should appear in app launcher -->
 3     <intent-filter>
 4         <action android:name="android.intent.action.MAIN" />
 5         <category android:name="android.intent.category.LAUNCHER" />
 6     </intent-filter>
 7 </activity>
 8 
 9 <activity android:name="ShareActivity">
10     <!-- This activity handles "SEND" actions with text data -->
11     <intent-filter>
12         <action android:name="android.intent.action.SEND"/>
13         <category android:name="android.intent.category.DEFAULT"/>
14         <data android:mimeType="text/plain"/>
15     </intent-filter>
16     <!-- This activity also handles "SEND" and "SEND_MULTIPLE" with media data -->
17     <intent-filter>
18         <action android:name="android.intent.action.SEND"/>
19         <action android:name="android.intent.action.SEND_MULTIPLE"/>
20         <category android:name="android.intent.category.DEFAULT"/>
21         <data android:mimeType="application/vnd.google.panorama360+jpg"/>
22         <data android:mimeType="image/*"/>
23         <data android:mimeType="video/*"/>
24     </intent-filter>
25 </activity>

  The first activity, MainActivity, is the app's main entry point—the activity that opens when the user initially launches the app with the launcher icon:
  The ACTION_MAIN action indicates this is the main entry point and does not expect any intent data.
  The CATEGORY_LAUNCHER category indicates that this activity's icon should be placed in the system's app launcher. If the <activity> element does not specify an icon with icon, then the system uses the icon from the <application> element.
  These two must be paired together in order for the activity to appear in the app launcher.

 通常 ACTION_MAIN 表示是主程序 , CATEGORY_LAUNCHER表示在应用列表中显示。 它们要同时使用。

  The second activity, ShareActivity, is intended to facilitate sharing text and media content. Although users might enter this activity by navigating to it from MainActivity, they can also enter ShareActivity directly from another app that issues an implicit intent matching one of the two intent filters.
  

  Note: The MIME type, application/vnd.google.panorama360+jpg, is a special data type that specifies panoramic photos, which you can handle with the Google panorama APIs.

 

posted @ 2016-09-25 09:05  f9q  阅读(436)  评论(0编辑  收藏  举报