[Android development diary in a row from scratch] (11)-Android Intent mechanism, androidintent
Let's continue our Android journey. Today, I want to introduce the Android Intent.
For component-based application development, you not only need to construct and find components that meet your needs, but also need to organically connect the components and exchange information, to provide the services that the application expects. To achieve better Component Reuse and make full use of the capabilities of each component, the connection mode of these components must be flexible and unified, and can be dynamically expanded. Therefore, Android allows you to establish connections using Intent objects and implement component communication. The Intent object is the core of Android component connection, which embodies the design idea of the entire component connection model.
1. Intent object
The core design concept of Android's Intent mechanism is to introduce the Component Management Service as the manager for connecting components. This service can understand the categories and functions of each component in the system through the component configuration information, so as to help call components to find implementation components that meet their needs, and completely decouple callers and implementers.
General Intent implementation flowchart:
Ii. Intent Filter object
In addition to Intent objects, there is also an important role in the Android Intent mechanism: Intent Filter object. Intent Filter object refers to the android. content. IntentFilter class object, which is the "sister object" of the Intent object ". Similar to the Intent object, the Intent Filter object also contains Action, Type, Data, Catagory, and other Data items. The structure and meaning of each Data item also correspond to the Data items of the Intent.
Iii. Intent Matching Algorithm
When the Component Management Service receives the Intent object of the requested Component, it first checks whether the Intent object contains the Component information of the target implementation Component. If this parameter is not specified, the Intent Filter information of all components is obtained from the application management service, and the Intent object is compared with the Intent object. It involves the Intent matching algorithm.
The algorithm inputs the Intent object to be compared with the Intent Filter object. If the return value is positive, the match is successful. The greater the positive value, the higher the matching degree. The flowchart is as follows.
Among them, Data and Type information are the most complex Data items in Intent Filter. The comparison algorithm is the key to determine the matching degree between Intent and Intent Filter objects.
If the Intent object contains the Type information, the Type information of the Intent Filter must correspond to it. Otherwise, the matching will also end with a failure. Then, if the Intent object contains Data items, the URI information of the Data item is split into Scheme and Authority, and compared with the corresponding part of the Intent Filter object one by one, as long as there is any discrepancy between the two, matching will fail.
Taking the Android native email application as an example, it configures the following Intent Filter information:
<Activity android: name = ". secondActivity. messageCompose "> <intent-filter> <action android: name =" android. intent. action. VIEW "/> <action android: name =" android. intent. action. SENDTO "/> <data android: scheme =" mailto "/> <category android: name =" android. intent. category. DEFAULT "/> <category android: name =" android. intent. category. BROWSABLE "/> <! -- You can continue to add related actio, category, and data items --> </intent-filter> <! -- You can continue to add related intent-filter items --> </activity>
When the caller sends the following request, an intent match is triggered:
Intent intent = new Intent (Intent. ACTION_SENDTO); intent. putExtra (Intent. EXTRA_TEXT, ""); // Where, mailto is the scheme part of URI intent. setData (Uri. parse ("mailto: xxx@oo.com"); startActivity (intent );
Start matching check. ACTION contains OK-> data contains OK-> Category check (send a request through the Context. startActivity function, the component management service automatically adds Intent. CATEGORY_DEFAULT) OK-> match successful.
4. Select matching Components
If multiple Intent Filter objects match the Intent objects generated by the call component, Filter them in all Intent Filter objects that meet the conditions, select the implementation components that best meet the call components and user requirements. This is the choice of matching components. However, you can set the priority of the Intent Filter object, that is, you can change the android: priority attribute in the <intent-filter> configuration item, or modify it dynamically using the IntentFilter. setPriority function. The value range is-1000 to 1000. The default value is 0.
In the Intent mechanism of Android, by introducing third-party Component Management Services, the coupling between calling components and implementing components is reduced, and the flexibility and reusability of the entire system are improved, this makes application development easier and faster. At the same time, the third-party service intervention increases the cost of connecting components, which may make calls between components not smooth enough. Therefore, in the component management service, the system has greatly optimized the component matching and selection process to improve the component calling efficiency, including:
(1) Index
The Component Management Service uses a hash table to index the actions, types, and other data items of all Intent Filter objects. Each index item corresponds to a set of Intent Filter objects with the same Action, Type, or other data items. Intent first compares with the index item to quickly select an Intent Filter object that may match the Intent. In this way, the matching speed is accelerated.
(2) Cache
The so-called cache records the matching results of Intent and Intent Filter. When the same Intent is called, the results of the last record can be directly returned, thus skipping the matching process, call the acceleration component.
5. A small example of Intent usage
PS: For details about how PackageManager is used, refer to this article: Android essay-PackageManager
Vi. Conclusion
Do you understand the Intent transfer principle here. In an image, Intent is like a plug, while Intent Filter is like a socket. The socket hole can work as long as it meets the number of holes and feet of the plug. If it is more, it will be okay, but it will not work if it is less.
Intent is the core of the Android Application Model and solves the connection problem between components. The Intent matching policy provided by the Component Management Service Reduces the coupling between components, improves the flexibility of the platform, enhances the reusability of components, and fundamentally reduces the burden of application development.
(1) android Intent mechanism detailed explanation http://www.oschina.net/question/565065_67909
(2) android intent and intent action book http://www.apkbus.com/android-72361-1-1.html
==========================================================
Author: cpacm
Address: http://www.cnblogs.com/cpacm/p/3939937.html
Why is it so difficult to find someone who loves me?
Find a person you love first. If that person only loves you, but you don't love him, what does this mean?
You should first learn to love others, love him wholeheartedly, and then infect him with your love, so that he will fall in love with you.
Love is a thing for two people. If you love him or love you, you can always go forever?
I am so tired that I have no taste of love.