Android note 02:intent Mechanism detailed

Source: Internet
Author: User

First, what is intent?

Intent provides the intent mechanism in Android to assist in the interaction and communication between applications, intent is responsible for the action of one operation in the application, the action involves data, the additional data is described, and Android according to this intent description, responsible for finding the corresponding components, Pass the intent to the calling component and complete the call to the component. Intent can be used not only between applications, but also between Activity/service within an application. As a result, intent can be understood as a "medium" for communication between different components, specifically providing information about what components call each other.

Second, the role of intent

Intent is an abstract description of the action to be performed, which is generally used as a parameter, and is assisted by Intent to complete communication between the various components of Android. For example, call StartActivity () to initiate an activity, or broadcaseintent () to all interested broadcasereceiver, or by StartService ()/ Bindservice () to start a backend service. So it can be seen that intent is primarily used to initiate other activity or service, so intent can be understood as an adhesive between the activity.

Three, two types of intent

Intent can be broadly divided into two types: explicit intent and implicit intent.

An explicit intent:

This intent is explicit if the full class name (package name and class name) of the component to be started is explicitly included in our defined intent, meaning intent's "intent" is obvious. The most typical scenario for using explicit intent is to start a component in your own program, because you must know your own class name for the component you want to start. For example, in response to user actions, an explicit intent starts an activity in your program or launches a service download file.

Implicit intent:

Instead of explicitly specifying the component name to start which activity, the system finds the most appropriate component based on the action (action), category, Data (Uri), and data type. In general, we need to set up category, action and data information in <intent-filter>. It should be noted that in order to ensure the security of the app, we should always use explicit intent to start the service and not set any intent Filter for that service. It is risky to start the service through an implicit intent because you are not sure which service in the end will start up in response to your implicit intent, and more sadly, because the service does not have a UI running in the background, So the user doesn't know which service is running.

Intent filter, or Intent filter, a component can contain 0 or more Intent filter. Intent filter is written in the app's manifest file, which specifies the type of Intent that the component can handle by setting the action or URI data type.

The specific method of implicit intent:

<ActivityAndroid:name=". Threeactivity "           >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.VIEW"/>                <categoryAndroid:name= "Android.intent.category.DEFAULT"/>                <DataAndroid:scheme= "http"/>            </Intent-filter></Activity>

Where the <action> tag indicates which action the current activity can respond to. The <category> tag appends some information to more accurately indicate the category that the intent that the current activity can respond to may also have. The activity should intent only if <action> and <category> match the action and category specified in intent.

<data> tags can more precisely specify what type of data the current activity responds to.

The main contents of the <data> tag can be configured:

Android:scheme. The protocol part used to specify the data, such as HTTP.

Android:host. The host name portion of the data to specify.

Android:port. The port portion of the specified data, typically after the host name.

Android:path. Used to specify the host name and the part after the port, such as the contents of a URL followed by the domain name.

Android:mimetype. Lets you specify the type of data that can be processed and allows you to specify it by using a wildcard character.

The current activity responds to intent only when the content specified in the <data> tag is exactly the same as the data part carried in the intent.

An explicit intent example:

New Intent (This, activityb.  Class); startactivity (intent);

The above code specifies in the intent constructor that the componentname of the component to be started is activityb, the intent object is explicit, and when startactivity (intent) is called, The Android system will start activityb immediately.

An implicit intent example:

When using implicit intent, you need to specify its action. If your app does not complete a feature, but other apps may be able to do that, you can use the implicit intent to launch other apps to do the right function. For example, if you have a text message that you want to share with other apps, then the implicit intent object is to launch a potentially shared app, with the sample code as follows:

Intent sendintent =NewIntent ();//set action, action is very important to implicit intentsendintent.setaction (intent.action_send);//set the MIME type of the data to plain text typeSendintent.settype ("Text/plain");//set up additional dataSendintent.putextra (Intent.extra_text, textmessage);//Get Package ManagerPackagemanager pm =Getpackagemanager ();//first determine if there is any potential app activity in the system to support the reception and processing of the sendintentif(Pm.resolveactivity (sendintent, 0)! =NULL) {startactivity (sendintent);}

In the above code, we built a intent object and did not set component name for it, so the intent is an implicit intent object. We first set the ACTION value for intent to intent.action_send,action is very important for implicit intent. We then set the MIME type of the intent data to the plain text type ("Text/plain"), informing Android that our intent holds the text type of data. Finally, we set the actual text data through the Putextra () method as additional data.
It is important to note that after building the intent object, we do not immediately execute the startactivity (sendintent) method, but instead pass the sendintent as a parameter to Packagemanager resolveactivity () method, this method allows Android to find information about a potentially appropriate startup component based on the sendintent, and returns the result as an object of the Resolveinfo class, if NULL is returned. Indicates that no component in the current system can receive and process the sendintent. If the return is not NULL, then there is at least one component in the system that can receive and process the sendintent, and only in this case do we execute code startactivity (sendintent). It is a good programming practice to judge that the component to start is not present before starting the component through intent, because if there is no component in the system that supports your intent, then when you call StartActivity (), StartService (), Bindservice () and other methods, Android throws an exception.

(Subsequent revisions and additions will be available)

Android note 02:intent Mechanism detailed

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.