Intent Parsing
In addition to well-defined components, component-based architecture is also an art of Assembling these components together. In Android,
Intent (Usually translated:
Intention ...) Is a bridge connecting components. Some time ago, I saw my colleagues working on the Symbian platform.
Netease handheld mail (It is really a hard work, one meter of Nb. I warmly welcome all s60v3 users in 163 mail, and click here ...), you can add attachments to emails. For example, if you want to send an image bubble by email, you may need to select a collection image from the local device in an intuitive way, or take the camera for a perfect selfie. In Symbian, you need to use the underlying API to write such functions. To make the image selection experience better, you may need to make something similar to the image browser, in order to make the photo more smooth, you even need to implement a set of camera functions, from focus to brightness adjustment. In fact, users' mobile phones may have installed other professional image browsers, cameras, and other applications. These applications are already very useful, and users can already use them, if it can be called, it will be a better choice for developers and users of the mailbox. However, in a failed system like Symbian, the combination of applications and applications is incredibly weak. to reuse these systems, it is basically more difficult than logging on to the cloud. As a developer, you can only endure the nausea once and again. For the sake of users, do the additional work that repeatedly creates the wheel to be thankless. Fortunately, in Android, everything is much more beautiful. It saves developers from the details of interfaces and objects, so that we can devote more energy to the development of core functions. In Android, if you need to select a picture to take a picture, you only need to construct an intent that describes your intention and send it out, the system will help you select a component that can process the business to meet your needs, instead of having to tangle with specific interfaces and implementations. Perfect world should be like this.
Intent Composition
Intent's intention to be translated is actually very expressive. What intent expects to do is to completely decouple the real-time user from the caller. The caller can concentrate on clearly describing the intent and send it out to make the dream come true,. Of course, that's too much to say. Ding Jie Niu, you may be clear about everything.Intent(Reference/Android/content/intent.htmlIn Android, a class is displayed and an intent is initiated. You need to construct such an object and assign values to some of the following items:
-
- Action. When describing a willingness or desire in daily life, there is always a verb in it. For example, I wantDoThree push-ups; I wantViewAn x-clip; I wantWriteA history of tears, such as cloud. In intent, action is the action that describes the actions of reading, doing, and writing. When you specify an action, the performer will accept the input according to the instructions of the action to show the corresponding behavior, generate a compliant output. The intent class defines a batch of actions, suchAction_view,Action_pick, And so on, basically covers the common actions, the entire collection of the entire dragon's 18 ops. Of course, you can keep up with the times and create new actions, such as Lou. Compared with the predefined system, the circulation scope of these custom actions is very limited. Unless it is used for Nb, everyone needs to follow you, otherwise it is usually used for internal circulation.
- Data. Of course, actions alone are not enough, and more precise object information is required. For exampleBubbleThis action,Coffee maker, AndNiuIt's just a thousand miles away. Data description, represented asUri. For internal communication, it may be described as the form used by the content provider.Content: // xxxxThis kind of stuff, or an external shape suchTel: // xxxxSuch a link. In short, it is a URI that can clearly and accurately describe a data address.
-
- Type. If we talk about data, we must mention type. Many times, some people may misunderstand the difference between data and type, just likeNiuAndBubble MaziThe difference is the same, very small. But in fact it is not, type information, is usedMimeFor exampleText/plain. Here, the difference between the two is very clear. Data is the house number, indicating the specific location and specific analysis of specific problems, and type is to emphasize that things are clustered to solve the problem of batch. The actual example is as follows. For example, when a call is made from an application, the action is action_dial and the data is intent such as Tel: XXX. The corresponding human language isCall xxx. If type is used, it is much broader. For example, the browser receives an unknown MIME type data (such as a video ...), this kind of intent will be released, and other applications of the system will be requested for help. The natural language for a table to be achieved should be:View PDF documents.
- Category . With action and data or type, you can accurately express a complete intent, but sometimes you need to add some constraints to make it more accurate. For example, if you like to do push-ups, but three at a time only happens in special cases, you may say: I want to make three push-ups each time I eat them . The prefix corresponds to the category of intent category, which adds a constraint to the intent. In Android, an instance is that all the main activities of the application (that is, the first activity to be run when it is started independently ...), you must be able to accept the intent that category_launcher and action is action_main.
- component . Before that, we attempted to use action, data/type, and category to describe an intent. This is an android recommendation and we hope you will use it most of the time, in this mode, implicit intents is called in Android. In this mode, a flexible and scalable mode is provided to users and third-party applications. For example, if it is still a mailbox software, most of its functions are good, that is, the function of selecting images is very good. What should I do? If it uses implicit intents, it is an open system. If no other picture is selected on the mobile phone Program , you can continue to use the default mailbox. If there is, you can choose to replace the original module to complete this function, everything is natural. However, this mode does not have no cost, but requires some performance overhead, because there is a retrieval process after all. Therefore, Android provides another mode called explicit intents , which requires the help of component. Component is the class name , complete, like COM. XXXXX. XXXX , once specified, everything is clear, find this class (of course, it will be a specific subclass ...), succeeded. Otherwise, failed. The advantage is speed. It is suitable for you to use it when you know it is an internal module.
- Extras. Through the above items, we can identify the problem and solve it perfectly. The next important problem is passing parameters. Extras is used to do this, it isBundleClass Object, which has a set of serializableKey/value pair. Each action has the corresponding key and Value Type conventions. When initiating an intent, additional parameters that cannot be expressed by data must be put into extras (of course, if no additional parameter is required, forget it ...), otherwise, the executor will get crazy.
-
- Flags. The entire intent can be identified and input is complete, but some attachment commands need to be included in flags. As the name suggests, flags is an integer with the flag spaces of some columns. These flags are used to specify the running mode. For example, if you want the executor of this intent to run in two completely different tasks (or process is okay...), you need to setFlag_activity_new_task.
With the above, the image of an intent is on the paper. Such rich content determines that it is more powerful than the traditional model.
Intent matches the previous Android lecture on Moto Dev. Many of the following audience have expressed strong interest in the concept of intent and come up with their own concepts in the field for analogy, such as events and messages. At that time, I was thinking that intent, as the communication protocol between components, is different from the common simple communication method. First, we can see from the previous section that, it is described based on requirements rather than the implementer. Second, it is resolved directly by a third party rather than the two parties. This concept and the Mediator Pattern in the design pattern are one pulse, that is, all the peripheral components are only associated with the core modules of the system and are transferred through it, components are not directly hooked up.
As shown in, if you want to run the entire process, another important thing isIntent FiltersIt is used to describe an activity, serveice, and other components, what kind of intent is expected to respond. For a component, you only want other components to use the explicit it intents (that is, specify the component ...) you do not need to add intent filters. Otherwise, one or more intent filters are required. The items of intent filter are similar to the effects of intent in the mirror, including action, catagory, data, and type. Intent filters can be written to the configuration file and configured together with those components (do not remember what the configuration file is, please refer to here ...), several instances can be found on the intent introduction page (Reference/Android/content/intent.html). Similarly, intent filters can beCode, Dynamic plugging, this is dynamic pluggingBroadcast receiver erIs applicable. The core modules of the system are responsible for collecting the intent filters and their corresponding component information. When the requester needs a component and constructs an intent to describe its needs and sends it to the system core, the system core matches it with the known intent filters, select a required component to return. If there are multiple matching items, we will try to see if there are any default execution items. If there is no default ones, we will construct the UI and let the user make a choice. If so, the entire process will pass through.
Intent implementation
Is a simple implementation flowchart for requesting an activity component, which is the most used intent parsing instance. Process from callContext. startactivity (intent)Start, the caller passes in the constructed intent object, and then the process will let the actual performer, yesInstrumentationObject. It is the activity manager activated by the entire application and is responsible for the initiation, conversion, and termination of all the activities in the application. It has a hidden method.ExecstartactivitThe y method is used to start the activity based on intent. Remove some details. The most important thing it does is to pass the callActivitymanagerservice.
As mentioned above,System Core LayerIn fact, the system core layer is responsible for some key Android transactions.A group of services. They also run on virtual machines, which are consistent with the implementation mechanism of common services, but they do not show their faces and are only silently serving lower-layer services, so they are the core. Acitivitymanagerservice is a service responsible for activity scheduling. It may be involved in later scheduling details. Here, acitivitymanagerservice completes the operation in two steps. First, it submits the intent to another service.PackagemanagerserviceThe Service obtains information about the entire software package and its components. It matches the passed intent with all the known intent filters (if it carries component information, no need to compare ...), after finding this information, we will notify the relevant component information back to acitivitymanagerservice. Here, we will complete the many details of starting the activity. From this we can see that starting an activity requires processing of multiple services, which is not very lightweight. In the android random document, we will evaluate the performance. However, this operation is not the kind that will be repeatedly tortured in the loop. Therefore, the overall effect is worth the money compared with the performance price paid.