Android Notes 02: Detailed Intent Mechanism

1. What is an Intent?

Intent provides an Intent mechanism in Android to assist in the interaction and communication between applications. Intent is responsible for describing the action of an operation in the application, the data involved in the action, and the additional data. Android is responsible for finding the corresponding component according to the description of this Intent. Pass the Intent to the calling component and complete the component's call. Intents can be used not only between applications, but also for interactions between Activities/Services within applications. Therefore, Intent can be understood as the "medium" of communication between different components, which specifically provides relevant information for components to call each other.

Second, the role of Intent

Intent is an abstract description of the action to be performed, generally used as a parameter, by Intent to assist in the communication between the various components of android. For example, call startActivity() to start an activity, or pass it to all interested BroadcaseReceivers by broadcaseIntent(), or start a background service by startService()/bindservice(). So it can be seen that the intent is mainly It is used to start other activities or services, so intent can be understood as the glue between activities.

Three, the two types of Intent

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

Explicit Intent:

If the full class name (package name and class name) of the component to be started is explicitly included in the Intent we define, that is, the "intent" of the Intent is very obvious, then the Intent is explicit. The most typical case of using an explicit intent is to start a component in your own program, because you yourself must know the class name of the component you want to start. For example, start an Activity in your program through an explicit Intent or start a Service to download a file in response to a user action.

Implicit Intent:

Instead of specifying which activity the component name should start, the system finds the most appropriate component based on action, category, data (Uri), and data type. Generally speaking, we need to set information such as category, action and data in <intent-filter>. It should be noted that in order to ensure the security of the App, we should always use an explicit Intent to start the Service and do not set any Intent Filter for the Service. Starting a Service through an implicit Intent is risky, because you are not sure which Service in which App will eventually start in response to your implicit Intent, and sadly, because the Service runs in the background without a UI, the user I don't know which Service is running.

Intent Filter, that is, Intent Filter, a component can contain 0 or more Intent Filters. Intent Filter is written in the manifest file of the App, which specifies the type of Intent that the component can handle by setting the action or uri data type.

Concrete methods of implicit Intent:

<activity

           android:name=".ThreeActivity"

           >

            <intent-filter >

                <action android:name="android.intent.action.VIEW"/>

                <category android:name="android.intent.category.DEFAULT"/>

                <data android:scheme="http"/>

            </intent-filter>

</activity>

The <action> tag specifies the action that the current activity can respond to. The <category> tag adds some information to more precisely specify the category that the Intent that the current activity can respond to may also carry. Only when <action> and <category> match the action and category specified in the Intent at the same time, the activity can respond to the Intent.

The <data> tag can more precisely specify what type of data the current activity can respond to.

The following content can be configured in the <data> tag:

android:scheme. Used to specify the protocol part of the data, such as http.

android:host. The hostname part used to specify the data.

android:port. The port part used to specify the data, usually after the hostname.

android:path. Used to specify the part after the hostname and port, such as the content following the domain name in a URL.

android:mimeType. Used to specify the data types that can be processed, allowing the use of wildcards to specify.

Only when the content specified in the <data> tag is exactly the same as the data part carried in the intent, the current activity can respond to the intent.

 

Explicit Intent example:

Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);

The above code specifies that the ComponentName of the component to be started is ActivityB in the constructor of the intent. The intent object is explicit. When startActivity(intent) is called, the Android system will start ActivityB immediately.

Implicit Intent example:

When using an implicit Intent, you need to specify its action. If your app cannot perform a certain function, but other apps may do it, then you can use an implicit intent to start other apps to complete the corresponding function. For example, if you have a piece of text information that you want to share through other apps, then an implicit Intent object is used to start a potential app that supports sharing. The sample code is as follows:

Intent sendIntent = new Intent();
 // Set action, action is very important for implicit Intent 
sendIntent.setAction(Intent.ACTION_SEND);
 // Set the MIME type of data to plain text type 
sendIntent.setType("text /plain" );
 // Set extra data 
sendIntent.putExtra(Intent.EXTRA_TEXT, textMessage);

// Get the package manager 
PackageManager pm = getPackageManager();
 // First determine whether there is a potential App activity in the system that supports the reception and processing of the sendIntent 
if (pm.resolveActivity(sendIntent, 0) != null ) {
    startActivity(sendIntent);
}

In the above code, we constructed an Intent object and did not set the component name for it, so the Intent is an implicit Intent object. We first set the action value to Intent.ACTION_SEND for the intent. Action is very important for implicit Intent. Then we set the MIME type of the intent's data to plain text ("text/plain"), telling Android that our intent is holding text-type data. Finally we set the actual text data in as extra data via the putExtra() method. 
It should be noted that after constructing the Intent object, we did not execute the startActivity(sendIntent) method immediately, but passed the sendIntent as a parameter to the resolveActivity() method of the PackageManager, which would allow Android to find the potential based on the sendIntent The information of the component suitable for startup, and returns the result in the form of an object of the ResolveInfo class. If it returns null, it means that there is no component in the current system that can receive and process the sendIntent. If the return is not null, it means that there is at least one component in the system that can receive and process the sendIntent. Only in this case, we will execute the code startActivity(sendIntent), and judge whether the component to be started exists before starting the component through the intent. It is a good programming practice to not exist, because if there is no component in the system that supports your intent, then when you call startActivity(), startService(), bindService() and other methods, Android will throw an exception.

 (There will be revisions and additions in the future)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325060041&siteId=291194637