Intent and IntentFilter

Source: Internet
Author: User

Intent is used to start three components: Activity, Service, and BroadcastReceiver. It is also an important medium for communication between components.

Advantages of using Intent to start Components

1. Intent provides a consistent programming model for component startup. Whether the component you want to start is Activity, Service, or BroadcastReceiver, you can use Intent to encapsulate the startup Intent.

2. In some cases, an application only wants to start components with certain characteristics and does not want to be coupled with a specific component. Using Intent can easily achieve this high-level decoupling goal.

Component Attribute of Intent

The setComponent (ComponentNamecomp) method of the Intent object is used to set the Component Attribute of the Intent object. ComponentName contains the following constructor:

ComponentName (Stringpkg, String cls)

ComponentName (Contextpkg, String cls)

ComponentName (Contextpkg, Class cls)

According to the above constructor, to create a ComponentName object, you must specify the package name and class name -- this allows you to uniquely identify a component class, in this way, 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 code above creates an intent object and specifies the Component Attribute for it, which is equivalent to the following code:

Intent intent = newIntent (FirstActivity. This, SecondActivity. Class );

In addition to setComponent (), you can also use setClass () and setClassName () to explicitly specify the target component. You can also call the getComponent () method to obtain the ComponentName object encapsulated in Intent.

When the program starts the component in this form, it explicitly specifies the component class to be started in Intent. At this time, Intent is an explicit intent, and the explicit Intent application is narrow, this method is mostly used to start the component in this application, because you need to know the full qualified name of the target component class in advance. The implicit Intent uses the action, category, and data attributes in the Intent to specify certain conditions that the target component must meet. The system filters out the component that meets all the conditions, select the most appropriate component or select a component as the target component to start.

If the ComponentName attribute is specified in Intent, other Intent attributes are ignored.

Intent Action attribute

The action attribute is a string that represents a specific action. The Intent class predefines some action constants. You can also customize the action. Generally, a custom action should use the package name of the application as the prefix, and then append a specific uppercase string, such as "cn. Xing. Upload. Action. UPLOAD_COMPLETE "is a well-named action.

The setAction () method of the Intent class is used to set the action. The getAction () method can obtain the encapsulated action in the Intent.

Some pre-defined actions in the Intent class are as follows:

ACTION_CALL -- the target component is activity, which indicates the dialing action;

ACTION_EDIT -- the target component is activity, which indicates the action to display data to users for editing;

ACTION_MAIN -- the target component is activity, which indicates that it is started as the initial activity in the task;

ACTION_BATTERY_LOW -- the target component is broadcastReceiver, reminding the mobile phone that the power usage is too low;

ACTION_SCREEN_ON -- the target component is broadcast, which indicates that the screen is enabled.

Category attribute of Intent

The category attribute is also a string used to specify additional conditions required by some target components. An Intent object can contain any number of category attributes. The Intent class also predefines some category constants, and developers can also customize the category attribute.

The addCategory () method of the Intent class adds the Category attribute to the Intent class. The getCategories () method is used to obtain all the category encapsulated in the Intent class.

The pre-defined category in the Intent class is as follows:

CATEGORY_HOME -- indicates that the target activity must be an activity displaying homescreen;

CATEGORY_LAUNCHER -- indicates that the target activity can be used as the initial activity in the task stack. It is often used with ACTION_MAIN;

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

Data Attribute of Intent

The data attribute specifies the uri of the operated data. Data is often used with action. If action is ACTION_EDIT, the data value should indicate the URI of the edited document. If

Action is ACTION_CALL. The value of data should be a URI starting with "tel:" and appended with the number. If action is ACTION_VIEW, the value of data should be "http: "Start with and append the url uri...

The setData () method of the Intent class is used to set the data attribute. The setType () method is used to set the MIME type of data. The setDataAndType () method can set both. You can use the getData () method to obtain the value of the data attribute and the getType () method to obtain the MIME type of data.

Extra attribute of Intent

When starting a component through Intent, it is often necessary to carry some additional data. To carry data, you need to call the Intent putExtra () method. This method has multiple overload methods, which can be used to carry basic data types and their arrays, String types and their arrays, Serializable types and their arrays, parcelable type, its array, and Bundle type. The Serializable and Parcelable types represent a Serializable object. Bundle is similar to Map and can be used to store key-value pairs.

Flag attribute of Intent

The flag attribute is an int value that notifies the android system of how to start the target activity or what subsequent operations should be performed after the target activity is started. All flags are defined in the Intent class. Some common flags are as follows:

FLAG_ACTIVITY_NEW_TASK -- notifies the system to regard the target activity as the initial activity of a new task;

FLAG_ACTIVITY_NO_HISTORY -- the notification system should not place the target activity in the history stack;

FLAG_FROM_BACKGROUND -- the Intent of the notification system comes from the background operation, rather than the user's Direct selection...

IntentFilter class

The IntentFilter class indicates the Intent filter. In most cases, each component defines one or more intentfilters to indicate the Intent that can be processed.

Generally, the IntentFilter of component should be in AndroidManifest. Xml file.

Method defined:<Activity>, <author ER>, <service> Add one or more Child element. For example:

<! -- Declare the Activity as the 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 processes an implicit Intent, it compares the action, data, and category attributes of Intent and IntentFilter. If all three attributes are consistent, the component of IntentFilter can be used as a candidate for the target component (when multiple qualified component exists ).

1. Test the action attribute. Intent can define up to one action, while filter can define one or more actions.

The condition for test by action is: filter defines the intent action. For example, the intent action is "android. Intent. Action. MAIN, then defines "android. Intent. Action. MAIN "the filter of this action can pass the action test (of course, the filter can also contain more additional actions ).

If the filter does not define an action, the filter blocks all intents. If the intent does not define an action, you can test the action as long as the filter defines the action.

2. Test the category attribute. Intent can have multiple category or filter categories. The condition for testing by category is: filter defines all category of intent. For example, intent defines "android. Intent. Category. DEFAULT and cn. Xing. Intent. Category. If the two category attributes of UPLOAD are defined, the filter with the above two category attributes can pass the test (of course, the filter can also contain more additional category ).

According to the above rule, if a intent does not define category, all filters can pass the category test. But there is one exception: When you start an activity in startActivity (intent) mode, the system adds the intent value to android. Intent. Category. DEFAULT "category, which means that every activity that is expected to pass the category test must define" android "in its filter. Intent. Category. DEFAULT "(except for the activity used as the program entry ).

3. Test data attributes. Intent can define at most one data, and filter can define multiple data.

The conditions for passing the data test are:

A. If the intent does not specify data and data type, only filters with no data and datetype defined can pass the test;

B. If intent defines data and does not define ype, only filters with the same data definition but not datetype can pass the test;

C. If intent does not define data but defines datatype, only filters with the same datatype defined for undefined data can pass the test;

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

The data attribute is a URI that contains scheme, host, post, and path. The typical URI is:

Scheme: // host: port/path

Scheme, host, post, and path are optional. When comparing two data items, only the parts contained in the filter are compared. For example, if the scheme part is specified for a data filter, the test only compares the scheme part of the data. If the scheme part of the two is the same, it is regarded as "the same data ".

 

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.