Android Contacts的使用(一)

前阵子搞短信,发现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 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.

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. 

 启动该应用程序时需关闭任何其他Android应用程序。首先,创建一个ContentResolver的实例cr。然后使用ContentResolver的实例查询数据库并返回联系人列表游标。该查询是针对ContactsContract.Contacts.CONTENT_URI 进行存储的URI。下一步检查游标是否包含记录,如果包含记录,侧记录ID字段的值存储在ID变量中。他将作为一个参数在后面的地方使用。也把名称字段的值存储在name变量中。对于游标的更多详细用法可以查看 Android的游标教程 

 

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();

<未完待续> 

posted @ 2011-05-03 17:37  彼岸-夕  阅读(7960)  评论(9编辑  收藏  举报