8 Jul 2015

What is the use of Intent Filter

An intent filter declares the capabilities of its parent component — what an activity or service can do and what types of broadcasts a receiver can handle. It opens the component to receiving intents of the advertised type, while filtering out those that are not meaningful for the component.  

<activity
    android:name="com.amar.deep.ShareActivity"
    android:label="@string/title_activity_share" >
    <intent-filter>    
 <action android:name="android.intent.action.SEND" />
      <category android:name="android.intent.category.DEFAULT" />
      <data android:mimeType="text/plain" />
    </intent-filter>
</activity>


There are following test Android checks before invoking an activity:
  • A filter may list more than one action as shown above but this list cannot be empty; a filter must contain at least one element, otherwise it will block all intents. If more than one actions are mentioned then Android tries to match one of the mentioned actions before invoking the activity.
  • A filter may list zero, one or more than one categories. if there is no category mentioned then Android always pass this test but if more than one categories are mentioned then for an intent to pass the category test, every category in the Intent object must match a category in the filter.
  • Each element can specify a URI and a data type (MIME media type). There are separate attributes like scheme, host, port, and path for each part of the URI. An Intent object that contains both a URI and a data type passes the data type part of the test only if its type matches a type listed in the filter.                         
Action which is the most reasonable action to perform and Data that contains the basic information that drives our action were pretty easy to comprehend but Category is something that might need a bit of an explanation. Using the category attribute, we give/mention additional information about the action to execute (that’ll be matched for intent resolution). Examples:
  • CATEGORY_LAUNCHER – Show up in the Launcher (Google Experience Launcher or Google Now Launcher or Nova or some other that you installed and use) as a top-level application. In other words the activity is the initial activity of a task (will launch when app starts) and is listed in the system’s application launcher (app drawer).
  • CATEGORY_BROWSABLE – Activities that can be safely invoked from a browser when a user clicks on a hyperlink.
  • CATEGORY_ALTERNATIVE – Activity should be included in a list of alternative actions the user can perform on a piece of data. So for instance if your code doesn’t specifies the intent action but sets this category and there’s an intent filter with this category too (but some other intent action name) then without even knowing the action name that intent filter will match with the implicit intent and deliver it to the component.
  • CATEGORY_DEFAULT – The docs is a little hard to understand compared to this much simplerexplanation. The CATEGORY_DEFAULT category is applied to all implicit intents (automatically) passed to startActivity() and startActivityForResult(). So if you want your activity to receive implicit intents, it must include a category or "android.intent.category.DEFAULT" in its intent filters.


No comments:

Post a Comment