1. Intent object details
Android applications include three important components: Activity, service, and broadcastreceiver. The applications start them in the same way-all of which are started by intent, intent encapsulates the intention of the program to start the program. In addition, intent is also used to exchange information with the started component.
Component Type |
Start Method |
Activity |
Startactivity (intent) Startactivityforresult (intent, intrequestcode) |
Service |
Componentname startservice (intent Service) Boolean bindservice (intent service, serviceconnection Conn, int flags) |
Broadcastreceiver |
Sendbroadcast (intent) Sendbroadcast (intent, string receiverpermission) Sendorderedbroadcast (intent, string receiverpermission, broadcastreceiver resultreceiver, Handler scheduler, int initialcode, string initialdata, bundle initialextras) Sendorderedbroadcast (intent, string receiverpermission) Sendstickyorderedbroadcast (intent, broadcastreceiver resultreceiver, Handler scheder, int initialcode, string initialdata, bundle initialextras) |
The intent object contains seven attributes: component, action, category, Data, type, extra, and flag. The component is used to specify the target component to be started, extra is used to "carry" the data to be exchanged.
2. Intent attributes and intent-filter configuration
(1) component attributes
The Component Attribute of intent needs to accept a componentname object. The application can start a specific component based on the given component class.
When a program uses the Component Attribute of Intent (specifying which component to start) to start a specific component, the <intent-filter.../> element is almost unnecessary for the started component.
(2) Action, category attribute and intent-filter configuration
The action and category attributes of intent are common strings. Action indicates an abstract "action" to be completed by the intent. category is used to add additional category information to the action. Generally, action and category.
The <intent-filter.../> element usually contains the following sub-elements:
A, 0 ~ N <action.../> sub-elements
B, 0 ~ N <category.../> sub-elements
C, 0 ~ 1 <data.../> sub-element
The configuration of <action.../> <category.../> sub-elements is very simple. You can specify the Android: Name attribute. The value of this attribute is a common string.
When <activity... /> <intent-filter... /> the sub-element contains multiple <action... /> when the sub-element (equivalent to specifying multiple strings), it indicates that the activity can respond to the intent with the action attribute value of any of the strings.
An intent object can only contain one action attribute. The program calls the setaction (string Str) method of intent to set the action attribute value. However, an intent object can contain multiple category attributes, call the addcategory (string Str) method of intent to add.
When the program creates an intent, the intent starts the component whose category attribute value is intent. category_default constant (the constant value is Android. Intent. Category. Default) by default.
(3) specify action and category to call the system activity
In fact, Android provides a large number of standard action and category constants. The standard action constants and corresponding strings used to start the activity are as follows:
Action constant |
Corresponding string |
Simple Description |
Action_main |
Android. Intent. Action. Main |
Application Portal |
Action_view |
Android. Intent. Action. View |
Display specified data |
Action_attach_data |
Android. Intent. Action. attach_data |
Specify that a piece of data will be appended to other places |
Action_edit |
Android. Intent. Action. Edit |
Edit specified data |
Action_pick |
Android. Intent. Action. Pick |
Select an item from the list and return the selected data |
Action_chooser |
Android. Intent. Action. chooser |
Display an activity Selector |
Action_get_content |
Android. Intent. Action. get_content |
Let the user select data and return the selected data |
Action_dial |
Android. Intent. Action. Dial |
Display dial panel |
Action_call |
Android. Intent. Action. Call |
Directly call a specified user |
Action_send |
Android. Intent. Action. Send |
Send data to others |
Action_sendto |
Android. Intent. Action. sendto |
Send messages to others |
Action_answer |
Android. Intent. Action. Answer |
Answer phone number |
Action_insert |
Android. Intent. Action. insert |
Insert data |
Action_delete |
Android. Intent. Action. Delete |
Delete data |
Action_run |
Android. Intent. Action. Run |
Operation and Maintenance |
Action_sync |
Android. Intent. Action. sync |
Execute Data Synchronization |
Action_pick_activity |
Android. Intent. Action. pick_activity |
Used to select activity |
Action_search |
Android. Intent. Action. Search |
Execute search |
Action_web_search |
Android. Intent. Action. web_search |
Execute Web Search |
Action_factory_test |
Android. Intent. Action. factory_test |
Factory test entry point |
The standard category constant and corresponding strings are as follows:
CATEGORY constant |
Corresponding string |
Simple Description |
Category_default |
Android. Intent. Category. Default |
Default category |
Category_browsable |
Android. Intent. Category. browsable |
Specify that the activity can be safely called by the browser |
Category_tab |
Android. Intent. Category. Tab |
Specify activity as the tab page of tabactivity |
Category_launcher |
Android. Intent. Category. Launcher |
Activity displays the list of top-level programs |
Category_info |
Android. Intent. Category. Info |
Used to provide package information |
Category_home |
Android. Intent. Category. Home |
Set the activity to run with the system startup |
Category_preference |
Android. Intent. Category. Preference |
This activity is a parameter panel |
Category_test |
Android. Intent. Category. Test |
This activity is a test |
Category_car_dock |
Android. Intent. Category. car_dock |
Run the activity when the mobile phone is inserted into the car base (hardware ). |
Category_assist_dock |
Android. Intent. Category. wash_dock |
Run the activity when the mobile phone is inserted into the desktop base (hardware ). |
Category_car_mode |
Android. Intent. Category. car_mode |
You can set this activity to be used in a vehicle-mounted environment. |
3. Data and type attributes and intent-filter configuration
The data property is usually used to provide operation data to the Action property. The data property receives a URI object, which is usually represented by a string in the following format:
Content: // com. Android. Contracts/contacts/1
Tel: 123
The colon of the preceding two strings roughly specifies the data type, followed by the data part.
The Type attribute is used to specify the data type or MIME type specified by the data attribute. When the intent does not specify the data attribute, the type attribute takes effect, otherwise, the android system will analyze the data type based on the data property value, so you do not need to specify the type attribute.
Once the action and data attributes are specified for intent, android can start a specific application based on the specified data type and perform corresponding operations on the specified data. The following describes the combination of several actions and data attributes:
Action_view content: // com. Android. Contacts/contacts/1: displays the contact information marked as 1.
Action_edit content: // com. Android. Contacts/contacts/1: edit the contact information marked as 1
Action_dial content: // com. Android. Contacts/contacts/1: display the interface for dialing contacts marked as 1
Action_view Tel: 123: display the interface for dialing to the specified number 123
Action_dial Tel: 123: display the interface for dialing to the specified number 123
Action_view content: // contacts/people/: displays information about all contacts. This combination allows you to conveniently view system contacts.
4. Extra attributes
The extra attribute of intent is usually used for data exchange among multiple actions. The extra attribute value of intent should be a bundle object, and the bundle object is like a map object, it can store multiple key-value pairs, so that you can exchange data between different activities through intent.