Android Intent Filters
As the name suggests it helps filtering the right app component which Android System is searching for based on the properties set in the intent object.
Declared in the AndroidManifest file of the project as a child tag to component tags like <activity>, <activity-alias>, <service>, <receiver>, <provider>.
Declared in the AndroidManifest file of the project as a child tag to component tags like <activity>, <activity-alias>, <service>, <receiver>, <provider>.
Syntax:
<intent-filter> … </intent-filter>
https://developer.android.com/guide/topics/manifest/intent-filter-element
Attributes
- android:icon: A drawable resource. Specifies an icon for the activity. Can be used as a user facing icon.
- android:label: A user-readable label for the parent component.
- android:priority: An integer value. Activities with highest priority are considered potential targets for the new intent request. Receivers are provided to handle the request in the order of the priority set.
- android:order: An integer value, introduced in API 28 i.e does not have impact for lower than 28 API level. Tells the system which filter should be processed in case multiple filters are matched.
Child tags
- <action> (Required)
- <data> (Optional)
- <category> (Optional)
<activity android:name="ShareActivity">
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
</activity>
The following sections describe how intents are matched to the appropriate components according to the intent filter declaration in an app's manifest file.
Intent Resolution
The process where the system searches for the app component based on the implicit intent received by comparing the received intent with the intent filters set in the AndroidManifest file of all the applications.The following sections describe how intents are matched to the appropriate components according to the intent filter declaration in an app's manifest file.
Action Test
- Intent filter should have at least 1 action defined so that the action test can start.
- If intent has action and matches at 1 action in intent-filter passes the test.
- If intent does not have action, then it always passes the test.
Category Test
- Intent filters and intent can have zero or more categories
- If intent has categories each of them should be matched with intent-filter category to pass the test.
- If intent does not have a category the test is passed always irrespective of filter categories.
Data Test
- Intent filters and Intent can have zero or more data
- Data URI is in <scheme>://<host>:<port>/<path> format. And each of these are optional but have linear dependency for test verification i.e If <scheme> does not match other attributes are ignored. If <scheme> and <host> are not specified, <port> is ignored.
- URI comparison is done based on what is specified in intent-filter. So if filter specifies only <scheme> all the URIs with that scheme are considered as matched.
- Test passes only if at least data and MIME present in intent are specified in intent-filters. If not specified, the test will fail.
- Exception: Test passes if the intent URI has a content: or file: URI and intent-filter does not specify it. This explains that the components are able to get the local data from file or content provided so just providing the MIME type is enough.
Points to note
- If an incoming request fails to match even one of the defined Child Tags the intent request is not delivered to the component.
- All the activities should have intent filters declared in the manifest file.
- All the activities should have a <category> child tag set to receive implicit intents.
- Intent filters are not the best way to secure your app components. Your app component can be started if some knows the component name.
- Secure app components by setting exported attributes to “false”. This will not make the component available to other apps.
- Always try using explicit intent to start your own app Service.
Resources
https://developer.android.com/guide/components/intents-filtershttps://developer.android.com/guide/topics/manifest/intent-filter-element
Comments
Post a Comment