Android Intent and Intent filters and its usage

Published June 17, 2021

Intents are messaging object that we can use to request operations from one component to other component within the same application or other application. This Intent can provide communication between components in different ways, but basically we will use following three ways

  • Start Activity:startActivity()

  • Start the service:bindService()

  • Delivery broadcast:sendBroadcast()

 

Intent type

Intents are divided into two types:

  • Explicit Intent : Specify the component to be started by name (fully qualified class name) .

  • Implicit Intent : does not specify a specific component, but declares the general operation to be performed, thereby allowing components in other applications to handle it .

We will create an Intent when Activity or service is started, the system will immediately start the application component specified in the Intent object.

 

An illustration of how implicit intents are passed through the system to start other activities

 

while creating Intent, the Android system compares the contents of the Intent with the Intent filters which we will declare in the manifest files of the applications.  If the Intent matches the Intent filter, the system will start the component and pass it to the object. If multiple intent filters are compatible to the current one, the system will display a dialog box that allows the user to select the application to use.

 

To ensure the security of the application, when starting the Service, always use an explicit Intent, and do not declare an Intent filter for the service. Starting from Android 5.0 (API level 21), if you use an implicit Intent to call bindService(), the system will throw an exception.

 

Build Intent

  • Component Name (ComponentName) : This is optional, but it is also an important information for constructing an explicit Intent, which means that the Intent should only be passed to the application component defined by the component name. property name of the intent is ComponentName, we can use the target component fully qualified class name specified for this object, including the name of the application package.

  • Action : A string that specifies the general action to be performed (for example, "view" or "select").

  • Data : URI (Uri object) that refers to the data to be operated and/or the MIME type of the data. The type of data provided is usually determined by the operation of the Intent.

    To set only the data URI, call setData(). To set only the MIME type, call setType(). If necessary, you can use setDataAndType() to explicitly set both at the same time.

    Warning: To set the URI and MIME type at the same time, do not call setData() and setType() because they will cancel each other's values. Please always use setDataAndType() to set both URI and MIME type.

  • Category : A string containing additional information about the type of Intent component that should be processed. You can put any number of category descriptions into an Intent, but most intents do not require categories.

  • Extra : A key-value pair that carries additional information required to complete the requested operation. Just as some operations use specific types of data URIs, some operations also use specific additional data. For example, use ACTION_SEND create Intent for sending e-mail, you can use the EXTRA_EMAIL key specified "target" recipient, and use the EXTRA_SUBJECT key to specify theme

  • Flags : Flags defined in the Intent class that serve as Intent metadata. The flag can instruct the Android system how to start the Activity (for example, which Task the Activity should belong to).

 

Intent resolution

When the system receives an implicit Intent to start an Activity, it compares the Intent with the Intent filter according to the following three aspects, and searches for the best Activity of the Intent:

  • Intent operation

  • Intent data (URI and data type)

  • Intent category

The system tests the implicit Intent based on the filter by comparing the Intent with all three elements. The implicit Intent must pass all three tests in order to be passed to the component. If the Intent cannot even match any of these tests, the Android system will not pass it to the component . However, because a component may have multiple intent filters, an intent that fails a component filter may pass another filter. (I experimented several times in the Demo, and found that Action and Data must be set at least one, otherwise it cannot be matched)

 

Action matching

We can set intent to perform some specific action by adding the intent filter with action name.

E.g:

<intent-filter>
    <action android:name="android.intent.action.EDIT"/>
    <action android:name="android.intent.action.VIEW"/>
</intent-filter >

Through this filter, we specify in the Intent operation must be listed in the filter in an action match .

If the filter does not list any actions, then the intent has no match, so all intents will fail the test. However, if the Intent does not specify an action, the test will pass (as long as the filter contains at least one action).

 

Category matching

To specify the categories of acceptable Intent, Intent filter either does not declare any category elements

E.g:

 
<intent-filter>
    <action android:name="android.intent.action.MAIN"/>
    <category android:name="android.intent.category.LAUNCHER"/>
    <category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>

 

 

For the intent to pass the category test, each category in the intent must match the category in the filter . The opposite is not necessarily true . The declared categories of the Intent filter can exceed the number specified in the Intent, and the Intent will still pass the test. Therefore, an intent without a category should always pass this test, regardless of the category declared in the filter .

Android will automatically CATEGORY_DEFAULTcategories to be passed to startActivity()and startActivityForResult()all the implicit Intent. Therefore, if the Activity needs to receive implicit Intent, it must include the " android.intent.category.DEFAULT" category in its Intent filter.

 

Data matching

Intent To specify the data received, both Intent filter may not declare any data elements

E.g:



 
<intent-filter>
    <data android:mimeType = "video/mpeg"  android:scheme = "http" />
    <data android:mimeType = "audio/mpeg"  android:scheme = "http" />
</intent-filter>

 

Each <data> element can be specified URI structures and data types (MIME media type). Each part of the URI contains individual scheme, host, port, and path attributes:

scheme://host:port/path

E.g:

content://com.intentexample.android:222/foldername/filename/etc

In this URI, the structure is content, the host is com.intentexample.android, the port is 222, and the path is foldername/filename/etc. Each of the above attributes is optional, but there is a linear dependency:

  • If the architecture is not specified, the host is ignored.

  • If the host is not specified, the port will be ignored.

  • If the schema and host are not specified, the path will be ignored.

 

When comparing the URI in the Intent with the URI specification in the filter, it is only compared with part of the URI contained in the filter. E.g:

  • If the filter only specifies a schema, all URIs with that schema match the filter.

  • If the filter specifies the schema and authority but does not specify the path, all URIs with the same schema and authority will pass the filter, regardless of the path.

  • If the filter specifies the architecture, authority, and path, only URIs with the same architecture, authority, and path will pass the filter.

The path specification can include the asterisk wildcard (* ), so only a partial match of the path name is required.

 

Data matching compares the URI and MIME type in the Intent with the URI and MIME type specified in the filter. The rules are as follows:

  • When the filter does not specify any URI or MIME type, the Intent without URI and MIME type will be run without error.

  • For an Intent that contains a URI but does not contain a MIME type (which is neither explicitly declared nor inferred from the URI), only if the URI matches the URI format of the filter and the filter also does not specify the MIME type. Will execute.

  • When the filter lists the same MIME type and does not specify the URI format, an Intent that contains the MIME type but does not contain the URI will execute.

  • If the MIME type matches the type listed in the filter, an Intent containing the URI and MIME type (either explicitly declared or inferred from the URI) will execute.  If the URI matches the URI Intent with the filter, or if having Intent content: or file: part of the URI and the filter is not specified, the test will pass the Intent. In other words, if the filter list only MIME types, it is assumed that the component support content:and file:data.

 

The last rule, reflects the expectations component from a file or can be obtained at the local data. Thus, it may list only the filter data types, without having to explicitly name content: and file: schema. This is a typical case. For example, the following data elements to Android noted from assembly may obtain and display the image data.


 
<intent-filter>
    <data android:mimeType = "image/*"  />
</intent-filter>

 

Intent match

Our application can use intent matching in a similar way. PackageManager. It provides a set of query...() methods to return all of the components to accept specific Intent. In addition, it provides a series of similar resolve...() methods to determine the best response to the Intent assembly. For example, queryIntentActivities() it will return a list of those able to perform all Activity of Intent passed as a parameter, and queryIntentServices() we can return to a similar list of services. These two methods will not activate the components, but only list the components that can respond. For broadcast receiver, there is a similar method:  queryBroadcastReceivers()

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

127 Views