Intent matching rules and intent matching rules
This article is translated from the Android API Guide and has been sorted out.
Intent matching rules: perform three tests: action, data, and category.
1. Test the action:
A filter can define zero or multiple actions. If Intent does not contain an action, it passes this test. if the Intent contains the action, it must match (the same) with the action in the filter to pass the test. if the filter does not define action, no Intent can pass the test (including the Intent that does not define the action ).
2. category test:
One filter can define zero or multiple category items. If Intent does not define category, it passes the test, if category is defined in Intent, each of the category must be matched in the filter to pass the test. it should be noted that the implicit Intent passed in through startActivity () or startActivityForResult () automatically contains CATEGORY_DEFAULT during the test. Therefore, if the application wants to receive the implicit Intent, it must contain "Android. intent. category. DEFAULT ".
3. data testing:
A filter can define zero or more data. Each data can define a mimeType and a composition of a URI struct. URI struct:
<Scheme>: // These four fields are optional, but if scheme is not defined, the host is ignored. If the host is not defined, the port is ignored, if neither scheme nor host is defined, the path will be ignored. path can contain the wildcard "*".
When the URI in the Intent is compared with the URI in the filter, only the part defined in the filter is compared.
When the URI and MIMETYPE In the Intent are compared with those in the filter:
A. When the Intent does not contain any URI or MIMETYPE, the test is passed only when the filter does not define any URI or MIMETYPE.
B. When the Intent only contains the URI that does not contain MIMETYPE, only the URI in the Intent matches the URI of the filter and the filter does not define MIMETYPE.
C. When Intent only contains MIMETYPE, only the MIMETYPE in Intent matches the MIMETYPE of the filter and the filter does not define the URI.
D. when the Intent contains both URI and MIMETYPE, perform two tests: one is to test whether the MIMETYPE In the Intent matches the MIMETYPE in the filter, and the other is to test whether the URI matches, if the filter does not define a URI, test when the URI in the Intent contains "file:" or "content. note that if the filter has a defined URI, even if the URI in the Intent contains "file:" or "content:", it cannot pass the test and must be matched.
Intent content settings:
Set action to use setAction
Set category to use addCategory. You can add multiple
Set URI separately using setData
Set MIMETYPE to use setType separately
Set both URI and MIMETYPE to use setDataAndType