SoFunction
Updated on 2025-04-11

Summary of Intent Usage in Android Development

This article describes the usage of Intent in Android development. Share it for your reference, as follows:

In Android mobile phone software development, Intent is an important object in mobile phone software development that needs to attract our attention. In fact, intent is also an iconic object that reflects the uniqueness of Android development.

When an activity wants to start another activity, perhaps a pattern that was familiar before was: call a new function, directly create an object with a window feature class, or directly call a startup function to start. This method is concise and clear, but it goes against the concept of Android development. Android uses Intent to "encapsulate" the program's "call intention". No matter what component the program wants to start, whether it is an Activity, a service, or a Broadcast Receiver, Android uses Intent objects to encapsulate this "start intention".

In addition, using Intent also has benefits. Sometimes, the application just wants to start components or applications with certain characteristics, but does not want to have rigid code coupling with such programs; in addition, the application sometimes may only know some characteristics of the thing to be started, and does not know what the program to be started specifically (for example, a program wants to start the application sending text messages, and at this time there are multiple software to send messages in the system, so we cannot know what software to open). At this time, what the program should do is to send a purpose to start the SMS application, rather than specifying what software to open. Of course, more general, Intent can also specify which application to open, and has a hard coupling relationship with some programs.

Intent is also an important medium for communication between application components. The two activities encapsulate the data that needs to be exchanged into Bundle objects, and then use Intent to carry the Bundle objects, thus realizing the data exchange between the two activities.

The following properties exist in the Intent: Component, Action, Category, Data, Type, Extra, and Flag. The Component property is used to specify the target component to be started, and the Extra property is used to carry the data to be exchanged.

The following is a detailed introduction to the Intent object:

1. Component properties of Intent

Component is used to specify the properties of the startup target component. The standard startup code is as follows:

ComponentName comp = new ComponentName(,);
Intent intent = new Intent();
Intent. setComponent(comp);
startActivity(intent);

The above code is used to create a ComponentName object in a standard way, and then call the setComponent() function of the Intent object to set the corresponding componentName for the Intent, and finally call the startAcitivity function to start a new Activity.

In fact, when you need to set the Component property for the Intent, Intent has provided us with a simple constructor, which is used as follows (the way we often use it):

Copy the codeThe code is as follows:
Intent intent = new Intent(,);

It is worth mentioning that in the secondActivity component, the getIntent object that calls the secondary component can be obtained by using the getIntent() method, and the getXXX method can be used in various ways here.

2. Action and Category properties of Intent

Action and Category properties are both ordinary strings, where Action represents abstract "actions". The Category property is used in conjunction with the Action property to express the intention to start a certain component.

All activities with the <intent-filter…/> tag may be started.

for example:

Public final staticString SOME_ACTION = ".SOME_ACTION"
// This is a character rotation, which is set casually, but generally has some abstract semantics.In thisActivityA certain buttononClick()Added to the method:
Intent intent = newIntent();
(thisActivity.SOME_ACTION);
startActivity(intent);

Such code does not specify which Activity to start, which is separated from "hard-coded", but which Activity to start will depend on the <intent-filter.../> tag in the Activity configuration file.

<intent-filter.../> is a child element of the <activity.../> element in the file. What needs to be done is to add a <intent-filter.../> tag to the Activity that actually needs the corresponding intention. Under <intent-filter.../>, there are three types of tags: 1.<action.../>2.<category.../>3.<data.../>. After specifying the android:name attribute, this activity has the corresponding attribute of the intention above.

For the above intention, add such code (of course, under the <intent-filter.../> tag):

Copy the codeThe code is as follows:
<action android:name = ".SOME_ACTION"/>

It should be mentioned that an Intent object can only contain one Activity attribute at most, and the program calls setAction(Stringstr) to set the property value of the Action; while an Activity can have multiple Category attributes, and the program can call addCategory(Stringstr) to add Category attributes. When the program creates an Intent, the created Intent property automatically starts the property value is the Intent.CATEGORY_DEFAULT constant, and its value is "", so when configuring a certain Activity property <categoryandroid:name = " "> can be added to the configuration file.

In fact, Android provides a large number of standard Action and Category constants internally.

The summary is as follows:

Action constants

Corresponding android:name settings

Simple instructions

ACTION_MAIN

Application portal

ACTION_VIEW

Display specified data

ACTION_ATTACH_DATA

.ATTACH_DATA

Specify where a module data is attached

ACTION_EDIT

Edit the specified data

ACTION_PICK

Select an item from the list and return the selected data

ACTION_CHOOSER

Show an Activity selector

ACTION_GET_CONTENT

.GET_CONTENT

Let the user select data and return the selected

ACTION_DIAL

Show dial panel

ACTION_SEND

Send data directly

ACTION_SENDTO

Send messages directly

ACTION_ANSWER

Answer the phone

ACTION_INSERT

Insert data

ACTION_DELETE

Delete data

ACTION_RUN

Run data

ACTION_SYNC

Perform data synchronization

ACTION_PICK_ACTIVITY

.PICK_ACTIVITY

Used to select an activity

ACTION_SEARCH

Perform a search

ACTION_WEB_SEARCH

. WEB_SEARCH

Diameter web search

ACTION_ FACTORY_TEST

.FACTORY_TEST

Factory Test Entrance Point

Here are only some excerpts. If you want to know all ACTIONs, you can directly view the instructions on Intent in Android's standard API.

3. Data and Type properties in Intent

Take a few examples to illustrate the usage of Data:

Add the following code to the OnClick() method of a button:

String data = ;
Uri uri =(data);
(Intent.ACTION_VIEW);
(uri);
startActivity(intent);

In this method, the button will start Renren.

Of course, the code is a simple way to write:

Uri myUri = ("");
Intent intent = new Intent(Intent.ACTION_VIEW,myUri);
startActivity(intent);

The above is a more detailed setting method.

4. Extra properties of Intent

The Intent attribute is usually used to exchange data between multiple activities. The Extra attribute value of the Intent should be a Bundle object. It can enter multiple key-value pairs, so that the corresponding data exchange between different activities can be performed through the Intent.

The methods provided by Intent are as follows:

putExtra(Bundledata)   getExtras()
putXXX(Stringkey XXX data)   getXXX(String key)
putSerializable(Stringkey, Serializable data) corresponding to
getSerializable(Stringkey, Serializable data)

The relevant code for adding key-value pairs has been given in the courseware, so I will not repeat it here.

Summarize:

Android applications always need to use Intent to implement a component that needs to be started. Intent is the encapsulation form of "start intention". This intention is not coupled with any program components. In this way, the scalability and maintainability of the program can be provided well. The configuration of <intent-filter/> is the most important tag for program components. It is best to use Eclipse to add corresponding tags, because some small errors such as spaces often cause some exceptions. I hope that all children's shoes will pay attention to it when using them.

For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android programming activity operation skills summary》、《Android resource operation skills summary》、《Android file operation skills summary》、《Summary of Android's SQLite database skills》、《Summary of Android operating json format data skills》、《Android database operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android View View Tips Summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.