data

本文介绍Android中如何使用<data>标签定义意图过滤器的数据规范,包括指定URI和MIME类型的方法,以及各属性的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SYNTAX:
<data android:scheme="string"
      android:host="string"
      android:port="string"
      android:path="string"
      android:pathPattern="string"
      android:pathPrefix="string"
      android:mimeType="string" />
CONTAINED IN:
<intent-filter>
DESCRIPTION:
Adds a data specification to an intent filter. The specification can be just a data type (the  mimeTypeattribute), just a URI, or both a data type and a URI. A URI is specified by separate attributes for each of its parts:

<scheme>://<host>:<port>[<path>|<pathPrefix>|<pathPattern>]

These attributes that specify the URL format are optional, but also mutually dependent:

  • If a scheme is not specified for the intent filter, all the other URI attributes are ignored.
  • If a host is not specified for the filter, the port attribute and all the path attributes are ignored.

All the <data> elements contained within the same <intent-filter> element contribute to the same filter. So, for example, the following filter specification,

<intent-filter . . . >
    <data android:scheme="something" android:host="project.example.com" />
    . . .
</intent-filter>

is equivalent to this one:

<intent-filter . . . >
    <data android:scheme="something" />
    <data android:host="project.example.com" />
    . . .
</intent-filter>

You can place any number of <data> elements inside an <intent-filter> to give it multiple data options. None of its attributes have default values.

Information on how intent filters work, including the rules for how Intent objects are matched against filters, can be found in another document, Intents and Intent Filters. See also the Intent Filters section in the manifest file overview.

ATTRIBUTES:
android:scheme
The scheme part of a URI. This is the minimal essential attribute for specifying a URI; at least one scheme attribute must be set for the filter, or none of the other URI attributes are meaningful.

A scheme is specified without the trailing colon (for example, http, rather than http:).

If the filter has a data type set (the mimeType attribute) but no scheme, the content: and file:schemes are assumed.

Note: Scheme matching in the Android framework is case-sensitive, unlike the RFC. As a result, you should always specify schemes using lowercase letters.

android:host
The host part of a URI authority. This attribute is meaningless unless a  scheme attribute is also specified for the filter.

Note: host name matching in the Android framework is case-sensitive, unlike the formal RFC. As a result, you should always specify host names using lowercase letters.

android:port
The port part of a URI authority. This attribute is meaningful only if the  scheme and  hostattributes are also specified for the filter.
android:path 
android:pathPrefix 
android:pathPattern
The path part of a URI which must begin with a /. The  path attribute specifies a complete path that is matched against the complete path in an Intent object. The  pathPrefix attribute specifies a partial path that is matched against only the initial part of the path in the Intent object. The pathPattern attribute specifies a complete path that is matched against the complete path in the Intent object, but it can contain the following wildcards:
  • An asterisk ('*') matches a sequence of 0 to many occurrences of the immediately preceding character.
  • A period followed by an asterisk (".*") matches any sequence of 0 to many characters.

Because '\' is used as an escape character when the string is read from XML (before it is parsed as a pattern), you will need to double-escape: For example, a literal '*' would be written as "\\*" and a literal '\' would be written as "\\\\". This is basically the same as what you would need to write if constructing the string in Java code.

For more information on these three types of patterns, see the descriptions of PATTERN_LITERAL,PATTERN_PREFIX, and PATTERN_SIMPLE_GLOB in the PatternMatcher class.

These attributes are meaningful only if the scheme and host attributes are also specified for the filter.

android:mimeType
A MIME media type, such as  image/jpeg or  audio/mpeg4-generic. The subtype can be the asterisk wildcard ( *) to indicate that any subtype matches.

It's common for an intent filter to declare a <data> that includes only the android:mimeTypeattribute.

Note: MIME type matching in the Android framework is case-sensitive, unlike formal RFC MIME types. As a result, you should always specify MIME types using lowercase letters.

INTRODUCED IN:
API Level 1
SEE ALSO:
<action> 
<category>

句法:
<data  android: scheme = " string " 
      android: host = " string " 
      android: port = " string " 
      android: path = " string " 
      android: pathPattern = " string " 
      android: pathPrefix = " string " 
      android: mimeType = " string "  />
包含在:
<意图过滤器>
描述:
添加一个数据规范的意图过滤器。该规范可以只是一个数据类型( mime类型属性),只是一个URI,或两者的数据类型和URI。一个URI是由它的每个部分分别指定属性:

<方案>:// <主机>:<端口> [<路径> | <pathPrefix> | <pathPattern>]

指定URL格式这些属性是可选的,但也是相互依存:

  • 如果方案 没有为意图过滤器指定,所有其他的URI属性被忽略。
  • 如果主机 没有为过滤器指定,端口属性,所有的路径属性都将被忽略。

所有<数据>包含在同一范围内的元素 <意图过滤器>元素提供相同的过滤器。因此,例如,下面的过滤器规格,

<意图过滤器。。。> 
    <数据 的android:计划= “东西”  机器人:主机= “project.example.com”  /> 
    。。。
</意图过滤器>

相当于这一个:

<意图过滤器。。。> 
    <数据 的android:计划= “东西”  /> 
    <数据 机器人:主机= “project.example.com”  /> 
    。。。
</意图过滤器>

您可以将任意数量的<数据>内部元素 <意向过滤器>给它的多个数据的选项。它的属性都没有默认值。

如何意图过滤器的工作,包括对intent对象如何与过滤器的匹配规则的信息,可以在另一个文件中,可以找到 意图和意图过滤器。又见 意图过滤器 清单文件概述部分。

属性:
机器人:方案
URI的方案组成部分。这是用于指定一个URI的最小必需属性; 的至少一个 方案属性必须为过滤器进行设置,或者没有其他的URI属性是有意义。

一个方案,但没有尾随冒号指定的(例如, HTTP,而不是HTTP: )。

如果过滤器具有数据类型集(mime类型 属性),但没有计划,内容:文件:假设方案。

:在Android框架方案的匹配是大小写敏感的,不像RFC。因此,你应该总是指定使用小写字母方案。

机器人:主机
一个URI权威的主机部分。此属性是毫无意义的,除非 方案属性也为过滤器中指定。

:在Android框架主机名匹配是大小写敏感的,不像正式的RFC。因此,你应该总是指定使用小写字母的主机名。

机器人:端口
一个URI港务局一部分。此属性才有意义,如果 方案和  主机的过滤器也被指定的属性。
机器人:路径 
的android:pathPrefix 
机器人:pathPattern
URI的路径组成部分,必须以/开始。该 路径属性指定打击的完整路径在一个Intent对象相匹配的完整路径。所述  pathPrefix属性指定针对仅在意图对象的路径的初始部分匹配部分路径。所述 pathPattern 属性指定针对意图对象的完整路径相匹配的完整路径,但它可以包含以下通配符:
  • 星号(“ * ”)匹配的0到紧接在前面的字符的许多事件的序列。
  • 一个时期后跟一个星号(“ * ”)匹配的0至多个字符的任意序列。

因为' \ '作为当字符串从XML读取(之前被解析为一个模式)转义字符,则需要仔细逃生:例如,一个字面' * '会被写成“ \\ * “与字面' \ '会被写成” \\\\ “。这基本上是一样的,你需要建造,如果在Java代码中的字符串写什么。

关于这三种模式的更多信息,请参阅的说明 PATTERN_LITERAL, PATTERN_PREFIXPATTERN_SIMPLE_GLOB在 PatternMatcher类。

这些属性是有意义的只有 方案主机 的过滤器也被指定的属性。

机器人:mime类型
MIME媒体类型,如 图像/ JPEG音频/ MPEG4-通用。子类型可以是星号通配符( *),以表示任何亚型相匹配。

是很常见的一个意图过滤器来声明一个<数据>只包括机器人:mime类型属性。

:在Android框架MIME类型匹配是大小写敏感的,不同于正式的RFC MIME类型。因此,你应该总是指定使用小写字母MIME类型。

介绍了:
API级别1
也可以看看:
<操作> 
<类别>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值