Intent and Intentfilter detailed

Source: Internet
Author: User

Intent is used to start Activity,service, as well as broadcastreceiver three of components, and is an important medium for communication between components.

Advantages of using intent to start components

1, Intent provides a consistent programming model for the startup of components. Regardless of whether the component you want to start is Activity,service or broadcastreceiver, you can use the intent of the intent package to start.

2, at some point, the application simply wants to start a component with a feature and does not want to be coupled with a particular component. The use of intent can easily achieve this high level decoupling purpose.

Component Properties of Intent

The SetComponent (Componentnamecomp) method of the intent object is used to set the component property of the intent. The componentname contains several constructors as follows:

ComponentName (stringpkg, String CLS)

ComponentName (contextpkg, String CLS)

ComponentName (contextpkg, Class CLS)

As the above constructors know, creating a ComponentName object requires specifying the package name and class name--this uniquely identifies a component class so that the application can start a specific component based on the given component class. For example:

ComponentName Comp =new componentname (firstactivity. This,secondactivity. Class);

Intent Intent = Newintent ();

Intent SetComponent (comp);

The above three code creates a intent object and assigns it a component property, which is exactly equivalent to the following code:

Intent Intent = newintent (firstactivity. This,secondactivity. Class);

In addition to using setcomponent (), you can use SetClass (), Setclassname () to explicitly specify the target component, and you can call the Getcomponent () method to obtain the intent object encapsulated in the componentname.

When the program starts the component in this form, it specifies the component class to be started explicitly in the intent, at this time the intent belongs to the explicit intent, the explicit intent application situation is narrow, and is used to start the component in this application. Because this way you need to know the fully qualified name of the target component class in advance. The implicit intent, through the action,category in intent, specifies some conditions that the target component needs to meet, and the system filters out the component that meet all the conditions, Select the most appropriate component or the user chooses a component to start as the target component.

If the ComponentName property is specified in intent, the other properties of intent are ignored.

The action attribute of the intent

The Action property is a string that represents a particular action. The intent class has predefined action constants, and developers can customize the action. In general, a custom action should be prefixed with a application package name, and then attach a specific uppercase string, such as "CN." Xing Upload Action Upload_complete "is a named good action.

The Setaction () method of the intent class is used to set the Action,getaction () method to get the action encapsulated in intent.

The following are the predefined partial action in the intent class:

The action_call--target component is an activity, which represents the dialing action;

action_edit--The target component is an activity, representing the action of displaying data to the user for editing;

The action_main--target component is an activity that is initiated as an initial activity in a task;

action_battery_low--target component is broadcastreceiver, remind mobile phone battery is too low;

The action_screen_on--target component is broadcast, which indicates that the screen is open.

Category Properties of Intent

The Category property is also a string that specifies the additional conditions that some target components need to meet. Intent objects can contain any number of category properties. The intent class also predefined some category constants, and developers can also customize category properties.

The Addcategory () method of the intent class adds category properties to the intent, and the GetCategories () method is used to get all intent encapsulated in the category.

The following is a predefined part of the intent class Category:

category_home--indicates that the target activity must be an activity showing homescreen;

category_launcher--indicates that the target activity can be used as the initial activity in the task stack, often in conjunction with Action_main;

category_gadget--indicates that the target activity can be embedded as part of another activity.

Intent's Data property

The Data property specifies the URI of the action. Data is often used in conjunction with the action, if the value of the action is action_edit,data should indicate the URI of the edited document; If

The value of the action for Action_call,data should be a URI that starts with "Tel:" and appends the number to it; If the value of the action is action_view,data should be a URI that starts with "http:" and then appends the URL ...

The SetData () method of the intent class is used to set the Data property, and the SetType () method is used to set the MIME type of data, and the Setdataandtype () method can set both. The value of the data property can be obtained by the GetData () method, and the MIME type of data is obtained by the GetType () method.

Extra properties of Intent

When starting a component via intent, it is often necessary to carry some additional data over. Carrying data requires calling the intent Putextra () method, which has multiple overloaded methods that can be used to carry basic data types and their arrays, string types and their arrays, serializable types and their arrays, parcelable types, and their arrays. Type of bundle. The serializable and parcelable types represent a serializable object, bundle similar to a map, and can be used to store key-value pairs.

Flag Properties of Intent

The Flag property is an int value that informs the Android system how to start a target activity, or what subsequent actions should be taken after the target is started. All flag are defined in the intent class, and some commonly used flag are as follows:

The flag_activity_new_task--notifies the system to take the target activity as the initial activity of a new TASK;

flag_activity_no_history--inform the system not to put the target activity into the historical stack;

flag_from_background--Notification System This intent from the background operation, not the direct choice of the user ...

Intentfilter class

The Intentfilter class represents the intent filter, and in most cases each component defines one or more intentfilter to indicate the intent that it can handle.

Generally speaking, component's intentfilter should be in Androidmanifest. defined in the XML file.

Defined method: Add one or more child elements to the <activity>,<receiver> <service> element. Such as:

<!--declaration as activity--> of program entry
<activityandroid:name= ". Firstactivity ">

<intent-filter>
<action android:name= "Android. Intent Action MAIN "/>
<category android:name= "Android. Intent Category LAUNCHER "/>
</intent-filter>
</activity>

Intentfilter and implicit intent

When the Android system handles implicit intent, it compares the Action,data, category properties of intent and Intentfilter, if all 3 of these properties match, The component that intentfilter belongs to can be a candidate for the target component (there are multiple qualifying component).

1. Test the Action property. The intent can define up to 1 action only, and filter can define 1 or more action.

The condition for the action test is: Filter defines the intent action. For example, intent's action is "Android." Intent Action MAIN ", then defines" Android. Intent Action MAIN "This action's filter can be tested by the action (of course, filter can also contain more extra action).

If filter does not define an action, this filter will block all intent. If intent does not define an action, then the action test can be done as long as the filter defines the action.

2. Test the Category property. Intent can be any number of category,filter can also be any category. The conditions for passing the category test are: Filter defines all the category of the intent. For example, intent defines "Android." Intent Category DEFAULT "and" CN. Xing Intent Category UPLOAD "These 2 category, the filter of the above 2 category properties is defined to pass the test (of course, the filter can also contain more extra category).

According to the rules above, if a intent does not define a category, all filter can pass the category test. But one exception: when starting an activity in startactivity (intent), the system adds a value of "Android" to intent. Intent Category DEFAULT "category, which means that every activity that expects to pass the category test is defined in its filter as" Android. " Intent Category DEFAULT "(in addition to activity as a program entry).

3. Test the Data property. Intent can be defined with up to 1 data,filter to define multiple data.

The conditions for passing the data test are:

A. If intent does not specify data and data type, then only the filter with no data and datetype is defined to pass the test;

B. If the intent defines the data without defining datatype, then only filter with the same data and no Datetype defined can pass the test;

C. If intent does not define the data but defines the datatype, then only the data is undefined and the same datatype filter is defined to pass the test;

D. If intent defines both data and datatype, only the same data and datatype filter can pass the test.

The Data property is a URI that contains scheme,host, post, and path, and the typical URI is:

Scheme://host:port/path

Scheme, host, post, and path are optional. When comparing 2 data, only the parts contained in the filter are compared. For example, one of the filter's data only specifies the scheme part, then the test only compares the scheme portion of data, as long as the scheme portion of the two is the same, it is considered "the same data".

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.