前阵子搞短信,发现Android 1.x至2.0 版本联系人数据库很多地方做了更改,且关于这方面的资料也比较少,所以找到一篇文章稍作翻译了下,以供大家参考,该文将分三部分发布。有翻译不对的地方还望见谅。本文多以俗语著称。最后,还是那句老话,谢绝转载!
Working With Android Contacts
Introduction To Android Contacts
Learn to work with the Android contacts database. Basic knowledge of accessing SQLite in Android along with using Cursors is expected. See the Android SQLite and Cursor Article <http://www.cnblogs.cc2/Android/Tutorials/Accessing-Data-With-Android-Cursors/> for more information. Google changed the contacts database moving from 1.x to 2.0 versions of Android. This tutorial will be broken into 3 sections. First covering accessing contacts in Android 2.0. The second page will deal with accessing the contacts in Android 1.6 and before. Third we'll glue it all together with a class that abstracts specific classes for each version and a set of classes to manage the data from the contact records.
学习使用Android联系人数据库。要求懂得基本的SQLite的知识。可以查看 Android SQLite and Cursor Article相关文章以获取更多信息。从Android 1.x 至 2.0 版本谷歌改变了Android的联系人数据库。该手册主要分为三个部分:一是介绍2.0中访问名片夹;二是介绍1.6之前的版本;三我们综合了为每个版本给出一个抽象类和累积来管理名片记录数据。
Create a new project called TestContacts in Eclipse setup for Android 2.0.
首先在Eclipse中创建一个新项目TestContacts设置为Android 2.0。
Android Contact API For 2.0
Granting Access 授予权限
Before an application can query the contact records access must be granted through the AndroidManifest.xml file stored in the root of the project. Add the following uses-permission belows the uses-sdk statement. 在AndroidManifest.xml文件中授予以下权限
<uses-permission android:name="android.permission.READ_CONTACTS" />
Querying The Android Contact Database 联系人数据库查询
Retrieving Contact Details 检索联系方式
Basic contact information stored in Contacts table with detailed information stored in individual tables for normalization. In Android 2.0 to query the base contact records the URI to query is stored in ContactsContract.Contacts.CONTENT_URI.
基本的个人信息存储在名片夹表,而详细的存储在个人表里。在Andoid2.0中查询相应联系记录的URI是ContactsContract.Contacts.CONTENT_URI。
package com.test;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
public class TestContacts extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);//查询通讯录
if(cursor.getCount()>0){
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));//联系人id
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));//联系人名称
if(cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))>0){
//Query phone here. Covered next 在该处查询电话号码
}
}
}
}
}
This application starts off as any other Android application. First create a ContentResolver isntance in cr. Then use the ContentResolver instance to query the database and return a Cursor with the contacts list. The query is perofrmed against the URI stored in ContactsContract.Contacts.CONTENT_URI. Next check if the cursor contains records and if so loop through them. The record ID field is stored in the id variable. This will be used as a where parameter later. Also the display name field is stored in the string name. For more details about working with cursors see Android Cursors Tutorial <http://www.cnblogs.cc2/Android/Tutorials/Accessing-Data-With-Android-Cursors/>.
?启动该应用程序时需关闭任何其他Android应用程序。首先,创建一个ContentResolver的实例cr。然后使用ContentResolver的实例查询数据库并返回联系人列表游标。该查询是针对ContactsContract.Contacts.CONTENT_URI 进行存储的URI。下一步检查游标是否包含记录,如果包含记录,侧记录ID字段的值存储在ID变量中。他将作为一个参数在后面的地方使用。也把名称字段的值存储在name变量中。对于游标的更多详细用法可以查看 Android的游标教程 <http://www.higherpass.com/Android/Tutorials/Accessing-Data-With-Android-Cursors/> 。
Phone Numbers 电话号码
Phone numbers are stored in their own table and need to be queried separately. To query the phone number table use the URI stored in the SDK variable ContactsContract.CommonDataKinds.Phone.CONTENT_URI. Use a WHERE conditional to get the phone numbers for the specified contact.
电话号码存储在它们自己的表中,需要单独进行查询。要查询的电话号码表使用的是SDK中的变量ContactsContract.CommonDataKinds.Phone.CONTENT_URI存储的URI。使用WHERE条件得到指定联系人的电话号码。
//根据ID查询出电话号码
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
// Do something with phones
}
pCur.close();
Perform a second query against the Android contacts SQLite database. The phone numbers are queried against the URI stored in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. The contact ID is stored in the phone table as ContactsContract.CommonDataKinds.Phone.CONTACT_ID and the WHERE clause is used to limit the data returned.
在Android联系人SQLite数据库中执行第二个查询。查询的电话号码是针对ContactsContract.CommonDataKinds.Phone.CONTENT_URI存储的URI。CONTACT_ID存储在电话表中,ContactsContract.CommonDataKinds.Phone.CONTACT_ID和where子句用于限制返回的数据。
?
Email Addresses 电子邮件地址
Querying email addresses is similar to phone numbers. A query must be performed to get email addresses from the database. Query the URI stored in ContactsContract.CommonDataKinds.Email.CONTENT_URI to query the email address table.
?查询电子邮件地址类似电话号码。必须执行一个查询从数据库中获取电子邮件地址。根据存储在ContactsContract.CommonDataKinds.Email.CONTENT_URI的URI来查询电子邮件地址表。
Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);
while (emailCur.moveToNext()) {
//如果email地址被保存在一个数组中,你将得到多个邮件地址
String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
}
emailCur.close();
As with the phone query the field names for the email table are also stored under ContactsContract.CommonDataKinds. The email query is performed on the URI in ContactsContract.CommonDataKinds.Email.CONTENT_URI and the WHERE clause has to match the ContactsContract.CommonDataKinds.Email.CONTACT_ID field. Since multiple email addresses can be stored loop through the records returned in the Cursor.
正如手机查询email表中的字段名称也存在ContactsContract.CommonDataKinds。该email执行查询的URI ContactsContract.CommonDataKinds.Email.CONTENT_URI和where子句必须符合ContactsContract.CommonDataKinds.Email.CONTACT_ID领域。多个email地址可以通过存储在游标返回的记录循环。
Notes 注释
Custom notes can be attached to each contact record. As before these are stored in a separate table and are related based on the contact ID.
可以为每个联系人记录附加自定义注释。这些注释被存储在一个单独的表中,根据相关的联系人ID查询。
String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] noteWhereParams = new String[]{id,ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null);
if (noteCur.moveToFirst()) {
String note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
}
noteCur.close();
Notes are stored in the Android Contacts generic data table. When accessing specific data the WHERE clause will need 2 conditionals. First the standard contact ID, second a MIMETYPE for the data that is being requested. The Android SDK comes with a series of auto-generated variables that take care of this. Use the ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE variable to limit the query to note records. The data table URI is stored at ContactsContract.Data.CONTENT_URI. Finally the note field name is stored in ContactsContract.CommonDataKinds.Note.NOTE.
注释存储在Android联系人通用数据表中。当访问指定数据时where子句将需要2个条件。首先是标准的联系人ID,第二个是对那些被请求数据的媒体类型。在Android SDK中有一系列自动生成的变量处理这个。使用ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE来限要查询的记录。数据表URI存放在ContactsContract.Data.CONTENT_URI。最后,注意字段名称存储在ContactsContract.CommonDataKinds.Note.NOTE。
Postal Addresses 邮政地址
Android can store multiple postal addresses per contact. Addresses are also stored in the data table like notes and queried via the URI stored in ContactsContract.Data.CONTENT_URI. Similar to the notes query a MIMETYPE must be added to the WHERE conditional. Also in Android 2.0 the Address record was split into multiple fields containing different parts of the address (PO-Box, stree, city, region, postal code). In earlier versions of the Android SDK this was a free-form string storage.
Android的每个联系人都可以多个邮政地址。地址也存储在Notes数据表中,并通过存储在ContactsContract.Data.CONTENT_URI的URI查询。类似于注释查询,媒体类型必须被添加到where条件。另外,在Android2.0中,邮政地址记录被分割成多个小地址(邮政信箱、应力[不知道什么意思]、城市、地区、邮政编码)。在早期的Android SDK版本中,该地址是由一个自由格式的字符串存储。
String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] addrWhereParams = new String[]{id,ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI,null, addrWhere, addrWhereParams, null);
while(addrCur.moveToNext()) {
String poBox = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
String street = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
String city = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
String region = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
String postalCode = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
String country = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
String type = addrCur.getString(addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
}
addrCur.close();
This code is similar to the previous example. Notice the field names for the address pieces are stored in ContactsContract.CommonDataKinds.StructuredPostal.
此代码类似于前面的示例。请注意该地址块的字段名称都存储在ContactsContract.CommonDataKinds.StructuredPostal。
Instant Messenger (IM) 即时消息
The instant messenger query performs just as the notes and address queries. Important field names for IM related data are stored in ContactsContract.CommonDataKinds.Im.
即时消息查询仅作为注释及地址查询。IM相关数据的重要字段名称存储在ContactsContract.CommonDataKinds.Im。
String imWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] imWhereParams = new String[]{id,ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE};
Cursor imCur = cr.query(ContactsContract.Data.CONTENT_URI,null, imWhere, imWhereParams, null);
if (imCur.moveToFirst()) {
String imName = imCur.getString(imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
String imType = imCur.getString(imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE));
}
imCur.close();
Organizations 组织
The last part of the contact record to be covered is the Organizations data. The Android contact record can contain information about Employment, professional, and social memberships as well as roles and titles. These records are queried from the URI stored in ContactsContract.Data.CONTENT_URI. Important field names for the organization data are stored in ContactsContract.CommonDataKinds.Organization.
联系人记录的最后一部分是组织的数据。Android的联系人记录可以包含有关就业、职业信息、社会成员以及角色和职称。这些记录从C??ontactsContract.Data.CONTENT_URI存储的URI查询。组织数据的重要字段名称存储在ContactsContract.CommonDataKinds.Organization。
String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] orgWhereParams = new String[]{id,ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI,null, orgWhere, orgWhereParams, null);
if (orgCur.moveToFirst()) {
String orgName = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
String title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
}
orgCur.close();
分享到:
相关推荐
在深入探讨《Android Contacts查询全过程》这一主题时,我们首先需理解其核心概念与操作流程。此篇文章聚焦于Android系统中联系人数据的查询机制,尤其是通过代码层面解析这一过程,为开发者提供深入理解与实践指导...
Contacts是Android系统中的核心组件之一,它负责管理和展示设备上的联系人信息。在Android 4.1.1版本中,Contacts应用程序经过优化,提供了更加高效和用户友好的体验。本文将深度剖析这一版本的Contacts代码,帮助...
在Android 4.4系统中,Contacts应用是用户管理联系人的重要组成部分。它不仅负责显示、编辑和组织联系人的信息,还与其他系统服务如Google账户同步、来电显示等功能紧密协作。这里我们将深入探讨Android 4.4 ...
android Contacts 源码eclipse编译Contacts 联系人 eclipse 源码此为android Contacts 联系人源码在eclipse编译的APK。 可以运行在 android4.4 以及一下的系统中,本人通过大量修改 直接安装运行即可。
这里我们深入探讨“android Contacts”源码,这是一份非常有价值的参考资料,对于理解Android系统中联系人管理的内部工作原理以及学习如何开发自己的联系人应用具有重要意义。 1. **数据存储结构** Android的...
Android 4.0(冰淇淋三明治)的Contacts应用是Android系统中一个至关重要的组成部分,它管理着用户的联系人信息,提供查找、编辑和同步联系人的功能。了解并研究其源码能帮助开发者深入理解Android系统的内部工作...
Android 5.0对联系人应用进行了性能优化,例如使用CursorLoader减少内存占用,利用异步任务处理大数据集,以及利用硬件加速提升UI渲染速度。 9. **隐私与安全** 联系人应用尊重用户的隐私,只有在用户明确授权的...
Android Contacts API. Quick Start Initialize Contacts Library public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); Contacts.initialize(this); } ...
"android contacts"这一主题涉及了高效地处理联系人数据的关键技术,包括高效的联系人分组读取、完整的联系人列表展示、SQLite数据库的运用以及通过ksoap2调用Web服务。接下来,我们将详细探讨这些知识点。 1. **高...
在Contacts源码中,我们可以看到如何创建和操作联系人表,以及如何使用ContentProvider来封装数据库操作,提供统一的数据访问接口。 ContentProvider是Android系统中数据共享的关键组件,它使得不同的应用能够安全...
在深入学习Contacts模块之前,我们先探讨一个与Android UI设计相关的概念:ActionBar及其Tab导航。 在Android应用开发中,ActionBar是一个重要的组件,它通常位于屏幕顶部,用于展示应用的品牌标识、提供导航和操作...
在Android系统中,联系人应用(Contacts)是用户与他人通信信息的核心接口。它整合了电话簿、电子邮件、社交媒体等多种联系方式,为用户提供了一个统一的管理界面。本篇将深入探讨Android源码,揭示Contacts应用的...
"Android Contacts之三自定义的联系人列表特效 A" 主题着重于如何利用Android SDK和相关技术来实现一种独特的联系人展示方式。我们将深入探讨如何自定义联系人列表,使其不仅功能强大,而且视觉上引人注目。 首先,...
Contacts源码中的重点在于ContentProvider,它是Android四大组件之一,负责管理并分享应用程序的数据。 ContentProvider类`com.android.providers.contacts.ContactsProvider2`是Contacts应用的核心。它实现了...
在这个项目"Android Contacts之三自定义的联系人列表特效 b"中,开发者实现了一系列增强用户体验的功能,包括滑动特效、中文索引以及关键字搜索。下面我们将详细探讨这些知识点。 1. **滑动特效**:滑动特效是提升...
联系人(com.android.contacts).bak
这个项目的名字"AndroidContacts,安卓工作室联系方式.zip"暗示了它可能是由一个专注于Android开发的工作室创建,旨在提供一种更便捷的方式来处理手机中的联系人数据。通过分析这个项目的源代码,我们可以学习到如何...
在Android 1.6及更低版本中,接触API使用的是`Contacts`和`ContactsContract`的旧版。主要区别在于URI和数据模型。例如,获取所有联系人的URI是`ContactsgetContentResolver().query(ContactsContract....
一、Android Contacts 应用架构 Android Contacts 应用由多个模块组成,包括UI层、数据访问层和同步适配器等。UI层主要负责展示联系人列表和详细信息,而数据访问层则处理数据库操作,同步适配器则用于与服务器进行...
Content Providers是Android中负责数据共享的关键组件,Contacts Provider就是其中之一,它负责管理联系人数据库,提供数据读写接口。 在源码中,我们可以看到Contacts Provider主要由两个表构成:Contacts表和...