Intent provides a common messaging system that allows you to see delivery intent in your application to perform actions and generate events, using intent to activate three types of core components of Android applications: Activity activities, service services, Broadcast recipient broadcast.
Intent also divides into the hermit intention and the display intention .
Display Intent: invoke the Intent.setcomponent (), Intent.setclassname (), or Intent.setclass () method to explicitly define the intent of the component name for display intent, Show intent to explicitly define which component to activate.
Hermit Intent: no explicit designation of the component; The following is a case of a hermit's intent.
You should first add the corresponding intent-filter in the manifest file
<application android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" > <activity Android:label= "@string/app_name" Android:name= ".
Intentactivity "> <intent-filter > <action android:name=" Android.intent.action.MAIN "/>
<category android:name= "Android.intent.category.LAUNCHER"/> </intent-filter> </activity> <activity android:name= ". Otheractivity "android:label=" @string/windowtitle "> <intent-filter > <action android:name=" cn.it Cast.zhangxx "/> <action android:name= cn.itcast.laoli"/> <category "android:name=" Egory.java "/> <category android:name=" Android.intent.category.DEFAULT "/> <category android:na Me= "cn.itcast.category.php"/> <data android:scheme= "itcast android:host=" www.itcast.cn "android:path="/limi
ng "/> <data android:mimetype=" image/* "/> </intent-filter> </activity> </application>
In the Mainactivity.java
public void openactivity (View v) {
/**
* (without data parameters) matches the action and category in intent as long as they appear in Intent-filter. Otherwise matching failure
/
Intent Intent = new Intent ()//implicit intent to activate activity
intent.setaction ("cn.itcast.zhangxx");
Intent.addcategory ("Cn.itcast.category.java");
Intent.setdata (Uri.parse ("itcast://www.itcast.cn/liming"));
Intent.settype ("Image/jpeg");
According to the Android API,
//this method automatically clears the any data that is previously set (for example by SetData (Uri)) .
That is, if you set the SetData method to set the Settype method, then the Settype method will clers out SetData method
//solution as follows:
Intent.setdataandtype ( Uri.parse ("itcast://www.itcast.cn/liming"), "Image/jpeg");
StartActivity (Intent)//Method Intent added Android.intent.category.DEFAULT category
}
The following are the matching rules for Hermit's intent:
Intent Filter (Intent filter) is actually used to match implicit Intent, when an intent object is tested by an intent filter, only three aspects are referred to: action, data (URI, and data type) and categories.
Action Tests (action test)
An Intent object can specify only one action name, while a filter may enumerate multiple action names. If the intent object or filter does not specify any actions, the result will be as follows:
- • If the filter does not specify any action, then all intents will be blocked, so all intents will fail to test. No intention of being able to pass through this filter.
- • On the other hand, as long as the filter contains at least one action, an intent object that does not specify the action automatically passes the test
Category Tests (Category test)
The category in the intent object must match the category in the filter for an intention to match the test by category. This filter can enumerate other categories, but it cannot omit any of the categories in this intent.
In principle, an intent object without a category should always be able to pass a matching test, regardless of what is in the filter. In most cases this is right. With one exception, Android treats all implicit intentions passed to startactivity () as they contain at least one category: "Android.intent.category.DEFAULT" (Category_default constant). Therefore, activities that want to receive implicit intent must include "Android.intent.category.DEFAULT" in their intent filters. (Exceptions are filters with "Android.intent.action.MAIN" and "Android.intent.category.LAUNCHER" settings)
Data test
When a URI in an intent object is used to compare to a URI in a filter, the various components of the URI are compared. For example, if the filter only specifies a scheme, all of the scheme's URIs can match this filter, and if the filter specifies a scheme, host name, and no path section, all URIs with the same scheme and host name can match this filter. Regardless of their path; If the filter specifies a scheme, host name, and path, only URIs with the same scheme, hostname, and path can match this filter. Of course, a path specification in a filter can contain wildcard characters so that only partial matching is required.
The data test compares the URI and data types specified in both the intent object and the filter.
The rules are as follows:
A. An intent object that contains neither a URI nor a data type can pass the test only if the filter also does not specify any URIs and data types.
B. An intent object that contains a URI but no data type can pass the test only if it matches a URI in its URI and a filter that also does not specify the data type. This usually occurs on a URIs similar to mailto: and Tel: They do not refer to actual data.
C. An intent object that contains a data type but does not contain a URI can pass the test only if the filter enumerates the same data types and does not specify a URI.
D. An intent object that includes both a URI and a data type (or a data type that can be inferred from a URI) can be tested if its type matches the type enumerated in the filter. It can also pass the test if its URI matches a URI in this filter or if it has a content contents: or file File:uri and the filter does not specify a URI. In other words, a component is assumed to support content: and file: Data if its filter enumerates only one data type.
The above is for Android intent detailed introduction, especially suitable for beginners to learn, I hope to help you learn.