Intent in Android

Source: Internet
Author: User

An android application consists of four components. For details about these four components, see "Composition of Android Applications ".

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 actions, actions involving data, and additional data of an application. Android identifies the corresponding components based on the description of the intent, pass intent to the called component and complete the call of the component.

Therefore, intent acts as a media intermediary here, providing information about component calls to each other to decouple callers from callers.

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 that we want to perform the "View" action. The corresponding viewing object of this action is "a contact ", call startactivity (intent ),

The constructed intent is passed in. The system will find the activity that meets the intent requirements in manifest according to the description in this intent. The system will call the activity that is found, that is, detailactivity, and finally pass in the intent, detailactivity performs the corresponding operation based on the description in the intent.

1. abstract description

In the android reference document, intent is an abstract description (indeed abstract) of an operation ). Let's take a look at the abstract description.

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 for us:

Copy content to clipboard

Code :

Main_action
View_action
Edit_action
Pick_action
Get_content_action
Dial_action
Call_action
Sendto_action
Answer_action
Insert_action
Delete_action
Run_action
Login_action
Clear_credentials_action
Sync_action
Pick_activity_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.

SecondIs the data to be operated. 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 documentation 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:

Copy content to clipboard

Code:

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
Pick_action content: // contacts/-- displays a list of all contacts, allows users to select a contact in the list, and then returns this contact to the parent activity. For example, the email client can use this intent and requires the user to select a contact from the contact list.
In addition to the two important attributes of action and data, there are also some additional attributes:

Category (category), additional information about the executed action. For example, launcher_category indicates that the receiver of intent should appear as a top-level application in launcher, while alternative_category indicates that the current intent is one of a series of optional actions that can be executed on the same piece of data.

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.

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.

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.
As applications are constantly added to the system, they can add new actions, data/type, and category to extend the language.
Applications can also provide their own activities to process existing "phrases" and change the behavior of these "phrases.

Ii. How to parse intent in Android

In applications, we can use intent in two forms:

Intent: Specifies the intent of the Component Attribute (call setcomponent (componentname) or setclass (context, class ). Notify the application to start the corresponding component by specifying a specific component class.

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 already clear. Android needs to parse the indirect intent, map intent to the activity, intentreceiver, or service that can process the intent.

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.

3. Application Example

The following uses an example in the android SDK to describe how intent is defined and parsed. This application allows users to browse the notepaper list and View Details of each notepaper.

XML Code

Copy content to clipboard

Code:

<Manifest
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. Google. Android. Notepad">
<Application
Android: icon = "@ drawable/app_notes"
Android: Label = "@ string/app_name">

<Provider
Class = "notepadprovider"
Android: Authorities = "com. Google. provider. Notepad"
/>

<Activity
Class = ". noteslist"
Android: Label = "@ string/title_notes_list">
<Intent-filter>
<Action
Android: value = "android. Intent. Action. Main"
/>
<Category
Android: value = "android. Intent. Category. launcher"
/>
</Intent-filter>
<Intent-filter>
<Action
Android: value = "android. Intent. Action. View"
/>
<Action
Android: value = "android. Intent. Action. Edit"
/>
<Action
Android: value = "android. Intent. Action. Pick"
/>
<Category
Android: value = "android. Intent. Category. Default"
/>
<Type
Android: value = "Vnd. Android. cursor. DIR/vnd. Google. Note"
/>
</Intent-filter>
<Intent-filter>
<Action
Android: value = "android. Intent. Action. get_content"
/>
<Category
Android: value = "android. Intent. Category. Default"
/>
<Type
Android: value = "Vnd. Android. cursor. Item/vnd. Google. Note"
/>
</Intent-filter>
</Activity>

<Activity
Class = ". noteeditor"
Android: Label = "@ string/title_note">
<Intent-Filter
Android: Label = "@ string/resolve_edit">
<Action
Android: value = "android. Intent. Action. View"
/>
<Action
Android: value = "android. Intent. Action. Edit"
/>
<Category
Android: value = "android. Intent. Category. Default"
/>
<Type
Android: value = "Vnd. Android. cursor. Item/vnd. Google. Note"
/>
</Intent-filter>
<Intent-filter>
<Action
Android: value = "android. Intent. Action. Insert"
/>
<Category
Android: value = "android. Intent. Category. Default"
/>
<Type
Android: value = "Vnd. Android. cursor. DIR/vnd. Google. Note"
/>
</Intent-filter>
</Activity>

<Activity
Class = ". titleeditor"
Android: Label = "@ string/title_edit_title"
Android: theme = "@ Android: style/theme. Dialog">
<Intent-Filter
Android: Label = "@ string/resolve_title">
<Action
Android: value = "com. Google. Android. notepad. Action. edit_title"
/>
<Category
Android: value = "android. Intent. Category. Default"
/>
<Category
Android: value = "android. Intent. Category. Alternative"
/>
<Category
Android: value = "android. Intent. Category. selected_alternative"
/>
<Type
Android: value = "Vnd. Android. cursor. Item/vnd. Google. Note"
/>
</Intent-filter>
</Activity>

</Application>

 

</Manifest>First ActivityIs com. Google. Android. notepad. noteslist. It is the main portal of the application and provides threeFunction, Which are described by three intent-filters:

1. The first entry is to enter the top-level entrance of the notepaper application (the action is Android. App. Action. Main ). Android. App. Category. launcher indicates that the activity will be listed in launcher.

2. The second is when the type is vnd. android. cursor. DIR/vnd. google. note, you can view the available notes (the action is Android. app. action. view), or let the user select a note and return it to the caller (the action is Android. app. action. pick ).

3. The third is when the type is vnd. android. cursor. item/vnd. google. note, return a selected note to the caller (the action is Android. app. action. get_content), but the user does not need to know where to read the note. With these functions, the following intent will be parsed to the activity noteslist:

Copy content to clipboard

Code:

{Action = Android. App. Action. Main}: the activity that matches this intent will be treated as the top-level entry to the application.

{Action = Android. App. Action. Main, Category = Android. App. Category. launcher}: The intent actually used by the launcher to generate the top-level list of the launcher.

{Action = Android. App. Action. View data = content: // com. Google. provider. notepad/Notes }:

Displays the list of all notebooks under "content: // com. Google. provider. notepad/Notes". You can traverse the list and view the details of a notepaper.

{Action = Android. App. Action. Pick DATA = content: // com. Google. provider. notepad/Notes }:

Show "content: // COM. google. provider. note list under notepad/Notes allows you to select one in the list, and then return the selected note URL to the caller.

{Action = android. app. action. get_content type = vnd. android. cursor. item/vnd. google. note}: similar to the intent in which the preceding action is pick, the intent allows callers to specify the data types they need to return (in this case, the activity that calls noteslist, the system will find the appropriate activity based on the data type (here the system will find the activity noteslist) for the user to choose a note.
The second activity is com. Google. Android. notepad. noteeditor. It displays a note for the user and allows the user to modify it.

It defines two intent-filters, so it has two functions.

The first feature is when the data type is vnd. android. cursor. item/vnd. google. note, you can view and modify a note (the action is Android. app. action. view and Android. app. action. edit ).
The second feature is that when the data type is vnd. android. cursor. DIR/vnd. google. note: displays a new note interface for the caller and inserts the new note into the note list (the action is Android. app. action. insert ).
With these two functions, the following intent will be parsed to the activity noteeditor:
Copy content to clipboard
Code:
{Action = Android. App. Action. View data = content: // com. Google. provider. notepad/Notes/{ID}: displays the user with a note marked as ID.

{Action = Android. App. Action. edit data = content: // com. Google. provider. notepad/Notes/{ID}: allows users to edit the notepaper marked as ID.

{Action = android. app. action. insert data = content: // COM. google. provider. notepad/Notes}: In "content: // COM. google. provider. notepad/Notes "creates a new blank note in the notepad list and allows users to edit the note. After the user saves the note, the URI of the new note will be returned to the caller.
The last activity is com. Google. Android. notepad. titleeditor, which allows users to edit the title of the notepaper.

It can be implemented as a class that an application can call directly (explicitly set the Component Attribute in intent). However, here we will provide you with a method to publish optional operations on existing data.

In the unique intent-filter of this activity, a private action: COM. Google. Android. notepad. Action. edit_title indicates that you are allowed to edit the title of the notepaper.

Like the previous view and edit actions, when calling this intent, you must specify a specific note (type: VND. Android. cursor. Item/vnd. Google. note ). The difference is that only the title in the note data is displayed and edited here.

In addition to the default category (Android. Intent. Category. Default), the title editor also supports two standard categories: Android. Intent. Category. Alternative and
Android. Intent. Category. selected_alternative.

After these two categories are implemented, other activities can call queryintentactivityoptions (componentname, intent [], intent, INT) to query the action provided by this activity, without the need to know its specific implementation;

Alternatively, call addintentoptions (INT, Int, componentname, intent [], intent, Int, menu. item []) to create a dynamic menu. It should be noted that the intent-filter has a clear name (specified by Android: Label = "@ string/resolve_title"). When users browse data, if this activity is an optional data operation, specifying a specific name can provide users with a better control interface.

With this function, the following intent will be parsed to the activity titleeditor:
Copy content to clipboard
Code:
{Action = com. google. android. notepad. action. edit_title DATA = content: // COM. google. provider. notepad/Notes/{ID }}: displays and allows users to edit the title of the notepaper identified as ID.

 

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.