I have previously reported four important components of Android Application Development for everyone, and detailed the four important components of an Android application. These four components are independent and can be called and coordinated to form a real Android Application. The communication between these components is mainly completed with Intent assistance. Intent describes the actions, actions involving data, and additional data of an application. Android identifies the corresponding components based on the description of the Intent, pass Intent to the called component and complete the call of the component. Therefore, Intent serves as a media intermediary, providing information about component calls to each other to decouple callers from callers.
For example, in an application maintained by a contact, when we assume the corresponding Activity is listActivity on the screen of a contact list, click a contact, if you want to jump out of the contact's Details Screen and assume that the corresponding Activity is detailActivity), listActivity needs to construct an Intent, which is used to tell the system, we need to perform the "View" action. The object corresponding to this action is "a contact". Then, call startActivity (Intent intent) to pass in the constructed Intent, the system will find the Activity that meets the Intent requirement in ManiFest according to the description in this Intent. The system will call the Activity found, that is, detailActivity, and finally pass in the Intent, detailActivity performs the corresponding operation based on the description in the Intent.
1. abstract description
In the Android reference document, Intent is defined as an abstract description of an operation ). Let's take a look at the abstract description.
First, it is a brief description of the action to be executed, such as VIEW_ACTION view) and EDIT_ACTION modification. Android defines a set of standard actions for us:
◆ MAIN_ACTION
◆ VIEW_ACTION
◆ EDIT_ACTION
◆ PICK_ACTION
◆ GET_CONTENT_ACTION
◆ DIAL_ACTION
◆ CALL_ACTION
◆ SENDTO_ACTION
◆ ANSWER_ACTION
◆ INSERT_ACTION
◆ DELETE_ACTION
◆ RUN_ACTION
◆ LOGIN_ACTION
◆ CLEAR_CREDENTIALS_ACTION
◆ SYNC_ACTION
◆ PICK_ACTIVITY_ACTION
◆ WEB_SEARCH_ACTION
In addition, we can also define our own actions based on the needs of the application, and define the corresponding Activity to process our custom actions.
Second, it is the data to be operated by the execution action. Android uses a URI pointing to the data. For example, in a contact application, a URI pointing to a contact may be: content: // contacts/1. This type of URI representation is described through the ContentURI class. For details, refer to the documentation of the android.net. ContentURI class.
Take the Contact application as an example. The following are some action/data pairs and their intention to express them:
◆ VIEW_ACTION content: // contacts/1 -- display the contact details with the identifier "1"
◆ EDIT_ACTION content: // contacts/1 -- edit the contact details with the identifier "1"
◆ VIEW_ACTION content: // contacts/-- display the list of all contacts
PICK_ACTION content: // contacts/-- displays a list of all contacts, allows users to select a contact in the list, and then returns this contact to the parent activity. For example, the email client can use this Intent and requires the user to select a contact from the contact list.
In addition to the two important attributes of action and data, there are also some additional attributes:
◆ Category), additional information about the executed action. For example, LAUNCHER_CATEGORY indicates that the receiver of Intent should appear as a top-level application in Launcher, while ALTERNATIVE_CATEGORY indicates that the current Intent is one of a series of optional actions, which can be executed on the same piece of data.
◆ Type data type), explicitly specifying the Intent data type MIME ). Generally, the Intent data type can be determined based on the data itself. However, by setting this attribute, You can forcibly use the explicitly specified type instead of derivation.
◆ Component), specifies the Class Name of the Intent target component. Generally, Android searches for other attributes in Intent, such as action, data/type, and category, and finds a matched target component. However, if this attribute of component is specified, the component specified by component will be used directly without executing the above search process. After this attribute is specified, all other Intent attributes are optional.
◆ Extras additional information) is a collection of all other additional information. You can use extras to provide extended information for components. For example, if you want to perform the "send email" action, you can save the email title and body in extras, send to the email sending component.
In short, action, data/type, category, and extras form a language. This language enables the system to understand phrases such as "View Details of a contact. As applications are constantly added to the system, they can add new actions, data/type, and category to extend the language. Applications can also provide their own activities to process existing "phrases" and change the rows of these "phrases.
Ii. How to parse Intent in Android
In applications, we can use Intent in two forms:
Intent: Specifies the Intent of the component Attribute and calls setComponent (ComponentName) or setClass (Context, Class ). Notify the application to start the corresponding component by specifying a specific component class.
Indirect Intent: the Intent of the comonent attribute is not specified. The Intent must contain sufficient information so that the system can determine the components that meet the Intent among all available components based on the information.
For a direct Intent, Android does not need to parse it because the target component is already clear. Android needs to parse the indirect Intent, map Intent to the Activity, IntentReceiver, or Service that can process the Intent.
The Intent parsing mechanism mainly finds the matching Intent by finding all intentfilters registered in AndroidManifest. xml and the Intent defined in them. In this parsing process, Android judges by the Intent action, type, and category attributes. The judgment method is as follows:
If Intent specifies an action, the action must be included in the IntentFilter action list of the target component; otherwise, the action cannot be matched;
If the Intent does not provide a type, the system obtains the data type from the data. Like action, the Data Type list of the target component must contain the Intent data type. Otherwise, the data type does not match.
If the data in the Intent is not a content: type URI and the Intent does not explicitly specify its type, it will match according to the scheme of the data in the Intent, such as http: or mailto. Similarly, the scheme of Intent must appear in the scheme list of the target component.
If Intent specifies one or more category categories, these categories must all appear in the Set category list. For example, Intent contains two categories: LAUNCHER_CATEGORY and ALTERNATIVE_CATEGORY. The parsed target component must contain at least these two categories.