Android Intent and AndroidIntent
Preface:
Each application is composed of several activities. Each Activity is a window in which the application interacts with the user and presents different interactive interfaces. Because each Acticity task is different, it is often used to jump between activities. In Android, this action is completed by Intent. You send an Intent to the system through startActivity (). The system will help you find the corresponding Activity based on this Intent, even if this Activity is in another application, you can also start it in this way.
Intent definition:
Intent is an Android system used to abstract and describe an operation to be executed. It can also communicate and transmit messages between different components.
Intent can be a specific component name, so that you can precisely start a system component, such as starting an Activity. It can also be fuzzy, with no component name specified. Any application that can match this Intent can receive it. For example, if a photo Intent is sent, all the photo apps will respond.
Explicit Intent and implicit Intent:
The explicit Intent is the name of the component that you already know to start, such as the package name and class name of an Activity. The Intent explicitly specifies this component (Activity ), generally, this kind of Intent is often used in an application because you already know the name of the component to be started.
The implicit Intent is the component name you do not know to start. You only know that an Intent action is to be executed, such as taking photos, recording videos, and viewing maps. Generally, this kind of Intent is used to transmit information between different applications.
When you create an explicit Intent to start an Activity or Service, the system immediately starts that component through your Intent object.
When you create an implicit Intent, the system will find the matching component based on the intent filter in the manifest file. If the Intent you send matches an intent filter, the system will pass your Intent to the corresponding filter component (Activity, Service, etc.) and start it. If you find the application corresponding to multiple matching intent filters, a dialog box appears asking you to select which application to accept your Intent.
Note: 1. intent filter is a syntax expression in the manifest file of an application, like an html Tag. It is a component of a system component tag (for example, Activity, it can be said that it is a sub-tag. The intent accepted by this system component is used to specify it. If a system component does not write intent filter, it can only be started through an explicit intent. 2. For security reasons, do not use the implicit intent filter to describe your Service components, because starting a Service with an implicit intent cannot guarantee that the Service will be started, the user does not know which Service will respond to your Intent. From Android5.0 (API 21), the system will throw an exception when you use an implicit Intent to call the bindService () method.
Create an Intent:
The Android system uses the information carried by the Intent object to determine which system component to start. For example, if the component name is accurate, the component of which category will receive the intent, intent also carries other information so that the component can correctly execute the action. An Intent mainly contains the following information:
Component name:
The component name to be started is optional when Intent is created, but it is an important identifier of an explicit Intent, it means that only the Component on which the Component name matches can receive the display intent you sent. If you do not write, the Intent you created is implicit. The system determines which components of the intent will receive the intent based on the other information of the Intent (such as action, data, and category, therefore, if you want to specify which component to start, you can use the component name.
The ComponentName attribute of Intent is the full name of a class, including the package name, such as com. example. exampleActivity, which can be set by the setComponent (), setClass (), and setClassName () Methods of Intent, or by the Intent constructor.
Action:
Intent, a string variable used to specify the action category (for example, view or pick) that Intent will execute ). You can customize the action in your application, but most of the time you only use the action defined in Intent. The following are common actions used to start an Activity:
When you call the startActivity () method to start an Activity, use this action to present some information to the user. For example, you can view a photo in the album and display a geographical location in the map.
Also known as "share" (share) intent, this action is used to share data that users want to share with other applications when the startActivity () method is called to start an Activity, for example, email app or social app.
To learn more about actions, you can view the Intent source code or api. Many actions are defined in Intent, and many actions are defined in the Android framework, for example, in the Settings application.
You can use the setAction () method of Intent or the constructor of Intent to specify the action. If you want to customize the action, you 'd better use your package name as the prefix, for example:
static final String ACTION_TIMETRAVEL = "com.example.action.TIMETRAVEL";
Data:
A Uri object corresponds to a data, which may be of the MIME type. When creating an intent, in addition to specifying the data URI, the specified data type (MIME type) is also very important. For example, an activity can display pictures but cannot play videos, although the URI format is similar when the Activity is started. It is important to specify the MIME type. It helps the system find the most appropriate system component to process your intent requests. However, MIME type can be inferred from URI, especially when data is a content: URI, such data indicates that it is provided by ContentProvider in the device.
To set only the data URI, you can call the setData () method. To set only the MIME type, you can call the setType () method. To set both, you can call setDataAndType ().
Note: To set URI and MIME, do not call setData () and setType (), because setDataAndType () is called directly instead because the set parameter data is cleared for each other ().
Category:
A string containing extra Intent information, indicating which type of component to process this Intent. Any number of Category descriptions can be added to Intent, but many intent does not need category. The following lists some common category:
The target Activity allows you to start from a link on a web page (than a link) to display data.
Indicates that the Activity is the original Activity of the Activity stack. The main Activity of the application can be found and started in the Desktop Application List.
You can set category by calling addCagegory.
The above Intent attributes (component name, action, data and category) are all Intent feature attributes. With these attributes, the Android system can find which application component will be started.
The following attributes are additional Intent attributes that do not affect intent processing and startup of system components.
Extras:
Intent can carry additional key-value data. You can call putExtra () to set data. Each key corresponds to a value data. You can also create a Bundle object to store all the data, and then call the putExtras () method to set the data. For the data key name, try to use the package name as the prefix, and then add others to ensure the uniqueness of the key.
Flags:
It is used to indicate how the system starts an Activity (for example, the stack of the Activity) and how the Activity is processed after it is started (for example, whether to classify the Activity as a list of recent activities ).
Create an explicit Intent:
An explicit Intent explicitly specifies the name of the component to be started, such as the name of Activity or Service. When creating an explicit Intent, you must define the Component Attribute. Other attributes are optional. The following example creates a Service named DownloadService in the app. The function is to download files from the network. You can start it using the following code:
// Executed in an Activity, so 'this' is the Context// The fileUrl is a string URL, such as "http://www.example.com/image.png"Intent downloadIntent = new Intent(this, DownloadService.class);downloadIntent.setData(Uri.parse(fileUrl));startService(downloadIntent);
Create an implicit Intent:
An implicit Intent defines the action to be executed. Any app on the device can respond to this action. It is very useful to use an implicit Intent. When your app cannot process certain request actions, other applications in the system can process these actions, you can easily use other applications to complete this operation. For example, if you downloaded a pdf file from Baidu cloud disk, Baidu cloud disk cannot be opened when you click to open this document, however, you may have installed other readers that can open pdf files on your system. A dialog box will pop up to list applications that can open pdf files, you can select an application to open the document you downloaded. For example, the following is an example of a document:
// Create the text message with a stringIntent sendIntent = new Intent();sendIntent.setAction(Intent.ACTION_SEND);sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);sendIntent.setType(HTTP.PLAIN_TEXT_TYPE); // "text/plain" MIME type// Verify that the intent will resolve to an activityif (sendIntent.resolveActivity(getPackageManager()) != null) { startActivity(sendIntent);}
Note: Your device may not be able to execute your implicit Intent application. In this case, startActivity will fail and crash will appear in your app, the best way is to call resolveActivity before startActivity to check whether any Activity can respond. If an application in the system can respond, the application will be opened directly. If multiple apps in the system can process this action, a dialog box will pop up asking you to select which application to process.
Summary:
The above content mainly describes the definition, classification, and attribute interpretation of Intent, so that you can have a detailed understanding of Intent and lay the foundation for later learning to use Intent for communication between applications.
This article is original. Please indicate the source for reprinting. violators must investigate
Public platforms: coder_online ),You canGet the original technical article as soon as possible,I am a friend of java/C ++/Android/Windows/Linux,Exchange programming experience online,Obtain basic programming knowledge,Solve programming problems. Programmer InterAction alliance, developer's own home.