Intent Android details

Source: Internet
Author: User

Basic Components of intents and intent Filters
Activity, service, and broadcast Receiver -- are activated using messages called intent.

 

Intent message transmission is a mechanism for binding between components during runtime. intent is an intent object, which contains the description of the operation to be performed, or, for broadcast, contains the message content being notified. there is a different mechanism for sending intent to these three components:
Use context. startactivity () or activity. startactivityforresult () to input an intent to start an activity.
Use activity. setresult () to input an intent to return the result from the activity.
Send the intent object to context. startservice () to start a service or send a message to a running service.
Send the intent object to context. bindservice () to bind a service.
Send intent objects to broadcast methods such as context. sendbroadcast (), context. sendorderedbroadcast (), or context. sendstickybroadcast.

In the above three cases, the android system will find the appropriate activity, service, or broadcast receivers to respond to intent. intents of the three are independent from each other. intent objects intent object an intent object contains information about components that accept the intent (for example, required actions and data required for this action) and the information required by the Android system (such as the category of the component and how to start it ). specifically, the component name is a componentname object. it is the complete name of the target component (for example, "com. example. project. app. the package name (for example, "com. example. project. the package name of the former is not necessarily the same as that of the latter. the component name is optional. if it is set, the intent object will be passed to an instance of the specified class. if this parameter is not set, Android uses other information to locate the target. the component name is set by setcomponent (), setclass (), or setclassname () and obtained by getcomponent. action: A string that is used to name the requested action, or an action that is being reported for a broadcast intent. for example, the constant target component action action_call activity initiates a telephone call. action_edit activity displays data for users to edit. action_main activity starts the activity as the first activity of a task. It does not input parameters or expect the return value. action_sync activity synchronizes data on the device with a server. action_battery_low broadcast aggreger issues a warning of insufficient power. action_headset_plug broadcast receiver a headset is being inserted or pulled out. the action_screen_on broadcast comment er screen is lit. action_timezone_changed broadcast receiver er time zone settings changed. you can also define your own action string to start your application. the custom action should contain the package name of the application. for example, "com. example. project. show_color ". action largely determines the structure of another part of intent, just as a method name determines the parameters it accepts and the return value. therefore, it is best to give action a name that best reflects its role. actions in an intent object are read and written using getaction () and setaction. the URI of the data to be operated and Its mime (Multipurpose Internet Mail Extension, Multipurpose Internet Mail Extensions) type. for example, if action is action_edit, data will contain the data URI to be edited. if action is action_call, data is Tel: The URI of the phone number. if action is action_view, data is http: The URI of the network address. when an intent matches a component, the data type besides URI is also important. for example, a program for displaying images should not be used to process audio files. data Types can often be inferred from Uris, especially content: URI, which indicates that the data belongs to a content provider. however, the data type can also be declared by the intent object. the setdata () method sets the URI, while the settype () method specifies the MIME type. The setdataandtype () method sets the data Uri and MIME type. they can be read using getdata () and GetType. category is a string that contains information about the types of components that process the intent. an intent object can have any category. the intent class defines many category constants. For example, the constant indicates that the target activity of category_browsable can be displayed in a browser-sample or email message. category_gadget this activity can be included in another activity that loads gadgets. category_home the activity displays the main screen, that is, the interface that you can see by pressing the Home Key. category_launcher: this activity can be used as the first activity of a task and is listed in the application initiator. category_preference this activity is an option panel. the addcategory () method adds a category to an intent object, removecategory deletes a category, and getcategories () obtains all the category of the intent. extras is additional information in the form of key-value pairs. for example, the intent of action_timezone_changed has a "time-zone" additional information to indicate the new time zone, while action_headset_plug has a "state" additional information to indicate whether the headset is inserted or pulled out. intent object has a series of put... () and set... () method to set and obtain additional information. these methods are similar to bundle objects. in fact, additional information can be read and written using putextras () and getextras () as bundle. flags. many icons indicate how the Android system starts an activity (for example, which task the activity belongs to) and how to handle it after it is started (for example, whether it belongs to the latest activity list ). android and applications use intent objects to send System Broadcasts and activate system-defined components. intent resolution intent Parsing

Intent has two types: explicit intent uses the name to specify the target component. since the component name is generally not familiar to other developers, this kind of intent is generally used for internal messages in the application-for example, an activity starts an affiliated service or another activity. implicit intent does not specify the target name. it is generally used to start components of other applications. android sends an explicit intent to the specified class. the intent object whose name is unique determines that intent is accepted.

For implicit intent, the android system must find the most suitable component to process it. it compares the intent content with the intent filter. intent filter is a component's related structure, indicating its ability to accept intent. the Android system enables intent-acceptable components based on the intent filter. if a component does not have an intent filter, it can only accept explicit intent. if yes, both are acceptable. when comparing an intent filter with an intent filter, only three attributes are considered: Action, data, and category. extra and flag are useless in intent parsing. intent filters activity, service, and broadcast referers can have multiple intent filters to tell the system what kind of implicit intent they can accept. intent filter has a very good name: It filters intent that you don't want to accept, leaving intent that you want to accept.

Explicit intent ignores intent filter. A component has a separate filter for everything that can be done. for example, the noteeditor activity of the Notepad program has two filters: one to start and display a specific record for the user to view or edit, and the other to start an empty record for the user to edit. filters and security filter and security an intent filter are not necessarily safe and reliable. an application can accept an implicit intent for a component, but it cannot prevent the component from displaying the intent. other programs can always use custom data with explicit program names to call this component. an intent filter is an instance of the intentfilter class, but it generally does not appear in the code, but appears in the android manifest file Format. (one exception is that the intent filter of broadcast receiver uses context. registerreceiver () is dynamically set, and its intent filter is also created in the Code .) A filter has fields such as action, data, and category. an implicit intent must pass three tests in order to be accepted by an intent filter. to be accepted by a component, an intent must use one of all its intent filters. action Test ...One intent object can only specify one action, while one intent filter can specify multiple actions. the action list cannot be blank; otherwise, it organizes all intents. the action of an intent object must match an action in the intent filter to pass. if the action list of the intent filter is empty, the operation fails. if the intent object does not specify action and the Action List of the intent filter is not empty. CATEGORY Test Note that the constants of action and category mentioned above are used in code, rather than in manifest files. for example, the category_browsable constant is represented as "android. intent. category. browsable ". if an intent needs to pass the category test, each category in the intent object must match one of the filters. theoretically, if no category is specified for an intent object, it should be able to pass any category test. one exception is that android considers the implicit intent passed to startactivity () as at least one category: "android. intent. category. default ". therefore, to accept an implicit intent activity, you must add "android. intent. category. default ". ("android. intent. action. main "and" android. intent. category. the intent filter exception of launcher. they do not need "android. intent. category. default ".) data Test ...EachThe element specifies a URI and a data type. each part of Uri has different attributes: Scheme, host, port, path: Scheme: // host: Port/path. For example, in the following URI: Content: // COM. example. project: 200/folder/subfolder/etc scheme is "content", host is "com. example. project ", port is" 200 ", path is" folder/subfolder/etc ". the host and port together form URI authority. if the host is not specified, the port is ignored. these attributes are optional, but they are not independent of each other: To make an authority meaningful, you must specify a scheme. to make a path meaningful, you must specify a scheme and an authority. when the URI in the intent object is compared with the intent filter, it only compares with the part defined in the filter. for example, if scheme is defined in the filter, all intent objects containing the URI of the scheme will pass the test. for path, wildcards can be used for partial matching.The Type attribute of the element specifies the data type. it is more common in filter than in Uri. both intent object and filter can use the "*" wildcard as the child type. for example, "text/*" "audio/*" indicates that all child types match. the data test rules are as follows: an intent object without a URI or data type only uses a filter that does not contain both. an intent object with a URI but no data type (and data types cannot be inferred from the URI) can only be matched using the filter: URI, And the type is not specified. this case is limited to a URI similar to mailto: And Tel: That does not specify the actual data. an intent that only contains the data type but does not contain the URI only uses this filter: this filter only lists the same data type, and does not specify the URI. an intent object that contains both the URI and the data type matches the Data Type of the intent object with the one in the filter. The URI of the intent object must match the URI of the filter, either the URI of the intent object is content: or file:, and the filter does not specify the URI. if an intent can be filtered by more than one activity or service, you may be asked which one to start. if none of them exist, an exception is thrown. common cases common situations the last rule (d) described above shows that components can usually obtain data from files and content providers. therefore, their filters can only list data types without column scheme. this is a special case. belowThe element tells android that this component can retrieve image data from a content provider and display it:Because most of the available data is provided by the content provider, the filter that specifies the data type but does not specify the URI is the most common case. another common configuration is that the filter has a scheme and a data type. for exampleThe component tells android that the component can obtain and display image data from the network:Consider the browser action when a user clicks a webpage. it first tries to display the data (as an HTML page for processing ). if it cannot be displayed, create an implicit intent and start an activity that can process it. if there is no such activity, it requests the Download Manager to download the data. it then places the data under the control of a content provider, so that many activities (with only data-type filters) can process the data. most applications can be started independently without reference to any specific data. these activities that can start the application have the action as "android. intent. action. main "filter. if they need to be displayed in the app launcher, they must specify "android. intent. category. launcher "category. The first activity, notelist, is different from other activities because it operates the directory (note list) of a note instead of a separate note. it is generally used as the initial interface of the program. it can do the following three things:

Related Article

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.