Android Content Providers(二)——Contacts Provider

article/2025/10/20 10:30:20

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:

  • emily.dickinson@gmail.com
  • emilyd@gmail.com
  • Twitter account "belle_of_amherst"

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


http://chatgpt.dhexx.cn/article/VxiaYr6f.shtml

相关文章

API Guides Contacts Provider (二)

Data From Sync Adapters 用户直接输入联系人的数据到设备中,但是也可以通过sync adapters从服务器上获取联系人的数据。sync adapter 会自动同步设备和服务器上的数据。sync adapter运行在后台,由系统来控制。系统调用ContentResolver去管理数据。 在A…

Android 7.0后SettingProvider ContactsProvider TelephonyProvider MediaProvider数据库位置

在Android7.0之后,很多Provider数据库的位置都发生了改变,在这记录下,免得以后又忘记了,找起来费劲。 1、SettingsProvider 在之前SettingsProvider是是以settings.db的方法存在,在Android N之后SettingsProvider数据存…

利用Contacts Provider读取手机联系人信息

参考:https://developer.android.google.cn/guide/topics/providers/contacts-provider.html Contacts Provider组织结构 Contacts Provider组织结构由三部分构成,如下图所示: (联系人)Contact:代表联系人,包含了多种联系渠道。(原始联系人)RawContact:每个原始联…

API Guides Contacts Provider

Contacts Provider Contacts Provider是Android的一个强大组件,它管理联系人的核心数据。你在手机联系人看到联系人信息,来源于Contact Provider。当然,你可以在自己的应用用访问ContactProvider的数据,也可以同步手机和服务…

Android官方文档—APP组件(Content Providers)(Contacts Provider)

通讯录内容提供者 Contacts Provider是一个功能强大且灵活的Android组件,用于管理设备的人员数据中​​央存储库。联系人提供程序是您在设备的联系人应用程序中看到的数据源,您还可以在自己的应用程序中访问其数据,并在设备和在线服务之间传…

理清contactsprovider

初步了解android contact provider android的联系人数据单独拿出来做成ContactsProvider,众多的table和view整体看下来,使得联系人的数据错综复杂。但是我们在开发的过程并不需要将所有的table都搞清楚。用到最多的是 ContactsContract.Contacts、Contac…

Android ContactsProvider源码分析

Android源码目录packages\providers下的应用是下载,通话等内置基本应用提供数据存储和操作的provider应用,本文章将针对ContactsProvider源码的架构和实现展开分析。(注:本文使用使用android4.0版本进行分析) 1、架构设…

ContactsProvider2

本篇不全也不细,只是根据按照个人理解和工作中遇到的问题,总结了个人认为的要点。 1. Android的数据库体系 1.1. 概述1.2 uri结构 2. ContactsProvider2 2.1. 概述2.2. Contacts2.db中的表2.3. ContactProvider2中的实现2.4. 批量访问 1. Android的数…

Android ContactProvider码源解析

Android Contacts源码解析2 4 ContactsProvider模块 1ContactsProvider简介2数据库创建3主要数据库的表结构相互关系 1data表2raw_contacts表3contacts表4mimetypes5其他表 文章转载自:https://blog.csdn.net/kafka_88/article/details/58585607 4,…

Contacts Provider基础

作为四大组件之一的ContentProvider工作中我们很少会用到自己自定义的ContentProvider,用到的最多的就是系统提供的。官方文档提供了两种系统ContentProvider,一种是CalendarProvider,一种是Contacts Provider。今天我们的主角就是Contact Provider。 The Contacts Provider is…

Error: invalid code, hints: [ req_id: * ]

1、问题描述:这个问题是在处理订单支付时需要用户登录遇到的,具体报错信息如下: 2、报错原因: 当前开发者的appid没有支付权限导致 3、解决方案: 需要负责人给分配对应的权限

This application’s bundle identifier does not match its code signing identifier.

今天使用carthage更新第三方后莫名出现真机云心失败,提示 This application’s bundle identifier does not match its code signing identifier. 解决方法: /usr/local/bin/carthage copy-frameworks 进入Building Phases 单击并在New Run Script Phase中添加脚本 将/usr/lo…

Code Sign error: a valid provisioning profile matching the application's Identifier 'com.yourcompany

出现这个错误的原因是因为:appid和provisioning profile不匹配 !有两种解决的办法:重新下载provisioning profile,或者可能因为:生成证书和appid 所用的根证书不同,最好重新都生成然后下载。(这…

ANTD react 手机号(验证码)登陆 + 账号登陆(图形验证码)

这种页面可能是大家常用的,但重写比较费时间,之前没有搜到完整的,在这里自己总结一下,方面复用 代码: <LoginFormformRef{formRef}initialValues{{autoLogin: false}}onFinish{async values > {await handleSubmit(values as LoginParams)}}><Tabs activeKey{type…

Springboot+axios+vue使用VerifyCodeUtils工具类实现验证码图片功能

一、环境准备 ideajava 1.8maven 3.6.3操作系统&#xff1a;window10vue.min.jsaxios.min.js 二、VerifyCodeUtils工具类 import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import ja…

Identity and Authentication - Common Authentication Methods

Username and Passwords This is the most common method of identifying users in the age of Software as a Service (SaaS) 解释上面的图中的步骤&#xff1a; Login Request: POST /login postuser {username: users,password: pws }Find users: SELECT * FROM databa…

iOS code signing identity 配置

PROJECT 和 TARGET 中都要选:code signing identity: 调试统一都选 developer, 发布统一都选 distribution provisioning Profile 也要选. 过期和无效的证书及时删除, 避免 ambiguous 警告. 选择正确的证书配置后仍然报错, clean 一下重启 xcode 还不行~~~重启电脑

Invalid CAPTCHA response. Please try again. (Code: 1201)

项目场景&#xff1a; Invalid CAPTCHA response. Please try again. (Code: 1201) 解决方案&#xff1a; 直接使用隐私浏览器打开即可。

经典的Embedding方法Word2vec

提起Embedding,就不得不提Word2vec,它不仅让词向量在自然语言处理领域再度流行&#xff0c;更为关键的是&#xff0c;自2013年谷歌提出Word2vec以来&#xff0c;Embedding 技术从自然语言处理领域推广到广告、搜索、图像、推荐等深度学习应用领域&#xff0c; 成了深度学习知识…

embedding表示方法及原理

目录 1.前言2.embedding表示方法2.1 word2vec embedding2.2 neural network embedding2.3 graph embedding 3.参考文献 1.前言 近几年embedding的使用及优化在各种比赛、论文中都有很多的应用&#xff0c;使用embedding表示特征的空间表示也在各种应用中确定是一种很有效的特征…