Objective:
Each application has a number of activity components, each of which is a window into which the application interacts with the user and presents a different interactive interface. Because each acticity task is different, it often jumps between activities, and in Android This is done by intent. You send a intent to the system through the StartActivity () method, and the system will help you find the activity based on this intent, even if the activity is in other applications, and it can be started in this way.
Definition of Intent:
Intent is an Android system used to abstract describe an operation to be performed, or to communicate and deliver messages between different components.
Intent intent can be a specific name for the specified component, so that you can start a system component precisely, such as starting an activity. It can also be fuzzy, without specifying the component name, as long as it is able to match to the intent of the application can be received, such as sending a photo intent, all the photo application will respond.
Explicit intent and implicit intent:
The explicit intent is that you already know the name of the component you want to start, such as the package name and the class name of an activity, specifically specifying this component (activity) in intent, which in general is often used in an application, Because you already know exactly which component name to start.
The implicit intent is that you don't know the name of the component you want to start, just know a intent action to perform, such as: Take pictures, video, view the map. In general, this intent is used to pass information between different applications.
When you create an explicit intent to start an activity or service, the system immediately launches that component through your intent object.
When you create an implicit intent, the system will find a matching component based on the intent filter in the manifest file, and if you send a intent match to a intent filter, the system will pass your intent to the corresponding component of the filter (Activity, service, etc.), and start it. If multiple matching intent filter applications are found, a dialog box pops up to let you choose which application will accept your intent.
Note: 1, intent filter is a syntax expression in an application manifest file, like an HTML tag, which is part of a system component tag (such as Activity), which can be said to be a sub-tag, This system component accepts what intent is used to specify, and if a system component does not write intent filter, it can only be started by an explicit intent. 2. For security reasons, your service component must not be illustrated with an implicit intent filter, because starting a service with an implicit intent often does not guarantee that the service will be started. And the user does not know which service will respond to your intent. From Android5.0 (API 21), the system throws an exception when you use the implicit intent to call the Bindservice () method.
Create a intent:
The Android system determines which system components to start by using the information that the intent object carries, such as the exact component name, which category of component receives the Intent,intent and carries other information to facilitate the component to perform the action correctly. A intent mainly contains the following information:
Component Name:
The name of the component to start is optional when creating intent, but it is an important sign of explicit intent, which means that only the component on Component name match can receive the display intent you send out. If you do not write then the intent you create is implicit, and the system will determine which components to receive this intent based on the intent other information (such as action, data, category), so if you want to explicitly start which component, is specified by component name.
The ComponentName property of intent is the full name of a class, including the package name, such as: com.example.ExampleActivity, which you can pass intent setcomponent (), SetClass (), The Setclassname () method can also be set by means of the intent construction method.
Action:
Intent, a string variable that specifies the type of action to be performed by the intent (for example, view or pick). You can customize the action in your application, but most of the time you only use the action defined in intent. Here are the actions that are often used to start activity:
When you invoke the StartActivity () method to start the activity, use this action to render some information to the user, such as viewing a photo in the album, showing a geographic location in the map.
Also known as the "share" (shared) intent, when invoking the StartActivity () method to initiate activity, use this action to share some of the data the user wants to share with other applications, such as mail apps or social apps.
To learn more about action, you can view the intent source code or API and define a lot of actions in intent, except in the Android framework, where many actions are defined elsewhere, such as in Settings applications.
You can specify the action by Intent's setaction () method or by intent's construction method. If you want to customize the action, it's best to prefix your package name, such as:
Static final String Action_timetravel = "Com.example.action.TIMETRAVEL";
Data:
A URI object that corresponds to a data that may be of mime type. When creating a intent, it is also important to specify the type of data (MIME type) In addition to the URI that specifies the data, for example, an activity can display a photo but cannot play the video, although the URI format is similar when the activity is started. It is important to specify the MIME type, which helps the system find the most appropriate system component to handle your intent request. However, MIME type can sometimes be inferred from URIs, especially if data is a URI of content: This data indicates that it is provided by ContentProvider in the device.
Only set the URI of the data can call the SetData () method, only set the MIME type can call the Settype () method, if you want to set both of these two can call Setdataandtype ().
Note: If you want to set the URI and mime, do not Call SetData () and Settype (), because the parameter data that is set will be cleared with each other, but instead call Setdataandtype () directly.
Category:
A string containing intent additional information that represents what type of component to handle this intent. Any number of category descriptions can be added to intent, but many intent do not require category, and some of the commonly used category are listed below:
Target activity allows data to be displayed from a link on a Web page (a slice link).
Indicates that this activity is the activity stack's initial activity, the application's main activity, can be found in the desktop application list and start it.
You can set the category by calling the Addcagegory () method.
The properties of the above intent (component name, action, data, and category) are intent feature properties that the Android system can find which application component will be started.
The following properties are additional properties of intent, which do not affect the processing of intent and the boot system components.
Extras:
Intent can carry extra key-value data, you can set the data by calling the Putextra () method, each key corresponds to a value data. You can also store all the data by creating a bundle object, and then set the data by calling the Putextras () method. The name of the data key should be prefixed with the package name as much as possible, and then the other, so as to ensure the uniqueness of the key.
Flags:
Used to indicate how the system initiates an activity (for example, which activity stack the activity belongs to) and how the activity is handled after it is started (for example, whether to classify the activity as the most recent list of activities).
To create an explicit intent:
An explicit intent explicitly specifies the name of the component to start, such as the activity name or service name. To create an explicit intent, you must define the component property, and other properties are optional. The following example is to create a service in the app, called Downloadservice, which is the function to download the file from the network, you can start it by using the following code:
Executed in's Activity, so ' this ' was the context//the FILEURL is a string URL, such as "Http://www.example.com/image. PNG "Intent downloadintent = new Intent (this, downloadservice.class);d ownloadintent.setdata (Uri.parse (FILEURL)); StartService (downloadintent);
To create an implicit intent:
An implicit intent defines the action that will be performed, and any app on the device can respond to that action. Using implicit intent is very useful when your app cannot handle certain request actions, but other applications in the system have the ability to handle this, and users can easily use other applications to do so. For example, you use Baidu cloud disk download a PDF document, you click Open this document when the Baidu Cloud disk is unable to open, but perhaps you installed on the system has other open PDF document reader, this time will pop up a dialog box, listing can open the PDF document of the application, You are free to choose an application to open the document you downloaded. For example, see the following example of a document:
Create the text message with a stringintent sendintent = new Intent (); sendintent.setaction (intent.action_send); sendint Ent.putextra (Intent.extra_text, TextMessage); Sendintent.settype (HTTP. Plain_text_type); "Text/plain" MIME type//Verify that the intent would resolve to an activityif (Sendintent.resolveactivity (Getpackageman Ager ()) = null) { startactivity (sendintent);}
Note: Perhaps your device does not have an app to perform your implicit intent, and this time startactivity will fail and your app will appear crash, The best way to do this is to call Resolveactivity before startactivity to check if any activity is responding. If there is an application in the system that can respond, it will open the application directly, and if there are multiple apps in the system that can handle the action, a popup dialog will let you choose which application to process.
Summarize:
The above content mainly describes the definition of intent, Classification and attribute interpretation, so that you have a detailed understanding of intent, for later learning to use intent to communicate between the application to lay the foundation.
This article is original, reproduced please indicate the source, offenders must investigate
focus on the public platform: the Programmer Interaction Alliance (coder_online), You can the first time to get original technical articles, and (Java/c/c++/android/windows/linux) technology Daniel to be friends, online communication programming experience, Get the basics of programming, solve programming problems. Programmer Interactive Alliance, Developer's own home.
Intent detailed in Android