Android self-use-intent Introduction

Source: Internet
Author: User

Reprinted!

Intent is a run-time binding mechanism that connects two different components while the program is running. With intent, your program can express a request or willingness to Android. Android selects appropriate components based on the content you want to complete the request. For example, if an activity wants to open a webpage browser to view the content of a webpage, the activity only needs to send a web_search_action request to Android. Android will send a request based on the intent content, query the intentfilter declared when each component is registered, and find the webpage browser activity to browse the webpage.

 

The intent object abstracts the operations to be executed. The basic content of the description can be divided into component name, action, data, and category), extra (additional information) and flag (FLAG) 6 sections, the following is a detailed introduction.

(1) component name refers to the name of the intent target component. The component name is a componentname object, which is a combination of the class name of the target component and the package name of the application where the target component is located. The package name does not have to match the manifest file.

The package name in exactly matches. The component name is optional. If the intent message specifies the name of the target component, this is an explicit message, which is passed to the specified component. If the target component name is not specified, Android uses intent

And the registered intentfilter to select the appropriate target component.

(2) Action refers to the string that describes the name of the Action triggered by intent. For broadcast intent, action refers to the action that is broadcasted. Theoretically, action can be any string, and Action strings related to Android applications are defined in the intent class as static string constants. Lists the actions of activity action intent that are common in the current Android system.

 

  • Action_call calls out the specified phone number in data.
  • Action_edit open the application corresponding to the specified data in data editing
  • Action_main main program portal, does not receive data, and does not return data after completion
  • Action_sync synchronizes data between the Android platform and the server
  • Action_view based on different data types, open the corresponding application to display data
  • Action_dial starts dialer or other Dialers and displays the specified phone number in Data
  • Action_sendto send data to the destination address described in Data
  • Action_time_tick broadcast triggered every minute of the system time
  • Action_time_changed system time changed through settings
  • Action_timezone_changed Time Zone change
  • Action_boot_completed system started
  • Action_package_added: the new application APK package has been installed.
  • Action_package_changed existing application APK package changed
  • Action_package_removed existing application APK package deleted
  • Action_uid_removed user ID deleted

 

 

(3) Data describes the data Uri and data type that intent wants to operate on. Some actions need to process the corresponding data. For example, for action_edit, its data can be editable Uris such as contacts and short messages. For

For action_call, its data can be a Tel: // format phone number Uri. Setting intent data correctly is important for Android to find components that match the intent request in the system. If you use action_call, but your data is set to a URI in the mailto: // format, then, the expected "Start call application" action will not be executed because there is no corresponding application. So every time we use intent, we should pay attention to the data types and formats related to the set action.

(4) category is an additional description of the requested component. Android also defines a set of static string constants in the intent class to indicate different intent classes.

(5) lists commonly used category constants.

 

  • The target activity of category_browsable can be activated by clicking a link in the Web browser (for example, clicking an image link in the browser)
  • Category_gadget indicates that the target activity can be embedded in other activities.
  • The target activity of category_home is home activity, that is, activity displayed after the mobile phone is started, or activity displayed after the home key is pressed.
  • Category_launcher indicates that the target activity is the activity with the highest priority in the application.
  • Category_preference indicates that the target activity is a preference activity.

 

 

 

(5) extra when we use intent to connect different components, we sometimes need to append additional information to intent to pass data to the target activity. For example, action_timezone_changed requires additional information to indicate the new time zone. Extra stores the key-Value Pair structure in the intent object. The intent object stores and obtains extra by calling the methods putextras () and getextras. Extra is saved as a bundle object. The bundle object provides a series of put and get methods to set and extract the corresponding key value information. The intent class also defines static string constants for the key values of some actions appended to Android applications.

 

 

 

 

Extra_bcc is a string array containing the BCC address

Extra_cc is a string array containing the CC address

Extra_email: array of strings containing the email sending Address

Extra_intent: Specifies the key with the intent option when action_pick_activity is used.

Extra_key_event the keyevent object that triggers the intent button

Extra_phone_number indicates the key of the telephone number string when the call-related action is used. The type is string.

Extra_shortcut_icon

...

 

 

Intent, which clearly specifies the name of the target component, is called "explicit intent ". Intent that does not explicitly specify the name of the target component is called "implicit intent ". Android uses intent filter to find objects related to hidden intent.

 

Explicit intent uses the component name to define the target component directly. However, because developers often do not know the component names of other applications, explicit intent is more used to transmit messages within the application. For example

In an application, an activity starts a service. Implicit intent, on the contrary, does not define the target component to be activated using the component name. It is more widely used to transmit messages between different applications.

 

 

After understanding the concepts of explicit intent and implicit intent, let's take a look at the factors that determine the intent target component. In an explicit intent message, the only element that determines the target component is the component name. Therefore, if your intent has clearly defined the name of the target component, you do not need to define other intent contents. The implicit intent is different. Because there is no clear target component name, the android system must help the application find the component that best matches the intent request intent. The specific selection method is: Android compares the intent request content with a filter called intent filter, which contains all possible components to be selected in the system. If a component of intent filter matches the content of the implicit intent request, Android selects this component as the target component of the implicit intent.

 

How does Android know that an application can process some kind of intent requests? This requires the application to declare the filter of its own components in androidmanifest. XML (that is, intent requests that can be matched ). No sound

The explicit intent filter component can only respond to explicit intent requests that specify its own name, but cannot respond to implicit intent requests. A component that declares the intent filter can respond to both explicit intent requests and implicit intent requests. When parsing implicit intent requests by comparing with intent filter, Android uses the following three factors as the reference for selection.

? Action

? Data

? Category

However, entra and flag do not work when parsing and receiving intent.

 

 

 

 

 

 

Intent Filter

The application components can declare one or more intent filters to notify Android users of the implicit intent requests they can respond to and process. Each intent filter describes the ability of the component to respond to intent requests-the type of request behavior and type of request data that the component wants to receive. For example, in the example of requesting a Web browser, the intent filter of the Web browser program should declare that the intent action it wants to receive is web_search_action, and the request data is in the url uri format.

How to declare your intent filter for the component? A common method is to use the attribute <intent-filter> In the androidmanifest. xml file to describe the intent filter of the component.

As mentioned above, the three elements used for comparison between implicit intent and intent filter are intent action, data, and category. In fact, an implicit intent request must be passed to the target component through these three checks. Android will not pass the hidden intent to the target component if it does not match any aspect. Next we will explain the specific rules for these three checks.

1. Action Test

The <intent-filter> element can contain sub-elements <action>, for example:

<Intent-filter>

<Action Android: Name = "com. example. Project. show_current"/>

<Action Android: Name = "com. example. Project. show_recent"/>

<Action Android: Name = "com. example. Project. show_pending"/>

</Intent-filter>

A <intent-filter> element must contain at least one <action>. Otherwise, no intent request can match the <intent-filter> element.

If the action requested by intent matches a <action> in <intent-filter>, the intent passes the <intent-filter> action test.

If the action type is not specified in the intent request or <intent-filter>, the following two cases may occur.

 

(1) If <intent-filter> does not contain any action type, no intent request can match <intent-filter>;

(2) If the intent request does not set the action type, as long as <intent-filter> contains the action type, this intent request will pass the <intent-filter> behavior test smoothly.

 

2. Category Test

The <intent-filter> element can contain <Category> sub-elements, such:

<Intent-filter...>

<Category Android: Name = "android. Intent. Category. Default"/>

<Category Android: Name = "android. Intent. Category. browsable"/>

</Intent-filter>

The intent request will pass the test only when all the category in the intent request matches the <Category> of an intentfilter in the component, the redundant <Category> declaration in intentfilter does not cause a matching failure. An intentfilter that does not specify any category for testing only matches intent requests that do not set a category.

3. Data Testing

The description of data in <intent-filter> is as follows:

<Intent-filter...>

<Data Android: TYPE = "Video/MPEG" Android: Scheme = "HTTP".../>

<Data Android: TYPE = "audio/MPEG" Android: Scheme = "HTTP".../>

</Intent-filter>

The <DATA> element specifies the data Uri and Data Type of the intent request to be accepted. The URI is divided into three parts for matching: Scheme, authority, and path. The URI data type and scheme of the inteat request set with setdata () must be consistent with the one specified in intentfilter. If authority or path is also specified in intentfilter, they also need to match to pass the test.

After explaining the basic concepts of intent, we will use intent to activate the phone dialing program that comes with Android. Through this instance, you will find that using intent is not as difficult as its concept.

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.