Intent
The basic design concept of Android is to encourage the reduction of coupling between components. Therefore, Android provides intent (intent) and intent provides a general message system, it allows you to pass intent between your application and other applications to execute actions and generate events. Using intent, You can activate three core components of the Android app: Activity, service, and broadcast receiver. Data can also be carried by intent. putextra (name, value)
Intent can be divided into explicit intent and implicit intent.
Explicit intent: Call intent. setcomponent () \ intent. setclassname () or intent. setclass () method or in new intent (. this, B. class) specifies the activity to be switched to. The intent explicitly specifies the component name as the explicit intent, and the explicit intent specifies the component to be activated.
Implicit intent: intent without explicitly specifying the component name is implicit intent. The Android system will find the most suitable component to process the intent based on the action, category, and data (Uri and data type) set in the implicit intent.
For implicit intent, how does Android find the most appropriate component? Before we define an activity, we specify an intent-filter. The intent filter is actually used to match the implicit intent, when an intent object is tested by an intent filter, only three aspects are referenced: Action, data (Uri and data type), and category.
Action Test)
An intent object can only specify one action name, and a filter may list multiple action names. If no action is specified for the intent object or filter, the result is as follows:
• If the filter does not specify any action, all intent will be blocked, so all intent tests will fail. There is no intention to pass this filter.
• On the other hand, as long as the filter contains at least one action, an intent object without a specified action will automatically pass this test
CATEGORY Test)
For an intent that can pass the class matching test, the category in the intent object must match the category in the filter. This filter can list another category, but it cannot omit any category in this intent.
In principle, an intent object without a category should always pass the matching test, regardless of what is in the filter. In most cases, this is true. With one exception, Android treats all the implicit intent sent to startactivity () as containing at least one category: "android. Intent. Category. Default" (category_default constant ). Therefore, activities that want to receive implicit intent must include "android. Intent. Category. Default" in their intent filter ". (The filters with "android. Intent. Action. Main" and "android. Intent. Category. launcher" are exceptions)
Data Test)
When a URI in an intent object is used to compare with a URI in a filter, each component of the URI is compared. For example, if the filter specifies only one scheme, all the Uris of the scheme can match the filter. If the filter specifies a scheme, the host name, but no path, all URIs with the same scheme and host name can match this filter, regardless of their path; If the filter specifies a scheme, host name, and path, only URLs with the same scheme, host name, and path can match this filter. Of course, the path type in a filter can contain wildcards, so that only partial matching is required.
The data test compares the URI and data type specified in the intent object and filter at the same time. The rules are as follows:
A. An intent object that neither contains the URI nor the data type can pass the test only when the filter does not specify any Uris and data type.
B. An intent object that contains a URI but does not have a data type can pass the test only when its URI matches the URI in a filter that does not specify a data type. This usually happens on a URL like mailto: And Tel: they do not reference actual data.
C. An intent object that contains a data type but does not contain a URI can pass the test only when the filter lists the same data type and does not specify a URI.
D. An intent object that contains both the URI and the data type (or can infer the data type from the URI) can be tested if its type matches the type listed in the filter. If its URI matches a URI in the filter, or it has a content: or file: URI, and this filter does not specify a URI, it can also pass the test. In other words, a component is assumed to support content: and file: data if its filter lists only one data type.
For example, androidmanifest. xml contains:
<Intent-filter>
<Action Android: Name = "com. nanlove. wangshiming"/>
<Action Android: Name = "wangshiming"/>
<Category Android: Name = "android. Intent. Category. Default"/>
<Category Android: Name = "wangshiming. Intent. Category"/>
<Data Android: Scheme = "love" Android: host = "hao123.com"
Android: Port = "888" Android: Path = "/mm" Android: mimetype = "text/plain"/>
</Intent-filter>
There can be multiple action items in <intent-filter> as long as one of them matches.
Intent. setaction ("com. nanlove. wangshiming"); the action in can also be
Intent. addcategory ("wangshiming. intent. "Category") addcategory in the Code is not used because android has the default category, as long as the configuration list contains <category Android: Name = "android. intent. category. default "/>,
If no data parameter exists, the setting action and category of the intent object appear in intent-filter and match with filter.
But there is data <data Android: Scheme = "love" Android: host = "hao123.com"
Android: Port = "888" Android: Path = "/mm"/> data items must be exactly matched,
Intent cannot be used when data and data types exist simultaneously in Android: mimetype = "text/plain. setdata (URI. parse ("Love: // hao123.com: 888/mm") because the setdata method automatically clears the previous data type: This method automatically clears any type that was previusly set by settype
So the following settype cannot be matched.
Intent. setdataandtype (URI. parse ("Love: // hao123.com: 888/mm"), "text/plain") should be used ");
It takes time to search for implicit intention matching rules in theory to apply explicit intention matching rules to the same application.