Intent and Intentfilter detailed

Source: Internet
Author: User
Tags home screen

Intent is used to initiate activity, Service, and broadcastreceiver three components, as well as an important medium for communication between components.

Advantages of using intent to start components
1, Intent provides a consistent programming model for component start-up. Whether the component you want to start is activity, Service, or broadcastreceiver, you can use the intent of the intent wrapper to start.
2, at some point, the application just wants to start a component with a certain feature and does not want to be coupled with a particular component. The use of intent can easily achieve this high-level decoupling purpose.

The component property of intent
The SetComponent (componentname comp) method of the intent object is used to set the component property of the intent. The componentname contains the following constructors:
ComponentName (String pkg, string cls)
ComponentName (Context pkg, String CLS)
ComponentName (Context pkg, class<?> CLS)
The above constructor shows that creating a ComponentName object requires specifying the package name and the class name-which uniquely identifies a component class so that the application can start a particular component based on the given component class. For example:
ComponentName comp = new ComponentName (firstactivity.this, Secondactivity.class);
Intent Intent = new Intent ();
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 = new Intent (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 get the intent object encapsulated in ComponentName.
When the program starts the component in this form, explicitly specifies the component class to be started in intent, at this time the intent is an explicit intent, the explicit intent application is relatively narrow, and is used to start the component in this application, Because this approach requires knowing the fully qualified name of the target component class in advance. The implicit intent uses the action, category, data property in intent to specify a number of conditions that the target component needs to satisfy, and the system filters out component that satisfy all the conditions. Choose the most appropriate component or the user chooses a component as the target component to start.
If the ComponentName attribute is specified in intent, the other properties of intent are ignored.

The Action property 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 the application package name and then attach a specific uppercase string, such as "Cn.xing.upload.action.UPLOAD_COMPLETE", which is a well-named action.
The Setaction () method of the intent class is used to set the action, and the Getaction () method can get the action encapsulated in the intent.
The following are some of the predefined action in the intent class:
The action_call--target component is activity and represents the dialing action;
action_edit--the target component is activity, which represents the action of displaying the data to the user for its editing;
The action_main--target component is activity, which indicates that the initial activity in the task starts;
action_battery_low--target component is broadcastreceiver, remind phone battery is too low;
The action_screen_on--target component is broadcast, which indicates that the screen is turned on.

Category attribute for intent
The Category property is also a string that specifies additional conditions that some target components need to meet. The intent object can contain any number of category attributes. The intent class also pre-defines some category constants, and developers can customize the category property.
The Addcategory () method of the intent class adds the Category property to intent, and the GetCategories () method is used to get all categories encapsulated in intent.
The following are some of the categories that are predefined in the intent class:
category_home--indicates that the target activity must be an activity that shows HOME screen;
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.

The data property of the intent
The Data property specifies the URI of the operation. Data is often used in conjunction with action, and if the action is Action_edit, the value of data should indicate the URI of the document being edited; If
Action is Action_call, and the value of data should be a URI that begins with "Tel:" and appends a number to it; If the action is Action_view, the value of data should be a URI that begins 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 through the GetData () method, and the MIME type of data is obtained through the GetType () method.

The extra property of intent
When starting a component through intent, it is often necessary to carry some extra data in the past. Carrying data requires calling intent's Putextra () method, which has multiple overloaded methods that can be used to carry the base data type and its array, the string type and its array, the serializable type and its array, the parcelable type and its array, Bundle types, and so on. Serializable and parcelable types represent a serializable object
, bundles are similar to map and can be used to store key-value pairs.

Flag attribute of Intent
The Flag property is an int value that informs the Android system how to start the target activity, or what follow-up action should be taken after initiating the target activity. All flags are defined in the intent class, and some of the commonly used flags are as follows:
The flag_activity_new_task--notification system takes the target activity as the initial activity of a new TASK;
flag_activity_no_history--notification system do not put the target ACTIVITY into the history stack;
flag_from_background--Notification System This intent originates from the background operation, not the user's direct choice ...

Intentfilter class
The Intentfilter class represents the intent filter, and in most cases each component defines one or more intentfilter that indicate the intent that it can handle.
In general, component's intentfilter should be defined in the Androidmanifest.xml file.
Defined method: Adds one or more <intent-filter> child elements in the <activity>, <receiver>, <service> element. Such as:
<!--declaring activity as a program entry--
<activity android: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 deals with implicit intent, it compares the action, data, category properties of intent and intentfilter, and if all 3 properties match, The component that the intentfilter belongs to can be a candidate for the target component (when there are multiple qualifying component).
1. Test the Action property. Intent can only define up to 1 actions, and filter can define 1 or more action.
The condition for passing the action test is: Filter defines the action of the intent. For example, intent's action is "Android.intent.action.MAIN", then the filter of the action that defines "Android.intent.action.MAIN" can pass the action test (of course, The filter can also contain additional action).
If the filter does not define an action, the filter blocks all intent. If intent does not define an action, then the action test can be passed as long as the filter defines the action.
2. Test the category attribute. Intent can be any number of category, filter can also be any category. The condition for passing the category test is: Filter defines all the category of intent. For example, intent defines the 2 category "Android.intent.category.DEFAULT" and "Cn.xing.intent.category.UPLOAD", The filter for the above 2 category attributes is defined to pass the test (of course, the filter can also contain additional category).
According to the above rules, if a intent does not have a category defined, all the filter can be tested by category. However, there is one exception: When you start an activity in startactivity (intent), the system adds a category with a value of "Android.intent.category.DEFAULT" for intent. This means that every activity that is expected to pass the category test will have to define "Android.intent.category.DEFAULT" in its filter (except as an activity at the entrance of the program).
3. Test the Data property. Intent can define a maximum of 1 data, and the filter defines multiple data.
The criteria for passing the data test are:
A. If intent does not specify the data and data type, only the filter with data and date type is not defined to pass the test;
B. If intent defines that data does not have a data type defined, only the filter with the same data defined and no date type is defined to pass the test;
C. If the intent does not have data defined and the data type is defined, only the filter that does not define data and defines the same data type can pass the test;
D. If intent defines both data and data type, only the filter with the same data and data type is defined to 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, if a data of filter only specifies the scheme part, the test is only compared with the scheme part of data, as long as the scheme part of the two is the same, it is considered as "the same data".


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Intent and Intentfilter detailed

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.