android Content Provider详解五

<style type="text/css"><!-- @page { margin: 2cm } H4 { margin-top: 0.49cm; margin-bottom: 0.51cm; background: transparent; border: none; padding: 0cm; direction: ltr; font-variant: normal; color: #000000; background: transparent; line-height: 155%; text-align: left; page-break-inside: avoid; widows: 0; orphans: 0; text-decoration: none; page-break-before: auto; page-break-after: auto } H4.western { font-family: "Cambria", serif; font-size: 14pt; so-language: en-US; font-style: normal } H4.cjk { font-family: "宋体"; font-size: 14pt; so-language: zh-CN; font-style: normal } H4.ctl { font-family: "Cambria", serif; font-size: 12pt; so-language: hi-IN; font-style: normal } P { margin-bottom: 0.21cm; background: transparent; border: none; padding: 0cm; direction: ltr; font-variant: normal; color: #000000; background: transparent; line-height: 0.18cm; text-align: left; page-break-inside: auto; widows: 0; orphans: 0; text-decoration: none; page-break-before: auto; page-break-after: auto } P.western { font-family: "Times New Roman", serif; font-size: 10pt; so-language: en-US; font-style: normal } P.cjk { font-family: "Times New Roman", serif; font-size: 10pt; so-language: zh-CN; font-style: normal } P.ctl { font-family: "Times New Roman", serif; font-size: 10pt; so-language: hi-IN; font-style: normal; font-weight: normal } A:link { text-decoration: none } A.ctl:link { so-language: zxx } --></style>
契约类

契约类定义了帮助应用使用content URIs的常量,列名,intent action,和其它contentprovider特性。契约类不会被provider自动包含;provider开发者必须定义它们然后使它们对其它开发者生效。Android平台中包含的很多provider都在包android.provider中有对应的契约类。

例如,用户词典Provider具有一个契约类UserDictionary,它包含了contentURIcolumnname常量。"words"表的contentURI定义在常量UserDictionary.Words.CONTENT_URI上。UserDictionary.Words类也包含了列名常量,在下面的代码片段中就用到了。举例,一个查询projection可以如下定义:

<style type="text/css"><!-- @page { margin: 2cm } P { margin-bottom: 0.21cm; background: transparent; border: none; padding: 0cm; direction: ltr; font-variant: normal; color: #000000; background: transparent; line-height: 0.18cm; text-align: left; page-break-inside: auto; widows: 0; orphans: 0; text-decoration: none; page-break-before: auto; page-break-after: auto } P.western { font-family: "Times New Roman", serif; font-size: 10pt; so-language: en-US; font-style: normal } P.cjk { font-family: "Times New Roman", serif; font-size: 10pt; so-language: zh-CN; font-style: normal } P.ctl { font-family: "Times New Roman", serif; font-size: 10pt; so-language: hi-IN; font-style: normal; font-weight: normal } A:link { text-decoration: none } A.ctl:link { so-language: zxx } --></style>

String[]mProjection =
{
UserDictionary.Words._ID,
UserDictionary.Words.WORD,
UserDictionary.Words.LOCALE
};

<style type="text/css"><!-- @page { margin: 2cm } P { margin-bottom: 0.21cm; background: transparent; border: none; padding: 0cm; direction: ltr; font-variant: normal; color: #000000; background: transparent; line-height: 0.18cm; text-align: left; page-break-inside: auto; widows: 0; orphans: 0; text-decoration: none; page-break-before: auto; page-break-after: auto } P.western { font-family: "Times New Roman", serif; font-size: 10pt; so-language: en-US; font-style: normal } P.cjk { font-family: "Times New Roman", serif; font-size: 10pt; so-language: zh-CN; font-style: normal } P.ctl { font-family: "Times New Roman", serif; font-size: 10pt; so-language: hi-IN; font-style: normal; font-weight: normal } A:link { text-decoration: none } A.ctl:link { so-language: zxx } --></style>

另一个契约类是用于联系人ProviderContactsContract类。此类的参考文档中包含示例代码片段。它的一个子类,ContactsContract.Intents.Insert,也是一个契约类,它包含了用于intentintent数据的契约。

<style type="text/css"><!-- @page { margin: 2cm } H4 { margin-top: 0.49cm; margin-bottom: 0.51cm; background: transparent; border: none; padding: 0cm; direction: ltr; font-variant: normal; color: #000000; background: transparent; line-height: 155%; text-align: left; page-break-inside: avoid; widows: 0; orphans: 0; text-decoration: none; page-break-before: auto; page-break-after: auto } H4.western { font-family: "Cambria", serif; font-size: 14pt; so-language: en-US; font-style: normal } H4.cjk { font-family: "宋体"; font-size: 14pt; so-language: zh-CN; font-style: normal } H4.ctl { font-family: "Cambria", serif; font-size: 12pt; so-language: hi-IN; font-style: normal } P { margin-bottom: 0.21cm; background: transparent; border: none; padding: 0cm; direction: ltr; font-variant: normal; color: #000000; background: transparent; line-height: 0.18cm; text-align: left; page-break-inside: auto; widows: 0; orphans: 0; text-decoration: none; page-break-before: auto; page-break-after: auto } P.western { font-family: "Times New Roman", serif; font-size: 10pt; so-language: en-US; font-style: normal } P.cjk { font-family: "Times New Roman", serif; font-size: 10pt; so-language: zh-CN; font-style: normal } P.ctl { font-family: "Times New Roman", serif; font-size: 10pt; so-language: hi-IN; font-style: normal; font-weight: normal } A:link { text-decoration: none } A.ctl:link { so-language: zxx } --></style>
引用MIME类型

Contentprovider可以返回标准的MIME类型或自定义的MIME类型字符串,或同时返回两者。

MIME类型具有以下形式

type/subtype

例如,世人皆知的MIME类型text/html具有text类型和html子类型。如果provider从一个URI返回此类型,这表示面向此URI的查询将返回带有HTML标记的文本。

自定义的MIME字符串,也被叫做"vendor-specific"MIME类型,具有更复杂的类型和子类型值,此类型的值总是这样

vnd.android.cursor.dir

用于多行,或这样

vnd.android.cursor.item

用于单行。

而子类型是每个provider都不相同的。Android内置的provider通常具有一个间单的字类型。例如,当联系人应用为一个电话号码创建一个新行时,它为新行设置下面的MIME类型:

vnd.android.cursor.item/phone_v2

可以看到子类型的值只是简单的phone_v2.

其它provider开发者可能基于providerauthority和表名字创建他们自己的子类型式样。例如,假设一个provider含有列车时刻表,providerauthoritycom.example.trains,并且它包含三个表:线路1,线路2,线路3。在URI的响应中,

content://com.example.trains/Line1

指向线路1表,provider返回对应的MIME类型为:

vnd.android.cursor.dir/vnd.example.line1

content URI

content://com.example.trains/Line2/5

指向线路2表的第5,provider返回的对应的MIME类型为:

vnd.android.cursor.item/vnd.example.line2

大多数contentproviders定义了契约类来包含它们所用到的MIME类型。例如,联系人Provider的契约类ContactsContract.RawContacts,定义了常量CONTENT_ITEM_TYPE它对应于一行原始的联系人数据。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值