Android向所有被赋予READ_CONTACTS权限的应用程序提供了联系人信息数据库的完全访问权限。Contacts Contract使用3层数据模型去存储数据,下面介绍Contacts Contract的子类:

1.Data 表中的每行都定义了个人的数据集(电话号码,email地址,等等),用MIME类型区分开。尽管有为每个个人数据的类型预定义可用的列名(ContactsContract.CommonDataKinds里装有合适的MIME类型),此表能被用来存储任何值。

当向Data表中加入数据的时候,你需要指定与数据集相关的Raw Contact。

2.RawContacts 从Android 2.0(API 5)向前,用户可以增加多个联系人账户的Provider。RawContacts表中的每行定义了一个与数据集相关的账户。说白了就是账户信息。

3.Contacts Contacts表的行聚合了RawContacts中的数据,每行代表了不同的人信息。

读取联系人详情:

首先在庆典文件中添加权限:<uses-permission android:name=”android.permission.READ_CONTACTS”/>

然后是访问Contacts Contract Content Provider:

private String[] getNames() {
/**
* Listing 8-36: Accessing the Contacts Contract Contact Content Provider
*/
// Create a projection that limits the result Cursor
// to the required columns.
String[] projection = {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
}; // Get a Cursor over the Contacts Provider.
Cursor cursor =
getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
projection, null, null, null); // Get the index of the columns.
int nameIdx =
cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME);
int idIdx =
cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID); // Initialize the result set.
String[] result = new String[cursor.getCount()]; // Iterate over the result Cursor.
while(cursor.moveToNext()) {
// Extract the name.
String name = cursor.getString(nameIdx);
// Extract the unique ID.
String id = cursor.getString(idIdx); result[cursor.getPosition()] = name + " (" + id + ")";
} // Close the Cursor.
cursor.close(); //
return result;
}

找到联系人姓名的联系信息:

 private String[] getNameAndNumber() {
/**
* Listing 8-37: Finding contact details for a contact name
*/
ContentResolver cr = getContentResolver();
String[] result = null; // Find a contact using a partial name match
String searchName = "andy";
Uri lookupUri =
Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_FILTER_URI,
searchName); // Create a projection of the required column names.
String[] projection = new String[] {
ContactsContract.Contacts._ID
}; // Get a Cursor that will return the ID(s) of the matched name.
Cursor idCursor = cr.query(lookupUri,
projection, null, null, null); // Extract the first matching ID if it exists.
String id = null;
if (idCursor.moveToFirst()) {
int idIdx =
idCursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID);
id = idCursor.getString(idIdx);
} // Close that Cursor.
idCursor.close(); // Create a new Cursor searching for the data associated with the returned Contact ID.
if (id != null) {
// Return all the PHONE data for the contact.
String where = ContactsContract.Data.CONTACT_ID +
" = " + id + " AND " +
ContactsContract.Data.MIMETYPE + " = '" +
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE +
"'"; projection = new String[] {
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
}; Cursor dataCursor =
getContentResolver().query(ContactsContract.Data.CONTENT_URI,
projection, where, null, null); // Get the indexes of the required columns.
int nameIdx =
dataCursor.getColumnIndexOrThrow(ContactsContract.Data.DISPLAY_NAME);
int phoneIdx =
dataCursor.getColumnIndexOrThrow(
ContactsContract.CommonDataKinds.Phone.NUMBER); result = new String[dataCursor.getCount()]; while(dataCursor.moveToNext()) {
// Extract the name.
String name = dataCursor.getString(nameIdx);
// Extract the phone number.
String number = dataCursor.getString(phoneIdx); result[dataCursor.getPosition()] = name + " (" + number + ")";
} dataCursor.close();
} return result;
}

Contacts子类还提供了电话号码查找URI,用来帮助找到与特定电话号码相关的联系人,执行呼叫者ID查找:

private String performCallerId() {
/**
* Listing 8-38: Performing a caller-ID lookup
*/
String incomingNumber = "(650)253-0000";
String result = "Not Found"; Uri lookupUri =
Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
incomingNumber); String[] projection = new String[] {
ContactsContract.Contacts.DISPLAY_NAME
}; Cursor cursor = getContentResolver().query(lookupUri,
projection, null, null, null); if (cursor.moveToFirst()) {
int nameIdx =
cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME); result = cursor.getString(nameIdx);
} cursor.close(); return result;
}

使用Intent创建和选择联系人,这是一种最佳的实践做法,其优势在于用户在执行相同任务的时候看到的一致的界面,这可以避免用户感到混淆,并且改善用户体验,下面是悬着一个联系人:

  private static int PICK_CONTACT = 0;

  private void pickContact() {
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}

当选择好之后,作为返回Intent的data属性内的URI返回:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((requestCode == PICK_CONTACT) && (resultCode == RESULT_OK)) {
resultTextView.setText(data.getData().toString());
}
}

插入新联系人:,当然这需要一个权限:

<uses-permission android:name="android.permission.WRITE_CONTACTS"/>

之后就可以插入联系人啦!

Intent intent =
new Intent(ContactsContract.Intents.SHOW_OR_CREATE_CONTACT,
ContactsContract.Contacts.CONTENT_URI);
intent.setData(Uri.parse("tel:(650)253-0000")); intent.putExtra(ContactsContract.Intents.Insert.COMPANY, "Google");
intent.putExtra(ContactsContract.Intents.Insert.POSTAL,
"1600 Amphitheatre Parkway, Mountain View, California"); startActivity(intent);

使用Contacts Contract Content Provider操作通讯录最佳实践的更多相关文章

  1. 我的Android 4 学习系列之数据库和Content Provider

    目录 创建数据库和使用SQLite 使用Content Provider.Cusor和Content Value来存储.共享和使用应用程序数据 使用Cursor Loader异步查询Content P ...

  2. Android Contacts (android通讯录读取)-content provider

    Content Provider 在数据处理中,Android通常使用Content Provider的方式.Content Provider使用Uri实例作为句柄的数据封装的,很方便地访问地进行数据 ...

  3. Android开发-API指南-Content Provider基础

    Content Provider Basics 英文原文:http://developer.android.com/guide/topics/providers/content-provider-ba ...

  4. Content Provider Basics ——Content Provider基础

    A content provider manages access to a central repository of data. A provider is part of an Android ...

  5. Android Content Provider简介

    Content Provider是Android的四大组件之一,与Activity和Service相同,使用之前需要注册: Android系统中存在大量的应用,当不同的应用程序之间需要共享数据时,可以 ...

  6. Android学习总结——Content Provider

    原文地址:http://www.cnblogs.com/bravestarrhu/archive/2012/05/02/2479461.html Content Provider内容提供者 : and ...

  7. Android开发学习之路--Content Provider之初体验

    天气说变就变,马上又变冷了,还好空气不错,阳光也不错,早起上班的车上的人也不多,公司来的同事和昨天一样一样的,可能明天会多一些吧,那就再来学习android吧.学了两个android的组件,这里学习下 ...

  8. 第八章:四大组件之Content Provider

    前言 Content Provider——Android四大组件之一. 本文要点 1.Content Provider简介 2.URI简介 3.如何访问Content Provider中数据 一.Co ...

  9. Android四个基本组件(2)之Service 服务与Content Provider内容提供商

    一.Service 维修: 一Service 这是一个长期的生命周期,没有真正的用户界面程序,它可以被用于开发如监视类别节目. 表中播放歌曲的媒体播放器.在一个媒体播放器的应用中.应该会有多个acti ...

随机推荐

  1. 【Matplotlib-01】Python 绘图库 Matplotlib 入门教程

    环境: Windows10 python3.6.4 numpy1.14.1 matplotlib2.1.2 工具:Cmder 目录: 1.线性图 2.散点图 3.饼状图 4.条形图 5.直方图 例1: ...

  2. Mysql之单表记录查询

    数据记录查询: 1.简单数据记录查询: select * from table_name; select allfield from table_name; select distinct(属性名) ...

  3. StandardContext

    错误信息: 2014-2-6 21:37:19 org.apache.catalina.startup.HostConfig checkResources信息: Reloading context [ ...

  4. hdu 5667 BestCoder Round #80 矩阵快速幂

    Sequence  Accepts: 59  Submissions: 650  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536 ...

  5. 【网络流问题·我就想建好模】

    ·为了有助于你读后文,在写题前先列出一些大米饼的代码习惯: 一个提醒:所有的ADD函数无特殊说明均如图:(没有w就直接跳过) 以及: go(i,a,b)=====for(int i=a;i<=b ...

  6. java实现生产者/消费者的三种方式

    package com.wenki.thread; import java.util.LinkedList; import java.util.concurrent.LinkedBlockingQue ...

  7. B/S与C/S架构

    1.CS.BS架构定义 CS(Client/Server):客户端----服务器结构.C/S结构在技术上很成熟,它的主要特点是交互性强.具有安全的存取模式.网络通信量低.响应速度快.利于处理大量数据. ...

  8. MySQl之最全且必会的sql语句

    创建一个名称为mydb1的数据库,如果有mydb1数据库则直接使用,如果无则创建mydb1数据库 create database if not exists mydb1; create databas ...

  9. sqlserver 取日期年份月份

    select convert(varchar(10),datepart(YYYY,a.fssj)) as years,--得到年份convert(varchar(10),datepart(mm,a.f ...

  10. java 获得当前时间前指定几个小时的时间?

    //@2016-12-13 获取当前时间,指定前面多少小时的时间 //返回格式YYYYMMDDHHMMSS public static String getBeforeHourTime(int iho ...