1. Introduction to implicit intent
Explicit intent we mentioned earlier, such:
Intent intent = new intent ();
Intent. setclass (this, other. Class );
// This statement indicates the explicit intent, because the activation object is explicitly set to the other class
Startactivity (intent );
As the name implies, the implicit intention is to find the most matched component without explicitly setting the activation object. For example, five people:
(1) A: 170
(2) B: 160
3) C: 180
(4) D: 190
(5) E: 200
If it is explicit, if we want to specify a, it will say, "I chose. ", but if it is an implicit intention, it will say:" I want to select 170cm people ". Although it does not specify to select a, it will find the person with the most matching conditions.
In the intent filter, matching conditions similar to the "height" condition in the preceding example include:
(1) Action
(2) Category
(3) data: Scheme, host, path, Type
When the conditions for activating components are set in the program, the program will look for the most matched components, but note: as long as there is a bit of mismatch, it will not match;
For example:
Intent intent = new intent ();
Intent. setaction ("");
// This statement only specifies the action
Startactivity (intent );
// Find the most matched component for activation. Intent. addcategory ("android. Intent. Category. Default") is called internally ");
Ii. Core code of implicit intent
First, set the intent filter for an activity in androidmanifest. xml:
<Activity> <intent-filter> <action Android: Name = ".... "/> <category Android: Name = ".... "/> <category Android: Name =" android. intent. category. default "/> <! -- This sentence is generally added --> <data Android: Scheme = "... "Android: host = "... "Android: Path = "/... "Android: TYPE = "... "/> </intent-filter> </activity>
The preceding settings are used to set the attributes of the activity. The following conditions must be set in the program:
(1) intent = new intent ();
(2) intent. setaction ("....");
(3) intent. addcategory ("....");
(4) intent. setdata (URI. parse ("...."));
// Set the scheme, host, and path conditions for data.
(5) intent. setdataandtype (URI. parse (""), string type );
// Set the scheme, host, path, and type Conditions for data at the same time.
(6) startactivity (intent );
// Call intent. addcategory ("android. Intent. Category. Default ");
Iii. code example
Scenario: There is a button in mainactivity. After you click the button, the implicit intent match is performed, and the otheractivity is found and activated.
Case 1:
<activity android:name=".OtherActivity" android:label="OtherActivity" > <intent-filter> <action android:name="com.xiazdong.action" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="com.xiazdong.category" /> <data android:host="www.xiazdong.com" android:scheme="xiazdong"/> </intent-filter></activity>
The code is:
Intent intent = new intent (); intent. setaction ("com. xiazdong. action "); intent. addcategory ("com. xiazdong. category "); intent. setdata (URI. parse ("xiazdong: // www.xiazdong.com/xia"); startactivity (intent); // calls intent in this method. addcategory ("android. intent. category. default ");
Case 2:
In <DATA>, an Android: mimetype = "text/*" is added. Intent. setdata cannot be used, but intent. setdataandtype () must be used ();
<activity android:name=".OtherActivity" android:label="OtherActivity" > <intent-filter> <action android:name="com.xiazdong.action" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="com.xiazdong.category" /> <data android:host="www.xiazdong.com" android:scheme="xiazdong" android:mimeType="text/*"/> </intent-filter></activity>
Code:
Intent intent = new intent (); intent. setaction ("com. xiazdong. action "); intent. addcategory ("com. xiazdong. category "); intent. setdataandtype (URI. parse ("xiazdong: // www.xiazdong.com/xia"), "text/plain"); // matches text/* startactivity (intent); // calls intent in this method. addcategory ("android. intent. category. default ");