An explanation of the relationship between Android Intent and/intent-filter.

Source: Internet
Author: User
Tags types of filters

Intent can be divided into two types:an explicit intentAndImplicit Intent;
an explicit intent: Specifies the target component through the Component Name field, because developers often do not know the component names of other applications, so explicit intent is typically used for intra-application messaging; For example, an activity initiates a subordinate service or initiates an activity at the same level;
Implicit Intent: does not specify the name of the target component (the Component Name field is empty); Implicit intent is often used to activate components in other applications;
The Android system passes an explicit intent message object to an instance of the specified target component name, and the intent message object uses only the component name content area to determine which component should obtain the intent message object, rather than the other content;
For an implicit intent message object, you need to specify a different policy; because the target component name is not specified, the Android system must find and match one of the most appropriate components or multiple appropriate components to handle the intent message object: An activity or service to perform a request action, or a group of broadcast receivers to respond to a message; For this purpose, Android defines a intent filter class named Intentfilter to accomplish this task; An instance of the Ntentfilter class implements the lookup and matching of the target component by comparing the contents of the intent message object with the intent filter, and each Intentfilter instance describes the ability of the component to respond to the intent message object: The component wants to receive what type of request behavior, what type of request data, and so on; The intent filter instance is associated to a potential component that receives intent message objects, declares the ability of the target component to respond to intent message objects, and defines the intent message objects that the target component can handle. They open the implicit intent of the intent Message object type that the component receives, and if a component does not have any intent filters associated with it, it can only receive an explicit intent message object. The component that declares the intent filter can receive explicit and implicit intent message objects;
The three elements that need to be compared when a intentfilter instance is matched with an implicit intent is:Actions Action、Data(including URI and MIME type) andcategory CategoryWhile additional information extras and flags flags do not work when finding matches; in fact, an implicit intent message object, if it is to be successfully passed to the target component, must pass these three aspects of the check and match, if any aspect does not match, The Android system does not pass the implicit intent message object to the target component;
Activity, service, and Broadcastreceiver in order to tell the system which implicit intent to handle, they can have one or more intent filter instances, and each filter describes a component's ability to be interested in a set of intent message objects In fact, it filters out the intent that are not interested, and is just an unwanted implicit intent message object; an explicit intent message object can always be successfully passed to the target component, regardless of what it contains or filters, without any lookups and matches; However, an implicit intent message object can be successfully passed to the target component only if it passes through the filter of the component;
Each piece of work that a target component can do has a separate filter, and each filter has an instance of the corresponding Intentfilter class, because the Android system must know the capabilities of the component before starting a component, but the intent filter is not usually set in the Java code , but is set in the label <intent-filter> in the application's manifest file Androidmanifest.xml, but with one exception, The filter of the broadcast message receiver can be dynamically registered through the function context.registerreceiver (), which directly creates an instance of the Intentfilter class;
A filter has corresponding action action, data and category category; Filter to check all three fields of the implicit intent message object, where any one of the field matches fails, The Android system does not pass the implicit intent message object to the target component, however, because a component can have multiple intent filters, an implicit intent message object cannot be checked by a filter, but it is possible to check with another filter;
First, the action check
tags in the manifest file <intent-filter> use sub-tags <action> to list action action, for example:
<intent-filter ...>
<action android:name= "Com.test.project.ADD_BOOK"/>
<action android:name= "Com.test.project.EDIT_BOOK"/>
<action android:name= "Com.test.project.DELETE_BOOK"/>
......
</intent-filter>
A intent Message object describes only one action, and a filter can specify multiple actions, the action list cannot be empty, a filter must contain at least one <action> child tag, otherwise it will block all intent message objects;
To pass the check, the action action in the intent message object must match one of the filter's action lists, and if no specific action action is specified in the intent message object or in the filter's action list, the following two scenarios occur:
1, if the filter does not specify any action action, then all intent message objects will not match successfully, all intent message objects would fail to match, that is, none of the intent message objects can pass through the filter;
2, if the intent message object does not specify any action action, it will automatically pass the filter check (as long as there is at least one action in the filter's action list, otherwise it is the case above);
ii. Types of inspection
tags in the manifest file <intent-filter> use sub-tags <category> to list category categories, for example:
<intent-filter ...>
<category android:name= "Android.intent.category.DEFAULT"/>
<category android:name= "Android.intent.category.BROWSABLE"/>
......
</intent-filter>
If a intent message object is to be checked by kind, then each species in this intent message object must match one of the categories in the filter list, that is, the filter can list additional kinds, but each of the intent message objects must be able to be found in the list of types of filters, As long as there is a category that is not found in the category List of the filter, the type detection is considered to be a failure;
Therefore, in principle, if a intent message object does not have a specified type, that is, the kind field is empty, then it should always be checked by the type of filter, regardless of the type of filter, but with one exception, The Android system treats all implicit intent message objects that are passed to the context.startactivity () function, and they must contain at least the kind of "Android.intent.category.DEFAULT" (corresponding Category_ DEFAULT constants); Therefore, if the activity is to receive an implicit intent message object, it must include the kind "Android.intent.category.DEFAULT" in the intent filter;
Notes: category "Android.intent.action.MAIN" tag activity starts a new task, category "Android.intent.category.LAUNCHER" identifies the start home interface; they can contain kind " Android.intent.category.DEFAULT "to a kind of class table, which can also not contain;
third, data inspection
Labels in the manifest file <intent-filter> use sub-tags <data> to list data;
<intent-filter ...>
<data android:mimetype= "video/mpeg" android:scheme= "http" .../>
<data android:mimetype= "audio/mpeg" android:scheme= "http" .../>
......
</intent-filter>
Each <data> tag contains a URI for the data and a MIME type for the data, and it has four properties: scheme, host, port, and path, respectively, corresponding to each part of the URI;
The format is as follows:
Scheme://host:port/path
The host and port collectively form the credentials of the URI (authority), and if the host is not specified, the port is also ignored, all of the four properties are optional, and not all of them are completely independent of me; if you want to make authority meaningful, the scheme must be specified; To make path meaningful, scheme and authority must be specified;
When checking the URI that matches the intent message object and filter, only the URI attribute appearing in the filter is compared, for example, if a filter only specifies scheme, all URIs of this scheme can be successfully matched by the filter; If a filter specifies scheme and authority, but no path is specified, all matching scheme and authority URIs can be successfully matched by the filter, regardless of their path, and if all four attributes are specified, the match succeeds. However, the path in the filter can contain wildcard characters that require matching part of path;
The Type property of the <data> tag specifies the MIME type of the data; Intent message objects and filter instances can use wildcard "*" to match subtype fields; For example: "text/*", "audio/*" means any subtype;
The data check checks both the URI and the data type, and the rules are as follows:
Rule 1, a intent Message object does not contain a URI or a data type: only if the filter does not specify any URI and data type, it cannot pass the check;
Rule 2, a intent Message object contains a URI, but does not contain a data type: only if the filter also does not specify any data types, and matches their URIs, it can pass the check;
Rule 3, a intent Message object contains a data type, but does not include a URI: only if the filter also specifies the data type, and the same as the intent message object, it can pass the check;
Rule 4, a intent Message object contains both a URI and a data type: A data type part that is checked only if it matches one of the data types specified in the filter; In the URI section, its URI is to appear in the filter, or it has a "content:" or "File:" uri, or the filter does not specify a URI; in other words, if its filter lists only data types, the component assumes support for "content:" or "File:";
If a intent message object can be filtered through multiple activity or service, the user may be asked: Which component is activated? If the target component is not found, an exception is generated;
Iv. General Conditions
The last rule above indicates that the component can fetch local data from a file or a content provider, so their filters only list data types and do not have to explicitly indicate the scheme name of "content:" or "File:";
is a typical case, a <data> tag looks like this:
<data android:mimetype= "image/*"/>
This configuration tells the Android system that the component can fetch image data from the content provider and display it, because most of the available data is distributed by the content provider, the filter specifies a data type but does not specify a URI, perhaps the most common;
Another common configuration is a filter that specifies a scheme and a data type, for example:
<data android:scheme= "http" android:type= "video/*"/>
This configuration tells the Android system that the component can get video data from the network and display it;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.