Android addCategory () and other instructions, androidaddcategory

Source: Internet
Author: User

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
  1. <Activity>
  2. <Intent-filter>
  3. <Action android: name = "..."/>
  4. <Category android: name = "..."/>
  5. <Category android: name = "android. intent. category. DEFAULT"/> <! -- This sentence is generally added -->
  6. <Data android: scheme = "... "android: host = "... "android: path = "/... "android: type = "... "/>
  7. </Intent-filter>
  8. </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
  1. <Activity
  2. Android: name = ". OtherActivity"
  3. Android: label = "OtherActivity">
  4. <Intent-filter>
  5. <Action android: name = "com. xiazdong. action"/>
  6. <Category android: name = "android. intent. category. DEFAULT"/>
  7. <Category android: name = "com. xiazdong. category"/>
  8. <Data
  9. Android: host = "www.xiazdong.com"
  10. Android: scheme = "xiazdong"/>
  11. </Intent-filter>
  12. </Activity>

The code is:

[Java]View plaincopy
  1. Intent intent = new Intent ();
  2. Intent. setAction ("com. xiazdong. action ");
  3. Intent. addCategory ("com. xiazdong. category ");
  4. Intent. setData (Uri. parse ("xiazdong: // www.xiazdong.com/xia "));
  5. 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
  1. <Activity
  2. Android: name = ". OtherActivity"
  3. Android: label = "OtherActivity">
  4. <Intent-filter>
  5. <Action android: name = "com. xiazdong. action"/>
  6. <Category android: name = "android. intent. category. DEFAULT"/>
  7. <Category android: name = "com. xiazdong. category"/>
  8. <Data
  9. Android: host = "www.xiazdong.com"
  10. Android: scheme = "xiazdong" android: mimeType = "text/*"/>
  11. </Intent-filter>
  12. </Activity>

Code:

[Java]View plaincopy
  1. Intent intent = new Intent ();
  2. Intent. setAction ("com. xiazdong. action ");
  3. Intent. addCategory ("com. xiazdong. category ");
  4. Intent. setDataAndType (Uri. parse ("xiazdong: // www. xiaz

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.