Android intent: Basic Concepts

Source: Internet
Author: User

An android application consists of four components: Activity, intent, service, and content provider. These four components are independent and can be called and coordinated to form a real Android Application. The communication between these components is mainly completed with intent assistance.

Intent describes the action and action data involved in an application operation. Android identifies the corresponding component based on the description of the intent and transmits the intent to the called component, and complete the call of the component.
For example, in an application maintained by a contact, when we click a contact on a contact list screen (assuming the corresponding activity is listactivity, you want to jump out of the contact's detail screen (assuming the corresponding activity is detailactivity). To achieve this purpose, listactivity needs to construct an intent, which is used to tell the system, we need to perform the "View" action. The object corresponding to this action is "a contact" and startactivity (intent, intent) is called. The system will follow the description in this intent, the activity found by the call is the detailactivity that performs the corresponding operation.
(1) abstract description
In the android reference document, intent is an abstract description of an operation. Let's take a look at the abstract description here. First, it is a brief description of the action to be executed, such as view_action (View) and edit_action (modify). Android defines a set of standard actions (partial ):
Android. Intent. Action. main_action
Android. Intent. Action. view_action
Android. Intent. Action. edit_action
Android. Intent. Action. pick_action
Android. Intent. Action. get_content_action
Android. Intent. Action. dial_action
Android. Intent. Action. call_action
Android. Intent. Action. sendto_action
Android. Intent. Action. answer_action
Android. Intent. Action. insert_action
Android. Intent. Action. delete_action
Android. Intent. Action. run_action
Android. Intent. Action. login_action
Android. Intent. Action. clear_credentials_action
Android. Intent. Action. sync_action
Android. Intent. Action. pick_activity_action
Android. Intent. Action. web_search_action
In addition, we can also define our own actions based on the needs of the application, and define the corresponding activity to process our custom actions.
Second, it is the data to be operated by the execution action. Android uses a URI pointing to the data. For example, in a contact application, a URI pointing to a contact may be: content: // contacts/1. This type of Uri representation is described through the contenturi class. For details, refer to the document of the android.net. contenturi class. Take the Contact application as an example. The following are some action/data pairs and their intention to express them:
View_action content: // contacts/1 -- display the details of the contact with the identifier "1"
Edit_action content: // contacts/1 -- edit the contact details with the identifier "1"
View_action content: // contacts/-- display the list of all contacts
In addition to the two important attributes of action and data, there are also some additional attributes:
A, category (category), additional information of the executed action. For example, launcher_category indicates that the receiver of intent should appear as a top-level application in launcher.
B, type (data type), explicitly specifying the intent data type (MIME ). Generally, the intent data type can be determined based on the data itself. However, by setting this attribute, You can forcibly use the explicitly specified type instead of derivation.
C, component (component), specifies the Class Name of the intent's target component. Generally, Android searches for other attributes in intent, such as action, data/type, and category, and finds a matched target component. However, if this attribute of component is specified, the component specified by component will be used directly without executing the above search process. After this attribute is specified, all other intent attributes are optional.
D, extras (additional information) is a collection of all other additional information. You can use extras to provide extended information for components. For example, if you want to perform the "send email" action, you can save the email title and body in extras, send to the email sending component.
In short, action, data/type, category, and extras form a language. This language enables the system to understand phrases such as "View Details of a contact.
(2) How to parse intent in Android
In applications, we can use intent in two forms:

A, direct intent: Jump with the class name, use setclass (context, class ). During intent transmission, find the target consumer (another activity, intentreceiver or service), that is, the responder of intent.

Activity jump segment:

Intent intent = new Intent();intent.setClass(Activity01.this, Activity02.class);startActivity(intent);

Service jump segment:

        mContext = this;        Intent i = new Intent();        i.setClass(mContext, SmartStayService.class);        mContext.startService(i);

You can also use the following method to declare the service in XML.

        startService(new Intent("org.allin.android.facedetcting"));

B. Indirect Intent: the intent of the Comonent attribute is not specified. The intent must contain sufficient information so that the system can determine the components that meet the intent among all available components based on the information. For a direct intent, Android does not need to parse it, because the target component is clear, and Android needs to parse the indirect intent, the intent is mapped to the activity, intentreceiver, or service that can process the intent through parsing.
The intent parsing mechanism mainly finds the matching intent by finding all intentfilters registered in androidmanifest. XML and the intent defined in them. In this parsing process, Android uses the intent action, type, and category attributes to determine the attributes. The judgment method is as follows:
If intent specifies an action, the action must be included in the intentfilter action list of the target component; otherwise, the action cannot be matched;
If the intent does not provide a type, the system obtains the data type from the data. Like action, the Data Type list of the target component must contain the intent data type. Otherwise, the data type does not match.
If the data in the intent is not a content: Type Uri and the intent does not explicitly specify its type, it will match according to the scheme (such as http: or mailto:) of the data in the intent. Similarly, the scheme of intent must appear in the scheme list of the target component.
If intent specifies one or more category categories, these categories must all appear in the Set category list. For example, intent contains two categories: launcher_category and alternative_category. The parsed target component must contain at least these two categories.

 

Http://blog.csdn.net/jinlking/archive/2009/05/06/4153254.aspx ()

Http://www.cnblogs.com/playing/archive/2010/09/15/1826918.html ()

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.