Introduction to intent Filter matching rules in Android _android

Source: Internet
Author: User

This article mainly introduced the implicit intent matching target component's rule, if has the narration not clear or the inaccurate place hoped that everybody pointed out, thanks everybody:

1. Intent Introduction

Intent is used to open another component in a component (Component, such as activity, Service, broadcast Receiver).

Intent can be divided into implicit (implicitly) and explicit (explicitly) two types:

Explicitly Intent: When you know which specific component to open, you can open the target component by specifying the caller and the callee;
Implicitly Intent: If you don't know exactly which component to open, by pointing to action, data, category, the system will find a matching component.
(1) explicitly Intent

When you know exactly which component you want to open, it's your dish. This is usually used in this way:

Intent Intent = new Intent (this, mainactivity.class);
Intent.putextra ("Key", "value");
StartActivity (Intent);

Executing the above code will cause the target component (here is mainactivity) to be created (OnCreate, and so on a series of life cycle methods are called). In the corresponding lifecycle method in mainacitivity, the data transmitted with intent can be obtained through Getintent.getxxxextra ("key").

(2) implicitly Intent

Implicitly intent the decoupling between the caller and the callee:

The caller describes his intent through the three aspects of action, data, and category, and the caller describes what they can respond to by a series of intent filter declarations declared in the manifest file. As a result, the caller and the callee do not need to understand each other, and the implicitly intent the link to them can work well together.

About intent more detailed introduction, you can refer to the official documents, here mainly introduced under the implicitly intent matching rules.

2.Intent Filter Matching rule

Only the action, data, category all three match, intent is a matching success, and then open the corresponding component. If a component declares multiple intent Filter, you can start the component only by matching any one.

(1) Action matching rule

A intent filter can declare that action in multiple action,intent is exactly the same as any action in the form of a string (note, case-sensitive), and the action is matched successfully. You can set the action for intent through the Setaction method, or you can pass in the action when you construct intent. It is important to note that the implicit intent must specify an action. For example, we defined the following intent Filter for myactivity in the manifest file:

<intent-filter>
  <action android:name= "Android.intent.action.SEND"/>
  <action android:name= "Android.intent.action.SEND_TO"/>
</intent-filter>

So as long as intent's action is "SEND" or "send_to", then the intent can match the action to the above activity. For example, our intent definition is as follows:

Intent Intent = new Intent ("Android.intent.action.SEND")
...
So our intent matches myactivity in action.

The Android system has predefined many actions, which represent some common operations. Common action is as follows (constants in the intent Class):

Intent.action_view
Intent.action_dial
Intent.action_sendto
Intent.action_send
Intent.action_web_search

(2) Data matching rules

Data can be further divided into URIs (by scheme, host, port, path | Pathpattern | Pathprefix these 4 components) and mimetype. The intent URI can be set by the SetData method, MimeType can be set through the Settype method. The implicit intent must also specify data. Similar to action, data is matched successfully as long as the intent data is exactly the same as any data declaration in intent filter. Note that if a URI is not specified in the Data Declaration section of Intent filter, the scheme part of the URI in the content or file,intent of the default URI must be content or file to match To specify the full data for intent, you must use the Setdataandtype method, for reasons, see the source code for the SetData and Settype methods:

Public Intent SetData (Uri data) {
  mdata = data;
  Mtype = null;
  return this;
}

Public Intent SetType (String type) {
  mdata = null;
  Mtype = type;
  return this;
}

As you can see from the above code, SetData will place the mimetype as Null,settype to leave the URI null. Let's illustrate the match of data. First, let's take a look at the syntax for specifying data in intent filter:

<data android:scheme= "..." android:host= "..." android:port= "..."
     android:path=
     "..." Android:pathpattern= "..." android:pathprefix= "..." android:mimetype= "..."
     />

where scheme, host and other parts need not be all specified. If we specify the following data for the MyActivity intent filter:

<intent-filter>
  <data android:mimetype= "vidoe/mpeg" android:scheme= "http" android:host= "www.xxx.com" />
  <data android:mimetype= "text/plain" android:scheme= "http"/>
</intent-filter>

So our intent want to match, mimetype can be "Text/plain" or "video/mpeg", scheme must be "http", host is not limited, because the second data does not specify host.

(3) matching rules for category

Unlike action and data, category in intent must all appear in intent filter to match success. Intent can not specify category, and if category is not specified in intent, the system will automatically bring "Android.intent.category.DEFAULT" to it. Therefore, the component that wants to receive implicitly intent must take "intent" in the Android.intent.category.DEFAULT filter declaration in the manifest file. We can add category to intent by Addcategory method.

(4) Whether the query has a component that can receive the specified intent

An activity that is best suited to intent is obtained by using the Packagemanager resolveactivity or intent resolveactivity method. The queryintentactivities that calls Packagemanager returns all the activity that successfully matched intent. You can refer to the official documentation for a detailed definition of these methods, and we will not repeat them here.

The above is the entire content of this article, I hope to help you learn.

Related Article

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.