Intent-filter Data"scheme, host, port, MimeType, Path, Pathprefix, pathpattern"

Source: Internet
Author: User

I've never been very clear about the properties of the data tag in the Activity tab of the Androidmanifest.xml in the Intent-filter, looking at the Dev guide and searching the web for a lot of relevant information on the Internet, now the data tag attribute meaning to make a summary.

First, the definition

scheme, host, port, Path, Pathprefix, Pathpattern are used to match the Data Uri in Intent. The specific rules are as follows:

Scheme://host:port/path or Pathprefix or Pathpattern

It is important to note here that scheme is not schema, perhaps you remember Xmlns:android= "Http://schemas.android.com/apk/res/android" This statement, you will think of the schema (at least I think of ...--!), but the scheme here is not schema. While writing androidmanifest.xml, there are smart tips, but I hope you can still notice.

The last word "path or pathprefix or pathpattern" means that the following path validation can use Android:path, Android:pathprefix, or Pathpattern in the Data property, and you can add Add any of the data tags, because it is "or", so as long as any one of the data match, the system will choose your activity to start, of course, if other activity also has the same data tag, the system will give the user a Chooser Dialog.

MimeType is also used to match the Intent. For example, when you use Intent.settype ("Text/plain"), then the system will match all of the registered android:mimetype= "Text/plain" Activity, For more information about MimeType, please refer to: "Go" Backup: Android common MimeType table.

Here are the three ways to pay great attention to Intent.settype (), Intent.setdata,intent.setdataandtype ()!

    • SetType The MimeType after the call, and then sets the data to null;
    • SetData The data after the call, and then sets the mimeType to null;
    • Data and MimeType are set only after Setdataandtype is called.
It is also important to note that if you set both MimeType and scheme within the data tag, then your Intent needs to set both the matching data and mimeType to call Setdataandtype, the system can match to this Ac Tivity (even if your mimeType is set to "*/*"). Of course, if you do not set MimeType, then call SetData to match, if you set any mimeType will not match to that Activity.

Second, the difference  The main difference here is that PathPathprefixPathpatternThe difference between
    • path is used to match the complete path, such as: http://example.com/blog/abc.html, where path is set to/blog/abc.html to match;
    • Pathprefix is used to match the beginning of the path, and with the above Uri, the Pathprefix is set to/blog to match;
    • Pathpattern matches the entire path with an expression, which requires the matching symbol and escape.
Match symbol:
    1. "*" to match 0 or more, such as: "A *" can match "a", "AA", "AAA" ...
    2. "." to match any character, such as: "." Can Match "a", "B", "C" ...
    3. So ". *" is used to match any character 0 or more, such as: ". *html" can match "abchtml", "cHTML", "html", "sdf.html" ...
escaped:Because when the XML is read, "\" is treated as an escape character (before it is used as a pathpattern escape), so it needs to be escaped two times, the XML is read once, and used again in Pathpattern. such as: "*" This character should be written as "\\*", "\" This character should be written as "\\\\".


three or one some examples

   Example 1:If we want to match http with a ". pdf" End of the path, so that other programs want to open the network PDF, users can choose our program to download view.  We can set scheme to "HTTP", Pathpattern set to ". *\\.pdf", and the entire intent-filter is set to:View Code
1 <intent-filter>2     <action android:name= "Android.intent.action.VIEW" ></action>3     < Category android:name= "Android.intent.category.DEFAULT" ></category>4     <data android:scheme= "http" Android:pathpattern= ". *\\.pdf" ></data>5 </intent-filter>
Copy Code

If you only want to work with a PDF of a site, adding android:host= "yoursite.com" to the Data tab will only match http://yoursite.com/xxx/xxx.pdf, but that won't match Www.yoursite.com, if you also want to match this site, you will need to add a data tag, in addition to Android:host instead of "www.yoursite.com" the others are the same.

Example 2:If we're doing an IM app, or something like Weibo, how do you get someone else to call sharing via Intent in the selection box?
We only use registered Android.intent.action.SEND and MimeType as "Text/plain" or "*/*" on it, the whole intent-filter is set to:View Code
1 <intent-filter>2     <action android:name= "Android.intent.action.SEND"/>3     <category android: Name= "Android.intent.category.DEFAULT"/>4     <data mimetype= "*/*"/>5 </intent-filter>
Copy Code

The reason for setting the category here is that the default category for the created instance of Intent contains the Intent.category_default, which is why Google does this so that the Intent always has a category.


Example 3:If we are doing a music playback software, when the file browser opens a music file, so that our app can appear in the selection box?
This is similar to the file association, actually do it as above, also very simple, we only use registered Android.intent.action.VIEW and mimeType for "audio/*" on it, the whole intent-filter set to:View Code
1 <intent-filter>2     <action android:name= "Android.intent.action.VIEW"/>3     <category android: Name= "Android.intent.category.DEFAULT"/>4     <data android:mimetype= "audio/*"/>5 </intent-filter >
Copy Code

Reference:"1" Dev Guide http://developer.android.com/guide/topics/manifest/data-element.html "2" Backup: Android Common mimeType table/HTTP/ Www.cnblogs.com/newcj/archive/2011/08/10/2134305.html

Intent-filter Data"scheme, host, port, MimeType, Path, Pathprefix, pathpattern"

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.