[Switch] intent Parsing

Source: Internet
Author: User

 

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 ...), one function is to add attachments to emails. For example, if you want to send an image bubble mm via 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:

  1. 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.
  2. 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: // xxxx
    This 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.
  3. 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, if you call an application, the action is ACTION_DIAL and the data is tel: xxx.
    Intent, 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.
  4. 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:Every time I eat itI want to make three push-ups. 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 Activity of the application (that is, the first Activity that runs when it is started independently) must be able to accept a Category
    CATEGORY_LAUNCHER, and Action is the intent of ACTION_Main.
  5. 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. This mode is called in Android.Implicit IntentsThis mode provides a flexible and scalable mode that gives users and third-party applications the right to choose. 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 there is no other picture selection program in the mobile phone, you can continue to use the default mailbox, you can choose to replace the full functionality of the original module. 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 calledExplicit IntentsYou need the help of Component. Component isClass Name, Complete, suchCom. xxxxx. xxxx
    Once it is specified, everything is clear and you can find this class (of course, it will be a specific subclass...). It succeeds, and vice versa. The advantage is speed. It is suitable for you to use it when you know it is an internal module.
  6. 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 isBundle
    Class 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.
  7. 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 Filters
It 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 be dynamically inserted and inserted in the code. Broadcast 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, yesInstrumentation
Object. 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. We will introduce the performance in the Android random document.
This is an evaluation. 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.

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.