Android foundation summary part 9: detailed explanation of intent application

Today, let's talk about the principle and application of intent in Android.

We have summarized several important components in Android. I believe you have a clear understanding of these components. Let's take a look at some common operations:

We found that in these operations, there is an intent involved, which looks like a very important component. What is the intent?

In short, intent is the data loader for data transfer between various components of the system. When we need to make a call action, we can tell the Android system through intent to complete this process. Intent is an operation of calling notification.

Intent has several important attributes, which will be introduced one by one below:

1. Action, the action to be executed

For activities with the following declarations:

TargetActivity declares < action >, i.e. target action, in its < intent Filter >. If we need to make a jump action, we need to specify the target action in intent, as follows:

When we specify the corresponding action for Intent, and then call the startActivity method, the system will jump to the corresponding Activity according to action.

In addition to the custom actions, intent also contains many default actions. Just list a few:

Each action has its specific purpose, and they will be used below.

2. Data and extras, that is, the data to be operated and the additional information transmitted to the target

Here is an example of interacting with the browser:

The above two methods are to start the browser, open the specified web page and search for keywords. The corresponding action is intent.action_ View and intent.action_ WEB_ Search, the former needs to specify the corresponding web page address, and the latter needs to specify keyword information. For keyword search, the browser will search according to the default search engine set by itself.

We note that when opening a web page, we specify a data attribute for intent, which actually specifies the data to be operated. It is in the form of a URI. We can convert a string with a specified prefix into a specific URI type, such as: "http:" or "HTTPS:" indicates the network address type, "Tel:" indicates the phone number type, and mailto: "indicates the email address type, wait. For example, if we want to call a given number, we can do this:

So how do we know if the target accepts this prefix? This requires a look at the matching rules of < data / > elements in the target.

The target < data / > tag contains the following sub elements, which define the matching rules of the URL:

Let's change the declaration information of targetActivity:

At this time, if only specifying action is not enough, we need to set the data value for it, as follows:

At this time, only when each part of the URL is consistent with the targetActivity configuration information can the jump succeed, otherwise it will be rejected by the system. But sometimes it's not good to limit the path. For example, we have such a URL:( scott://com.scott.intent.data:7788/target/hello )( scott://com.scott.intent.data:7788/target/hi )

What should we do at this time? We need to use another element: Android: pathprefix, which represents the path prefix.

We modify Android: path = "/ target" to Android: pathprefix = "/ target", and then we can meet the above requirements.

During the search, we use a putextra method to place the keyword as a parameter in intent. We become extras (additional information), which involves a bundle object.

Bundle is closely related to intent and is mainly responsible for saving additional parameter information for intent. It implements the android.os.paracelable interface and internally maintains a map type attribute to store additional parameter information in the form of key value pairs. When we use intent's putextra method to place additional information, this method will check that the default bundle instance is not empty. If it is empty, a new bundle instance will be created, and then the specific parameter information will be placed in the bundle instance. We can also create our own bundle object and specify this bundle for intent, as follows:

It should be noted that after using the putextras method to set the bundle object, the system does not reference, but copy. Therefore, if you change the data in the bundle instance after setting, the additional information inside intent will not be affected. How do we get the additional information set in intent? Correspondingly, we need to get the bundle instance from intent, and then get the corresponding key value information from it:

Of course, we can also use intent's getintextra and getstringextra methods to obtain, and their data sources are instance objects of bundle type in intent.

Earlier, we talked about the three properties of intent: action, data and extras. In addition, intent includes the following properties:

3. Category refers to the characteristics or behavior classification of the target to perform the action

For example, in the main interface of our application, activity is usually configured as follows:

Represents that the target activity is the initial activity in the task where the application is located and appears in the application list of the system launcher.

Several common categories are as follows:

When setting category for intent, you should use the addcategory (string category) method to add the specified category information to intent to match the target activity declaring this category.

4. Type: the mime data type that can be processed by the target activity to execute the action

For example, a target activity that can process pictures contains such mimeType in its declaration:

When using intent for matching, we can use setType (string type) or setdataandtype (URI data, string type) to set the mimeType.

5. Component, package or class name of the target component

When using component for matching, the following forms are generally adopted:

The first two are used to match targets in the same package, and the third is used to match targets in other packages. It should be noted that if we specify the component attribute in intent, the system will no longer match action, data / type and category.

That's all for today. I'll add it if I have time.

Original link: http://blog.csdn.net/liuhe688/article/details/7162988

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>