Previously wrote a intent blog, mainly about the implicit intent. Portal: "Android-Intent"
Intent Object Composition
Component name, Action, Data, Category, Extras, Flags
Component name
Component name is the component name, which is the component name to process the intent object.
The component name object is encapsulated by the ComponentName class, and the component name contains the package name and class name, which is declared in the Androidmanifest.xml file.
The component name is obtained through the getcomponent ( ) by setcomponent (), SetClass (), Setclassname ( ) settings.
Note that component name is an option, and if set, then the intent object explicitly specifies the component to turn to , and if it is not set, the intent object needs to filter the lookup based on other information.
Action
An action is a string constant that intent the action to complete.
There are a number of predefined constants in the intent class, for some common actions, and others that are defined elsewhere in the Android API. You can also define the action constants yourself, and the custom constants need to be prefixed with the package name of your app.
The action largely determines how other parts of intent are constructed, especially the data and extras domains. (as if the function name determines the value of the parameter and the return value.) So the name of the action should be as specific as possible, and they should be tightly combined with other domains in the intent.
Use Setaction () and Getaction () to set and read the Action property.
Data
The Data property is the URI and MIME type that performs the action, and different actions have different specifications.
For example, when the action is Action_edit, the data field is the uri;action of the document, the data field is Tel:uri, with the phone number to dial, and if the action is Action_view, the data field is http: Uri.
It is important to know the data type (MIME type) when matching intent and the components that can handle the data that the intent takes . For example, a component that displays an image should not be called to play an audio.
In many cases, data types, such as Content:uris, can be seen from URIs, indicating that the data is on the device, but is controlled by the content provider.
The data type can also be specified explicitly, such as the SetData () method specifies that the data is Uri,settype () specified as MIME type,setdataandtype () specifying that it is both a URI and a MIME type. Read the URI with GetData (), MIME type with GetType ().
Category
The category is a string that provides additional information about the kinds of components that can handle the intent object.
A intent object can contain any number of category description information.
The method corresponding to category has add addcategory (), remove removecategory (), and get all category GetCategories ().
Extras
The extra data passed to intent is defined as bundles, which are some key-value pairs. As if some actions correspond to a particular data URI, some actions correspond to a specific extras.
For example, action_timezone_changed intent object has a "Time-zone" extra to confirm the new time zone;
Action_headset_plug has a "state" extra indicates whether the headset is plugged in, and there is a "name" extra about the headset type;
If you want to design a show_color action, then the extra should contain the color value.
The intent object has a series of putxxx () functions to put into various data types, and a series of getxxx () functions are used to read the data.
In fact, data can be used as a bundle object using the Putextras () and Getextras () methods.
Flags
Various types of flag. Many are used to specify how the Android system initiates activity, and how to treat it when the activity is activated. All of these are defined in the intent class.
Intent Filter
To tell the system which implicit intent can be handled, Activity,service and broadcast receiver have one or more intent filter.
Each filter describes a component's ability, and a collection of intent that this component is willing to receive. It actually has the effect of filtering out certain types of intent, excluding unwanted intent , but also only excluding unwanted implicit types of intent, and explicit intent can be passed directly to its target object, whatever it contains, Filter is not queried.
But an implicit intent can only be passed to this component when it is able to pass one of the components ' filter.
A component has a variety of independent filter types for all the work it can do.
A intent filter is an object of the Intentfilter class, but since the Android system must know its capabilities before starting a component, intent filter is usually not built in Java code, Instead, it is in the application's manifest file (Androidmanifest.xml), as a <intent-filter> element.
An exception is the filter for broadcast receiver, which is dynamically registered through the Context.registerreceiver () method, which will be created directly as a Intentfilter object.
When a intent object and a intent filter are tested, only three aspects of the intent object are referenced:
- Action
- Data (URI and datatype)
- Category
Extras and flags do not work when the component of the resolution receives intent.
A filter contains fields that are parallel to the action,data,category in the intent object, and an implicit intent will test all three domains .
In order to pass to the component containing the filter, the intent object must pass all three Tests.
if one of the tests fails, Android The system will not put this intent object is passed to this component, at least not based on the filter 's Components .
However, because a component can have more than one intent filter, one intent object may not pass one of the filter, but it may pass through another.
Code Action
In manifest, the,<intent-filter> element lists the action as its <action> child elements:
<Intent-filter. . . > <ActionAndroid:name= "Com.example.project.SHOW_CURRENT" /> <ActionAndroid:name= "Com.example.project.SHOW_RECENT" /> <ActionAndroid:name= "Com.example.project.SHOW_PENDING" /> . . .</Intent-filter>
A filter can list multiple actions, the list cannot be empty, that is, a filter should contain at least one action , otherwise it will block any intent.
To pass this test, theaction specified in the intent object should match one of the listed actions , and if the intent object or filter does not specify an action, the result will be as follows:
1. If filter does not list any action, nothing makes intent match, so all intent will fail in the test. No intent can pass this test.
2. On the other hand, if a intent object does not specify an action, it will pass the test automatically. (As long as the filter contains at least one action).
Code Category
<intent-filter> also lists the classifications as its child elements, such as
<> <android:name= " Android.intent.category.DEFAULT "/> <android:name = "Android.intent.category.BROWSABLE" /> . . . </ Intent-filter >
If a intent object wants to pass category, then each category in the intent object must match one of the categories in the filter .
Filter can list redundant categories, but you cannot ignore any of the categories in intent.
In principle, a intent object without any category should always be able to pass the test . With one exception, Android considers the implicit intent of all incoming startactivity () methods to have at least one category: "Android.intent.category.DEFAULT" (The category _DEFAULT constant).
Therefore, activity that wants to receive an implicit intent object must contain "Android.intent.category.DEFAULT" in their intent filter . (The filter with "Android.intent.action.MAIN" and "Android.intent.category.LAUNCHER" is an exception, they can contain " Android.intent.category.DEFAULT ", but they do not need to do so. )
Code Data
As with actions and classifications, data is also present as a sub-tag, and may not appear or appear more than once. Such as:
<Intent-filter. . . > <DataAndroid:mimetype= "Video/mpeg"Android:scheme= "http" . . . /> <DataAndroid:mimetype= "Audio/mpeg"Android:scheme= "http" . . . /> . . .</Intent-filter>
Each of the <data> elements can specify a URI and a data type (MIME media type).
Each URI contains the following attributes: scheme, host, port, path (for example: Scheme://host:port/path)
When a URI in a intent object is compared to a URI in filter, it is compared to only the portion of the URI actually mentioned in filter. For example, if a filter only specifies scheme, then all URIs that have that scheme will match the filter.
The path in filter can contain wildcard characters (wildcards).
The type attribute in the <data> element specifies the MIME type of the data, which is more common in filter than URIs.
Both the intent object and the filter can use the wildcard character * to represent subtypes, such as "text/*" or "audio/*"-indicating any subtype matches.
The data test needs to test the URI and data type, as follows:
A a Intent object that, if neither a URI nor a data type is included, can be tested only if filter is not included.
b If a intent object contains only one URI, does not contain a data type, and the data type is not available from the URI, you can pass the test only if it matches the URI in the filter and does not contain a data type in the filter. (this would be is the case only for URIs like Mailto:and Tel:that does not refer to actual data.)
C a Intent object if only the containing data type does not contain a URI, you can pass the test only if the filter contains the same data type and does not contain any URIs.
D If a intent object contains both a URI and a data type, (or a data type that can be inferred from a URI), then it can pass the test of the data type section only if it matches one of the data types in the filter, if you want to pass the test of the URI part, One case is the match between its URI and filter, and the other is that intent contains content: or File:uri and the filter does not contain a URI. In other words, if a filter specifies only the data type, the component is predefined to support content: or file: Data.
If a intent can pass multiple activity or service filter, the user will be asked to choose which component to start, and if no target is found, an exception will be generated.
I'm the dividing line of the king of the land Tiger.
Android--another intent.