`
yunlonglove
  • 浏览: 238966 次
社区版块
存档分类
最新评论

Android Content Providers(二)——Contacts Provider

 
阅读更多

Contacts Provider是Android中一个强大并且灵活的组件,负责管理系统通讯录的数据,对外提供访问接口来对系统通讯录进行访问和操作。

以下是Contacts Provider的组织结构图:


可以看出Android的系统通讯录是三层架构,通过URI进行访问。下面看一下每一层的类结构和官方解释:

ContactsContract.Contactstable

Rows representing different people, based on aggregations of raw contact rows.

ContactsContract.RawContactstable

Rows containing a summary of a person's data, specific to a user account and type.

ContactsContract.Datatable

Rows containing the details for raw contact, such as email addresses or phone numbers.


Contacts表包含了不同的联系人的记录,RawContacts表是联系人的数据集合,指定用户账号和类型,Data表是存储具体的联系人信息,包括邮件、电话号码等。


首先来看看Row Contacts,以下是官方解释:

A raw contact represents a person's data coming from a single account type and account name. Because the Contacts Provider allows more than one online service as the source of data for a person, the Contacts Provider allows multiple raw contacts for the same person. Multiple raw contacts also allow a user to combine a person's data from more than one account from the same account type.


大致意思是Row contact存储了用户数据所在服务器的账号和账号类型,由于Contacts Provider允许多个在线服务作为一个联系人的数据源,同时也允许用户使用同一个服务但是不同的账号来存储联系人信息。可以理解成Row Contacts存储的是用户的账户信息,包括账户类型和账户名称等。下面看看Row Contacts的表结构:

Column name

Use

Notes

ACCOUNT_NAME

The account name for the account type that's the source of this raw contact. For example, the account name of a Google account is one of the device owner's Gmail addresses. See the next entry forACCOUNT_TYPEfor more information.

The format of this name is specific to its account type. It is not necessarily an email address.

ACCOUNT_TYPE

The account type that's the source of this raw contact. For example, the account type of a Google account iscom.google. Always qualify your account type with a domain identifier for a domain you own or control. This will ensure that your account type is unique.

An account type that offers contacts data usually has an associated sync adapter that synchronizes with the Contacts Provider.

DELETED

The "deleted" flag for a raw contact.

This flag allows the Contacts Provider to maintain the row internally until sync adapters are able to delete the row from their servers and then finally delete the row from the repository.


另外,官方文档中还给出了一点说明:ACCOUNT_NAME存储用户的账户名,比如sanpleuser@gmail.com,ACCOUNT_TYPE是账户类型,比如如果是Google账户则存储的值就是com.google。


Most of the data for a raw contact isn't stored in theContactsContract.RawContactstable. Instead, it's stored in one or more rows in theContactsContract.Datatable. Each data row has a columnData.RAW_CONTACT_IDthat contains theRawContacts._IDvalue of its parentContactsContract.RawContactsrow.


大部分raw contacts的数据并没有直接存储在RowContacts表中,而是以一行或多行的形式存储在了Data表中,每一行有一列Data.RAW_CONTACT_ID包含了一个指向RawContacts表的列RawContacts._ID,也就是说RawContacts表存储的是引用。为了更好的理解Row Contacts是如何工作的,文档中举了一个例子:


To understand how raw contacts work, consider the user "Emily Dickinson" who has the following three user accounts defined on her device:

This user has enabledSync Contactsfor all three of these accounts in theAccountssettings.

Suppose Emily Dickinson opens a browser window, logs into Gmail asemily.dickinson@gmail.com, opens Contacts, and adds "Thomas Higginson". Later on, she logs into Gmail asemilyd@gmail.comand sends an email to "Thomas Higginson", which automatically adds him as a contact. She also follows "colonel_tom" (Thomas Higginson's Twitter ID) on Twitter.

The Contacts Provider creates three raw contacts as a result of this work:

  1. A raw contact for "Thomas Higginson" associated withemily.dickinson@gmail.com. The user account type is Google.
  2. A second raw contact for "Thomas Higginson" associated withemilyd@gmail.com. The user account type is also Google. There is a second raw contact even though the name is identical to a previous name, because the person was added for a different user account.
  3. A third raw contact for "Thomas Higginson" associated with "belle_of_amherst". The user account type is Twitter.

大致意思就是说一个用户拥有三个账号,两个是gmail账号,一个是twitter账号,然后登陆其中一个gmail账号并添加了一个联系人,然后他又登陆另一个gmail账号并且发了一封邮件给之前添加的联系人,随后变自动把这个联系人添加到了当前登陆的这个gmail账户里,并且在twitter上关注了这个联系人。随后在RawContacts里面便添加了三条数据,第一行是以第一个gmail账号登陆的账户名,账户类型是Google,第二行数据是以第二个gmail账户登陆的账户名,账户类型仍然为Google,第三行是Twitter的账号,账号类型为Twitter。


接下来看看Data是如何存储数据的,关于Data的说明,官方文档给出了如下解释:

Notice that different types of data are stored in this single table. Display name, phone number, email, postal address, photo, and website detail rows are all found in theContactsContract.Datatable. To help manage this, theContactsContract.Datatable has some columns with descriptive names, and others with generic names. The contents of a descriptive-name column have the same meaning regardless of the type of data in the row, while the contents of a generic-name column have different meanings depending on the type of data.


不同的数据类型存储在一张表中,名字、电话号码、邮箱、地址或者照片等都存储在ContactsContract.Data表中,为了方便管理数据,Data表有一些描述性的列,还有其他一些一般的列。描述性列的内容不管数据的类型都有同样的意思,但是一般性的列的内容就会依据不同的数据类型有不同的意思。


Some examples of descriptive column names are:

RAW_CONTACT_ID

和RawContact的_ID对应的值

MIMETYPE

The type of data stored in this row, expressed as a custom MIME type. The Contacts Provider uses the MIME types defined in the subclasses ofContactsContract.CommonDataKinds. These MIME types are open source, and can be used by any application or sync adapter that works with the Contacts Provider.

MIMETYPE定义了数据的类型,通过CommonDataKinds指定数据的类型。

Generic column names

There are 15 generic columns namedDATA1throughDATA15that are generally available and an additional four generic columnsSYNC1throughSYNC4that should only be used by sync adapters. The generic column name constants always work, regardless of the type of data the row contains.

TheDATA1column is indexed. The Contacts Provider always uses this column for the data that the provider expects will be the most frequent target of a query. For example, in an email row, this column contains the actual email address.

By convention, the columnDATA15is reserved for storing Binary Large Object (BLOB) data such as photo thumbnails.

从DATA1到DATA15有15个字段来存储值,DATA1一般是索引列,Contacts Provider通常用这个索引列来进行目标查询。例如,在邮件行中,这一列存储实际的邮件地址。另外,DATA15是用来存储BLOB(Binary Large Object)类型数据的,比如用户头像。


这有一个例子来说明ContactsContract.CommonDataKinds;

For example, theContactsContract.CommonDataKinds.Emailclass defines type-specific column name constants for aContactsContract.Datarow that has the MIME typeEmail.CONTENT_ITEM_TYPE. The class contains the constantADDRESSfor the email address column. The actual value ofADDRESSis "data1", which is the same as the column's generic name.

通过下面的图可以有一个比较直观的认识:


CommonDataKinds.Email存储了字段名常量,而对应的ContactsContract.Data则存储了实际的值,二者通过RAW_CONTACT_ID对应同一个RawContact.这里DATA1存储的就是Address(邮件地址)


以下是其他一些CommonDataKinds的类型:

Mapping class

Type of data

Notes

ContactsContract.CommonDataKinds.StructuredName

The name data for the raw contact associated with this data row.

A raw contact has only one of these rows.

ContactsContract.CommonDataKinds.Photo

The main photo for the raw contact associated with this data row.

A raw contact has only one of these rows.

ContactsContract.CommonDataKinds.Email

An email address for the raw contact associated with this data row.

A raw contact can have multiple email addresses.

ContactsContract.CommonDataKinds.StructuredPostal

A postal address for the raw contact associated with this data row.

A raw contact can have multiple postal addresses.

ContactsContract.CommonDataKinds.GroupMembership

An identifier that links the raw contact to one of the groups in the Contacts Provider.

Groups are an optional feature of an account type and account name. They're described in more detail in the sectionContact groups.


未完待续。。。


欢迎关注我的新浪微博和我交流:@唐韧_Ryan

分享到:
评论

相关推荐

    platform_packages_providers_contactsprovider

    总的来说,`platform_packages_providers_contactsprovider`是Android系统中连接应用程序和联系人数据的核心组件,它通过Content Provider接口提供了对联系人数据的强大支持,并且考虑到隐私和安全因素,为用户提供...

    android ContentProviders

    Android中的ContentProviders是Android平台用来提供不同类型数据(比如音频、视频、图片、个人联系人等)的一个统一接口。ContentProviders使用URI(统一资源标识符)来识别数据集,并允许一个应用程序存储和检索...

    Android学习笔记之——Content Providers

    本博文学习一下Content Providers(内容提供器) 目录 Android运行时 (Android runtime)权限 在程序运行时申请权限 内容提供器简介 访问其他程序中的数据 Android运行时 (Android runtime)权限 Android开发...

    Android的Content ProviderS说明文档

    在Android系统中,Content Provider扮演着至关重要的角色,它是四大组件之一,负责数据的共享与交换。本说明文档将深入探讨Content Provider的概念、工作原理、使用方法以及它在实际开发中的应用。 一、Content ...

    Content Providers(android)

    Content Providers(android)

    Content_Providers

    Content Providers作为Android四大组件之一(Activity、Service、Broadcast Receiver、Content Provider),主要负责存储和检索结构化数据,比如联系人信息、日历事件等。它们通过标准的URI(Uniform Resource ...

    android provider

    - Android的内置Content Providers,如Contacts Provider、Media Store等,供开发者使用。 - 自定义Content Provider可以用于分享应用内的数据,比如日历事件、书签等。 8. **最佳实践** - 优化查询性能,避免...

    Android 系统应用 Contacts 源码

    ContentProvider类`com.android.providers.contacts.ContactsProvider2`是Contacts应用的核心。它实现了SQLite数据库操作,用于存储、读取和更新联系人信息。在源码中,我们可以看到如何使用SQLiteOpenHelper来创建...

    android Contacts 通讯录源码

    Android系统通过ContentProvider来统一管理数据访问,Contacts应用的核心在于`com.android.providers.contacts`包中的`ContactsProvider`类。这个类实现了ContentProvider接口,提供了对外的数据接口,包括增删查改...

    Android应用源码之30.Content_Providers(2).zip

    在Android应用开发中,Content Providers扮演着至关重要的角色。它们是Android系统中数据共享和交换的核心组件,使得不同应用程序之间可以安全地访问和操作数据。本资料主要关注Android应用源码中的Content ...

    Content-Providers(1).rar_android_content provider

    关于Content Providers Content Providers 是所有应用程序之间数据存储和检索的一个桥梁,作用是使得各个...把18.SQLite和22.22.Content Provider添加到项目中,先运行18.SQLite,然后在进行22.Content Provider测试

    Content Provider

    ### Content Provider 在 Android 中的应用 #### 一、概述 Content Provider 是 Android 四大组件之一,主要用于实现跨应用之间的数据共享。在 Android 开发中,应用程序通常需要与系统或其他应用程序进行数据交互...

    android联系人源码

    platform_packages_providers_contactsprovider platform_packages_providers_contactsprovider platform_packages_providers_contactsprovider platform_packages_providers_contactsprovider

    Android 源码分析_Contacts

    1. 数据存储:联系人的数据存储在ContentProvider中,主要在`com.android.providers.contacts`包下。ContentProvider通过SQLite数据库管理联系人信息,包括姓名、电话号码、电子邮件地址等。 2. 数据访问接口:...

    Android4.1 Contacts源码

    Content Providers是Android中负责数据共享的关键组件,Contacts Provider就是其中之一,它负责管理联系人数据库,提供数据读写接口。 在源码中,我们可以看到Contacts Provider主要由两个表构成:Contacts表和...

    android理论学习——基本概念

    本篇文章将深入探讨"android理论学习——基本概念"中的三个关键要素:Manifest、Content Providers以及Intent和Intent-filter。这些元素构成了Android应用程序的基础架构,使得开发者能够构建功能丰富的移动应用。 ...

    Mediaprovider简介

    Mediaprovider的简单说明。 MediaProvider包括五个类: com.android.providers.media.MediaProvider com.android.providers.media.MediaScannerCursor ...com.android.providers.media.MediaThumbRequest

    Android应用源码之29.Content_Providers(1).zip

    本资料包“Android应用源码之29.Content_Providers(1).zip”显然是为了帮助开发者理解和实践Content Provider的用法。以下是关于Content Providers的详细知识: 1. **Content Providers基础** - **定义**:Content...

Global site tag (gtag.js) - Google Analytics