使用Contacts Contract Content Provider操作通讯录最佳实践
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操作通讯录最佳实践的更多相关文章
- 我的Android 4 学习系列之数据库和Content Provider
目录 创建数据库和使用SQLite 使用Content Provider.Cusor和Content Value来存储.共享和使用应用程序数据 使用Cursor Loader异步查询Content P ...
- Android Contacts (android通讯录读取)-content provider
Content Provider 在数据处理中,Android通常使用Content Provider的方式.Content Provider使用Uri实例作为句柄的数据封装的,很方便地访问地进行数据 ...
- Android开发-API指南-Content Provider基础
Content Provider Basics 英文原文:http://developer.android.com/guide/topics/providers/content-provider-ba ...
- Content Provider Basics ——Content Provider基础
A content provider manages access to a central repository of data. A provider is part of an Android ...
- Android Content Provider简介
Content Provider是Android的四大组件之一,与Activity和Service相同,使用之前需要注册: Android系统中存在大量的应用,当不同的应用程序之间需要共享数据时,可以 ...
- Android学习总结——Content Provider
原文地址:http://www.cnblogs.com/bravestarrhu/archive/2012/05/02/2479461.html Content Provider内容提供者 : and ...
- Android开发学习之路--Content Provider之初体验
天气说变就变,马上又变冷了,还好空气不错,阳光也不错,早起上班的车上的人也不多,公司来的同事和昨天一样一样的,可能明天会多一些吧,那就再来学习android吧.学了两个android的组件,这里学习下 ...
- 第八章:四大组件之Content Provider
前言 Content Provider——Android四大组件之一. 本文要点 1.Content Provider简介 2.URI简介 3.如何访问Content Provider中数据 一.Co ...
- Android四个基本组件(2)之Service 服务与Content Provider内容提供商
一.Service 维修: 一Service 这是一个长期的生命周期,没有真正的用户界面程序,它可以被用于开发如监视类别节目. 表中播放歌曲的媒体播放器.在一个媒体播放器的应用中.应该会有多个acti ...
随机推荐
- [LeetCode] K-diff Pairs in an Array 数组中差为K的数对
Given an array of integers and an integer k, you need to find the number of unique k-diff pairs in t ...
- Markdown 编辑器使用指南
Markdown 编辑器使用指南 1.快捷键 加粗: Ctrl/Cmd + B 标题: Ctrl/Cmd + H 插入链接: Ctrl/Cmd + K 插入代码: Ctrl/Cmd + Shift + ...
- Docker入门之--定制镜像
1. 首先定制一个Web 服务器为例 1.1 启动镜像 执行下面命令 docker run --name webserver -d -p 80:80 nginx 1.2 查看容器和镜像状态 然后执行下 ...
- [BZOJ]4805: 欧拉函数求和
解题思路类似莫比乌斯函数之和 题目大意:求[1,n]内的欧拉函数$\varphi$之和.($n<=2*10^{9}$) 思路:令$ M(n)=\sum_{i=1}^{n}\varphi (i) ...
- hdu1698 线段树区间更新
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- BZOJ4727 [POI2017]Turysta
这题太神了还是去看刺儿神题解吧. http://www.cnblogs.com/neighthorn/p/6538364.html #include <cstdio> #include & ...
- IPQ4028开启I2C功能
0 概述 IPQ4028是一款集约式4核心ARM7 SOC芯片,内嵌独立双频WiFi子系统,offload式,支持MU-MIMO,最高支持1.2Gbps.标准的官方Demo方案中,IPQ4019开启了 ...
- Java Servlet 笔记1
1. 什么是Servlet. Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序 ...
- 如何用git命令生成Patch和打Patch
在程序员的日常开发与合作过程中,对于code的生成patch和打patch(应用patch)成为经常需要做的事情.什么是patch?简单来讲,patch中存储的是你对代码的修改,生成patch就是记录 ...
- Flexible DEMO 实现手淘H5页面的终端适配
<!DOCTYPE html> <html> <head> <title>淘宝flexiblejs</title> <meta cha ...