Android addCategory () and other instructions, androidaddcategory
1. Introduction to implicit intent
Explicit intent we mentioned earlier, such:
Intent intent = new Intent ();
Intent. setClass (this, Other. class); // this sentence indicates an explicit intent, because it explicitly sets the activation object to an 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 ("a"); // This sentence 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:
[Html]View plaincopy
- <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 intent = new Intent ();
(2) intent. setAction ("....");
(3) intent. addCategory ("....");
(4) intent. setData (Uri. parse ("..."); // you can specify the scheme, host, and path conditions for data.
(5) intent. setDataAndType (Uri. parse (""), String type); // set the scheme, host, path, and type Conditions of 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:
[Html]View plaincopy
- <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:
[Java]View plaincopy
- 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); // call intent. addCategory ("android. intent. category. DEFAULT") in this method ");
Case 2:
In <data>, an android: mimeType = "text/*" is added. intent. setData cannot be used, but intent. setDataAndType () must be used ();
[Html]View plaincopy
- <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:
[Java]View plaincopy
- Intent intent = new Intent ();
- Intent. setAction ("com. xiazdong. action ");
- Intent. addCategory ("com. xiazdong. category ");
- Intent. setDataAndType (Uri. parse ("xiazdong: // www. xiaz