怎么把contactscontacts文件导入安卓卓

2441人阅读
Android数据读写(6)
主要是用ContactsContract中的Data数据模型,该数据模型中存储了联系人的所有信息,包括:号码、姓名、Email、邮编、即时通讯账户、个性签名、昵称、个人网站等等,其实Data就是数据库中的一张表而已,该表中有一个比较重要的列叫做:MIMETYPE,有关MIMETYPE与Data的介绍,请看下面的链接,有3篇文章,讲得还可以下面就直接贴例子吧:
package com.con
import android.app.A
import android.content.ContentR
import android.database.C
import android.os.B
import android.provider.ContactsC
import android.provider.ContactsContract.D
import android.util.L
import android.view.V
import android.view.View.OnClickL
import android.widget.B
import android.widget.T
public class MainContentProvider extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1 = (Button) findViewById(R.id.Button01);
OnClickListener ocl = new OnClickListener() {
public void onClick(View v) {
ContentResolver contentResolver = getContentResolver();
// 获得所有的联系人
Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
// 循环遍历
if (cursor.moveToFirst()) {
int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
int displayNameColumn = cursor.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME);
// 获得联系人的ID号
String contactId = cursor.getString(idColumn);
// 获得联系人姓名
String disPlayName = cursor.getString(displayNameColumn);
Toast.makeText(MainContentProvider.this, &联系人姓名: &+disPlayName,
Toast.LENGTH_LONG).show();
Log.i(&ContentProvider&, &联系人姓名: &+disPlayName);
// 查看该联系人有多少个电话号码。如果没有这返回值为0
int phoneCount = cursor.getInt(cursor.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (phoneCount & 0) {
// 获得联系人的电话号码列表
Cursor phonesCursor = getContentResolver().query(
monDataKinds.Phone.CONTENT_URI,null,
monDataKinds.Phone.CONTACT_ID
+ & = & + contactId, null, null);
if (phonesCursor.moveToFirst()) {
// 遍历所有的电话号码
String phoneNumber = phonesCursor
.getString(phonesCursor
.monDataKinds.Phone.NUMBER));
Toast.makeText(MainContentProvider.this, &联系人电话: &+phoneNumber,
Toast.LENGTH_LONG).show();
Log.i(&ContentProvider&, &联系人电话: &+phoneNumber);
} while (phonesCursor.moveToNext());
phonesCursor.close();
//获得联系人的EMAIL
Cursor emailCursor = getContentResolver().query(
monDataKinds.Email.CONTENT_URI, null,
monDataKinds.Email.CONTACT_ID+& = &
+contactId, null, null);
if(emailCursor.moveToFirst()){
//遍历所有的email
String email = emailCursor.getString(emailCursor.getColumnIndex(
monDataKinds.Email.DATA1));
Toast.makeText(MainContentProvider.this, &Email:&+email,
Toast.LENGTH_LONG).show();
Log.i(&ContentProvider&, &Email: &+email);
} while(emailCursor.moveToNext());
emailCursor.close();
//获得邮编等信息
Cursor postalCursor = getContentResolver().query(
monDataKinds.StructuredPostal.CONTENT_URI, null,
monDataKinds.StructuredPostal.CONTACT_ID
+& = &+contactId, null, null);
if(postalCursor.moveToFirst()){
String country = postalCursor.getString(postalCursor.getColumnIndex(
monDataKinds.StructuredPostal.COUNTRY));
String city = postalCursor.getString(postalCursor.getColumnIndex(
monDataKinds.StructuredPostal.CITY));
String street = postalCursor.getString(postalCursor.getColumnIndex(
monDataKinds.StructuredPostal.STREET));
String postcode = postalCursor.getString(postalCursor.getColumnIndex(
monDataKinds.StructuredPostal.POSTCODE));
Toast.makeText(MainContentProvider.this, &国家: &+country+&/n城市: &
+city+&/n街道: &+street+&/n邮政编码: &+postcode, Toast.LENGTH_LONG).show();
Log.i(&ContentProvider&, &国家: &+country+&/n城市: &+city+&/n街道: &
+street+&/n邮政编码: &+postcode);
}while(postalCursor.moveToNext());
postalCursor.close();
Cursor imCursor = getContentResolver().query(Data.CONTENT_URI, null,
monDataKinds.Im.CONTACT_ID+&=&+contactId+& AND &+
ContactsContract.Data.MIMETYPE+&='&+
monDataKinds.Im.CONTENT_ITEM_TYPE+&'&, null, null);
if(imCursor.moveToFirst()){
String IM = imCursor.getString(imCursor.getColumnIndex(
monDataKinds.Im.DATA));
int type = imCursor.getInt(imCursor.getColumnIndex(
monDataKinds.Im.PROTOCOL));
switch(type){
case -1: Toast.makeText(MainContentProvider.this, &Custom: &+IM, Toast.LENGTH_LONG).show();
Log.i(&ContentProvider&, &Custom: &+IM);
case 0: Toast.makeText(MainContentProvider.this, &AIM: &+IM, Toast.LENGTH_LONG).show();
Log.i(&ContentProvider&, &AIM: &+IM);
case 1: Toast.makeText(MainContentProvider.this, &MSN: &+IM, Toast.LENGTH_LONG).show();
Log.i(&ContentProvider&, &MSN: &+IM);
case 2: Toast.makeText(MainContentProvider.this, &YAHOO: &+IM, Toast.LENGTH_LONG).show();
Log.i(&ContentProvider&, &YAHOO: &+IM);
case 3: Toast.makeText(MainContentProvider.this, &SKYPE: &+IM, Toast.LENGTH_LONG).show();
Log.i(&ContentProvider&, &SKYPE: &+IM);
case 4: Toast.makeText(MainContentProvider.this, &QQ: &+IM, Toast.LENGTH_LONG).show();
Log.i(&ContentProvider&, &QQ: &+IM);
case 5: Toast.makeText(MainContentProvider.this, &GoogleTalk: &+IM, Toast.LENGTH_LONG).show();
Log.i(&ContentProvider&, &GoogleTalk: &+IM);
case 6: Toast.makeText(MainContentProvider.this, &ICQ: &+IM, Toast.LENGTH_LONG).show();
Log.i(&ContentProvider&, &ICQ: &+IM);
case 7: Toast.makeText(MainContentProvider.this, &JABBER: &+IM, Toast.LENGTH_LONG).show();
Log.i(&ContentProvider&, &JABBER: &+IM);
case 8: Toast.makeText(MainContentProvider.this, &NETMEETING: &+IM, Toast.LENGTH_LONG).show();
Log.i(&ContentProvider&, &NETMEETING: &+IM);
}while(imCursor.moveToNext());
imCursor.close();
//获取website
Cursor websiteCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[]{monDataKinds.Website.URL},
monDataKinds.Website.CONTACT_ID+& = &+contactId +& AND &+
ContactsContract.Data.MIMETYPE+&='&+
monDataKinds.Website.CONTENT_ITEM_TYPE+&'&, null, null);
if(websiteCursor.moveToFirst()){
String website = websiteCursor.getString(websiteCursor.getColumnIndex(
monDataKinds.Website.URL));
Toast.makeText(MainContentProvider.this, &个人网站: &+website, Toast.LENGTH_LONG).show();
Log.i(&ContentProvider&, &个人网站: &+ website);
}while(websiteCursor.moveToNext());
websiteCursor.close();
//获得note
Cursor noteCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[]{monDataKinds.Note.NOTE},
monDataKinds.Note.CONTACT_ID+& = &+contactId+& AND &+
ContactsContract.Data.MIMETYPE+&='&
+monDataKinds.Note.CONTENT_ITEM_TYPE+&'&, null, null);
if(noteCursor.moveToFirst()){
String note = noteCursor.getString(noteCursor.getColumnIndex(
monDataKinds.Note.NOTE));
Toast.makeText(MainContentProvider.this, &个性签名: &+note, Toast.LENGTH_LONG).show();
Log.i(&ContentProvider&, &个性签名: &+note);
noteCursor.close();
//获得nickname
Cursor nicknameCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[]{monDataKinds.Nickname.NAME},
monDataKinds.Nickname.CONTACT_ID+&=&+contactId+& AND &+
ContactsContract.Data.MIMETYPE+&='&
+monDataKinds.Nickname.CONTENT_ITEM_TYPE+&'&, null, null);
if(nicknameCursor.moveToFirst()){
String nickname = nicknameCursor.getString(nicknameCursor.getColumnIndex(
monDataKinds.Nickname.NAME));
Toast.makeText(MainContentProvider.this, &昵称: &+nickname, Toast.LENGTH_LONG).show();
Log.i(&ContentProvider&, &昵称: &+nickname);
nicknameCursor.close();
//获得organization
Cursor orgCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[]{
monDataKinds.Organization.TITLE
monDataKinds.Nickname.CONTACT_ID+&=&+contactId+& AND &+
ContactsContract.Data.MIMETYPE+&='&+
monDataKinds.Organization.CONTENT_ITEM_TYPE+&'&, null, null);
if(orgCursor.moveToFirst()){
String company = orgCursor.getString(orgCursor.getColumnIndex(
String position = orgCursor.getString(orgCursor.getColumnIndex(
monDataKinds.Organization.TITLE));
Toast.makeText(MainContentProvider.this, &公司: &+company, Toast.LENGTH_LONG).show();
Log.i(&ContentProvider&, &公司: &+company+&/n职位: &+position);
}while(orgCursor.moveToNext());
orgCursor.close();
} while (cursor.moveToNext());
cursor.close();
b1.setOnClickListener(ocl);
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:640457次
积分:7625
积分:7625
排名:第1857名
原创:202篇
转载:48篇
评论:141条
(1)(1)(1)(1)(2)(2)(5)(7)(7)(7)(7)(9)(4)(1)(7)(3)(37)(8)(3)(6)(2)(2)(7)(3)(1)(9)(6)(5)(18)(1)(2)(2)(2)(5)(6)(7)(5)(26)(22)智能手机教程子分类2820人阅读
我8月份的时候接触过联系人这里,看了很多文章,把我弄蒙了,今天突然发现这篇文章,不错,如果我以后涉及到这方面的业务,会多来学习下,作者博客地址和英文原文地址都放在最下面了。
前阵子搞短信,发现Android 1.x至2.0 版本联系人数据库很多地方做了更改,且关于这方面的资料也比较少,所以找到一篇文章稍作翻译了下,以供大家参考,该文将分三部分发布。
Working With 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&&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 1.x&至 2.0 版本谷歌改变了Android的联系人数据库。该手册主要分为三个部分:一是介绍2.0中访问名片夹;二是介绍1.6之前的版本;三我们综合了为每个版本给出一个抽象类和累积来管理名片记录数据。
Create a new project called TestContacts in Eclipse setup for Android 2.0.
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& /&&
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.
import android.app.A
import android.database.C
import android.os.B
import android.provider.ContactsC
import android.provider.ContactsContract.C
public class TestContacts extends Activity {
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 在该处查询电话号码
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应用程序。首先,创建一个ContentResolver的实例cr。然后使用ContentResolver的实例查询数据库并返回联系人列表游标。该查询是针对ContactsContract.Contacts.CONTENT_URI
进行存储的URI。下一步检查游标是否包含记录,如果包含记录,侧记录ID字段的值存储在ID变量中。他将作为一个参数在后面的地方使用。也把名称字段的值存储在name变量中。对于游标的更多详细用法可以查看&&。
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 monDataKinds.Phone.CONTENT_URI. Use a WHERE conditional to get the phone numbers for
the specified contact.
电话号码存储在它们自己的表中,需要单独进行查询。要查询的电话号码表使用的是SDK中的变量monDataKinds.Phone.CONTENT_URI存储的URI。使用WHERE条件得到指定联系人的电话号码。&&&
//根据ID查询出电话号码
Cursor pCur = cr.monDataKinds.Phone.CONTENT_URI, null, monDataKinds.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 monDataKinds.Phone.CONTENT_URI. The contact ID is stored in the phone table monDataKinds.Phone.CONTACT_ID
and the WHERE clause is used to limit the data returned.
在Android联系人SQLite数据库中执行第二个查询。查询的电话号码是针对monDataKinds.Phone.CONTENT_URI存储的URI。CONTACT_ID存储在电话表中,monDataKinds.Phone.CONTACT_ID和where子句用于限制返回的数据。
Querying email addresses is similar to phone numbers. A query must be performed to get email addresses from the database. Query the URI stored monDataKinds.Email.CONTENT_URI to query the email address table.
查询电子邮件地址类似电话号码。必须执行一个查询从数据库中获取电子邮件地址。根据存储在monDataKinds.Email.CONTENT_URI的URI来查询电子邮件地址表。
Cursor emailCur = cr.monDataKinds.Email.CONTENT_URI,null,
monDataKinds.Email.CONTACT_ID + & = ?&, new String[]{id}, null);
while (emailCur.moveToNext()) {
//如果email地址被保存在一个数组中,你将得到多个邮件地址
String email = emailCur.getString(emailCur.monDataKinds.Email.DATA));
String emailType = emailCur.getString(emailCur.monDataKinds.Email.TYPE));
emailCur.close();
&As with the phone query the field names for the email table are also stored monDataKinds. The email query is performed on the URI monDataKinds.Email.CONTENT_URI and the WHERE clause has to match monDataKinds.Email.CONTACT_ID
field. Since multiple email addresses can be stored loop through the records returned in the Cursor.
正如手机查询email表中的字段名称也存在monDataKinds。该email执行查询的monDataKinds.Email.CONTENT_URI和where子句必须符合monDataKinds.Email.CONTACT_ID领域。多个email地址可以通过存储在游标返回的记录循环。
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[]{monDataKinds.Note.CONTENT_ITEM_TYPE};
Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null);
if (noteCur.moveToFirst()) {
String note = noteCur.getString(noteCur.monDataKinds.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 monDataKinds.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 monDataKinds.Note.NOTE.
注释存储在Android联系人通用数据表中。当访问指定数据时where子句将需要2个条件。首先是标准的联系人ID,第二个是对那些被请求数据的媒体类型。在Android SDK中有一系列自动生成的变量处理这个。使用monDataKinds.Note.CONTENT_ITEM_TYPE来限要查询的记录。数据表URI存放在ContactsContract.Data.CONTENT_URI。最后,注意字段名称存储在monDataKinds.Note.NOTE。
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[]{monDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI,null, addrWhere, addrWhereParams, null);
while(addrCur.moveToNext()) {
String poBox = addrCur.getString(addrCur.monDataKinds.StructuredPostal.POBOX));
String street = addrCur.getString(addrCur.monDataKinds.StructuredPostal.STREET));
String city = addrCur.getString(addrCur.monDataKinds.StructuredPostal.CITY));
String region = addrCur.getString(addrCur.monDataKinds.StructuredPostal.REGION));
String postalCode = addrCur.getString(addrCur.monDataKinds.StructuredPostal.POSTCODE));
String country = addrCur.getString(addrCur.monDataKinds.StructuredPostal.COUNTRY));
String type = addrCur.getString(addrCur.monDataKinds.StructuredPostal.TYPE));
addrCur.close();
This code is similar to the previous example. Notice the field names for the address pieces are stored monDataKinds.StructuredPostal.
此代码类似于前面的示例。请注意该地址块的字段名称都存储在monDataKinds.StructuredPostal。
The instant messenger query performs just as the notes and address queries. Important field names for IM related data are stored monDataKinds.Im.
即时消息查询仅作为注释及地址查询。IM相关数据的重要字段名称存储在monDataKinds.Im。
String imWhere = ContactsContract.Data.CONTACT_ID + & = ? AND & + ContactsContract.Data.MIMETYPE + & = ?&;
String[] imWhereParams = new String[]{monDataKinds.Im.CONTENT_ITEM_TYPE};
Cursor imCur = cr.query(ContactsContract.Data.CONTENT_URI,null, imWhere, imWhereParams, null);
if (imCur.moveToFirst()) {
String imName = imCur.getString(imCur.monDataKinds.Im.DATA));
String imType = imCur.getString(imCur.monDataKinds.Im.TYPE));
imCur.close();
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 monDataKinds.Organization.
联系人记录的最后一部分是组织的数据。Android的联系人记录可以包含有关就业、职业信息、社会成员以及角色和职称。这些记录从ContactsContract.Data.CONTENT_URI存储的URI查询。组织数据的重要字段名称存储在monDataKinds.Organization。
String orgWhere = ContactsContract.Data.CONTACT_ID + & = ? AND & + ContactsContract.Data.MIMETYPE + & = ?&;
String[] orgWhereParams = new String[]{monDataKinds.Organization.CONTENT_ITEM_TYPE};
Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI,null, orgWhere, orgWhereParams, null);
if (orgCur.moveToFirst()) {
String orgName = orgCur.getString(orgCur.monDataKinds.Organization.DATA));
String title = orgCur.getString(orgCur.monDataKinds.Organization.TITLE));
orgCur.close();
If you just read the begining of this article the first bit of this page is going to look familiar. This page is designed to act as a standalone guide to working with the contacts API in Android 1.6 and before. 假如你刚刚阅读了该文的开头是否有种时曾相识的感觉。该文主要是用来介绍1.6以下版本联系人的API。
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. 同Android Contacts的使用(一)
&uses-permission android:name=&android.permission.READ_CONTACTS& /&
Basic contact information stored in Contacts table with detailed information stored in individual tables for normalization. In Android 1.x to query the base contact records the URI to query is stored in People.CONTENT_URI. 基本的联系人信息存储在联系人表中,而详细信息存储在个人表中。在
Android1.x 中查询的联系人记录数据库的URI是People.CONTENT_URI。
package com.
import android.app.A
import android.content.ContentR
import android.database.C
import android.os.B
import android.provider.Contacts.P
public class TestContacts extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(People.CONTENT_URI, null, null, null, null);
if(cur.getCount()&0){
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(People._ID));
String name = cur.getString(cur.getColumnIndex(People.DISPLAY_NAME));
&Start off with the standard view loading. Then we create a ContentResolver instance that will be used to query the SQLite database that stores the contacts. The ContentResolver query returns a Cursor instance that holds the contact records queried from
the database. Then take the ID field from the contact record and store it in the string id and take the DISPLAY_NAME field and place it in the string name. For more information about cursors see the&.&&参考Android Contacts的使用(一)
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 Contacts.Phones.CONTENT_URI. Use a WHERE conditional to get the phone numbers for the specified contact.
同Android Contact的使用(一),查询的URI换成Contacts.Phones.CONTENT_URI。
if (cur.getInt(cur.getColumnIndex(People.PRIMARY_PHONE_ID)) & 0) {
Cursor pCur = cr.query(Contacts.Phones.CONTENT_URI,null,
Contacts.Phones.PERSON_ID +& = ?&,new String[]{id}, null);
int pCount = pCur.getCount();
String[] phoneNum = new String[pCount];
String[] phoneType = new String[pCount];
while (pCur.moveToNext()) {
phoneNum[i] = pCur.getString(pCur.getColumnIndex(Contacts.Phones.NUMBER));
phoneType[i] = pCur.getString(pCur.getColumnIndex(Contacts.Phones.TYPE));
i++;
Query the phones table and get a Cursor stored in pCur. Since the Android contacts database can store multiple phone numbers per contact we need to loop through the returned results. In addition to returning the phone number the query also returned the type
of number (home, work, mobile, etc). 查询电话表,返回一个游标pCur。由于Android中每个联系人可以存储多个电话号码,我们需要遍历返回的结果。查询除了返回电话号码外还返回了其他(家庭,工作,手机等)类型。
Querying email addresses is similar to phone numbers. A special query must be performed to get email addresses from the database. Query the URI stored in Contacts.ContactMethods.CONTENT_EMAIL_URI to query the email addresses.&&同Android Contact的使用(一),查询的URI换成Contacts.ContactMethods.CONTENT_EMAIL_URI。
Cursor emailCur = cr.query(Contacts.ContactMethods.CONTENT_EMAIL_URI,null,
Contacts.ContactMethods.PERSON_ID + & = ?&,new String[]{id}, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
emailCur.close();
Simple query Contacts.ContactMethods.CONTENT_EMAIL_URI with a conditional limiting the results to numbers that match the ID of the contact record matches the value in the field Contacts.ContactMethods.PERSON_ID. As with phone numbers each contact can contain
multiple email addresses so we need to loop through the Cursor records. 查询Contacts.ContactMethods.CONTENT_EMAIL_URI,以联系人记录的ID为条件匹配Contacts.ContactMethods.PERSON_ID的值。正如电话号码每个联系人也可以有多个email地址,同样的我们也需要遍历返回的结果。
Custom notes can be attached to each contact record. Notes though are stored in the main contact record and are simply accessed through the data stored in People.NOTES. 可以为每个联系人附加自定义注释。注释存储在联系人记录中,并通过People.NOTES存储的数据进行访问。
String notes=cur.getString(cur.getColumnIndex(People.NOTES));
Android can store multiple postal addresses per contact. Addresses are stored in the contact methods table and need to have a second conditional added to retrieve the data. Add a conditional Contacts.ContactMethods.KIND that matches Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE
to only query postal addresses from Contacts.ContactMethods.CONTENT_URI.&每个联系人都可以存储多个邮政地址。地址存储在联系方式表中,要获取数据,需要有次要条件。添加适配Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE的条件Contacts.ContactMethods.KIND来访问Contacts.ContactMethods.CONTENT_URI传递过来的地址。
String addrWhere = Contacts.ContactMethods.PERSON_ID
+ & = ? AND & + Contacts.ContactMethods.KIND + & = ?&;
String[] addrWhereParams = new String[]{id,
Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE};
Cursor addrCur = cr.query(Contacts.ContactMethods.CONTENT_URI,
null, addrWhere, addrWhereParams, null);
while(addrCur.moveToNext()) {
String addr = addrCur.getString(
addrCur.getColumnIndex(Contacts.ContactMethodsColumns.DATA));
String type = addrCur.getString(
addrCur.getColumnIndex(Contacts.ContactMethodsColumns.TYPE));
addrCur.close();
Query Contacts.ContactMethods.CONTENT_URI with 2 conditionals, one limiting the contact ID and the second Contacts.ContactMethods.KIND matching Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE to only query postall addresses. Android can store multiple postal
addresses so loop through the list of returned results. Android also stores a type record for the address. In Android 1.6 and before the address is stored as a free-form string containing the data. In Android 2.0 and later this has changed to being a series
of fields containing parts of the address. 查询Contacts.ContactMethods.CONTENT_URI需要2个条件,一个是联系人的ID另一个是通过Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE来匹配Contacts.ContactMethods.KIND查询postal地址。Android可以通过循环来遍历返回的结果列表中的多个postal地址,他也存储了地址类型的记录。在Android1.6或更早的版本中,该地址是由一个自由格式的字符串存储,在2.0或更高的版本中,已经更改为多个小地址存储。
The instant messenger query works just like the previous 2. The data is queried from Contacts.ContactMethods.CONTENT_URI and needs conditionals for the contact ID and Contacts.ContactMethods.KIND matching Contacts.ContactMethods.CONTENT_IM_ITEM_TYPE. 即时消息查询类似于postal地址查询,查询这些数据也需要联系人的Id和Contacts.ContactMethods.CONTENT_IM_ITEM_TYPE来匹配Contacts.ContactMethods.KIND为条件。
String imWhere = Contacts.ContactMethods.PERSON_ID
+ & = ? AND & + Contacts.ContactMethods.KIND + & = ?&;
String[] imWhereParams = new String[]{id,
Contacts.ContactMethods.CONTENT_IM_ITEM_TYPE};
Cursor imCur = cr.query(Contacts.ContactMethods.CONTENT_URI,
null, imWhere, imWhereParams, null);
if (imCur.moveToFirst()) {
String imName = imCur.getString(
imCur.getColumnIndex(Contacts.ContactMethodsColumns.DATA));
String imType = imCur.getString(
imCur.getColumnIndex(Contacts.ContactMethodsColumns.TYPE));
imCur.close();
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 Contacts.Organizations.CONTENT_URI. 联系人记录的最后一部分数据组织数据。在Android中联系记录可以包含有关就业、职业信息、社会成员以及角色和职称等信息。这些记录需从Contacts.Organizations.CONTENT_URI存储的URI查询。
String orgWhere = Contacts.ContactMethods.PERSON_ID + & = ?&;
String[] orgWhereParams = new String[]{id};
Cursor orgCur = cr.query(Contacts.Organizations.CONTENT_URI,
null, orgWhere, orgWhereParams, null);
if (orgCur.moveToFirst()) {
String orgName = orgCur.getString(
orgCur.getColumnIndex(PANY));
String title = orgCur.getString(
orgCur.getColumnIndex(Contacts.Organizations.TITLE));
orgCur.close();
Working With Android Contacts
To put this together into an application there are a few glue pieces that need to be setup along with creating classes to manage accessing the data. First we need to create a set of classes to hold the data. Also we'll create a class to handle 2.0 API calls
and a class to handle 1.6 and earlier API calls. There's also a wrapper class that determines and loads the proper class.
需把整合类一起放在程序里,同时创建类来管理访问这些数据。(该处实在是翻译不通)。首先,我们需要创建一个类用来保存数据。此外,我们将创建一个类用来处理2.0API 调用和一个类用来处理1.6 或更早的 API 调用。还有一个包装类,用来确定并加载适当的类。&
The contact classes are a series of classes to hold a list of contacts. The list is stored in the class ContactList that maintains an ArrayList of Contacts. The Contact objects are represented in the Contact class. The contact class stores all the data from
the Android contact record. In addition to the ContactList and Contact classes there are specialized classes to represent some of the record data.
Contact类是一系列保持名片夹列表的类。该类存储在ContactList类中,此类维护着联系人列表。Contact类表示联系人对象,该类保存着所有从Android查询出的联系人数据。另外,在ContactList和Contact类中,有特定的类来表示某些记录的信息。
We will create classes to represent the address, email, instant messenger, phone number, and organization(s). Most of these classes are mere data storage classes with variables and getter/setters.
我们将创建一个实体类来表示地址、email、即时通讯、电话号码、组织等信息。该类大部分数据是单纯的变量,和一些getter/setters方法。
The ContactList class is a very basic class designed to hold an ArrayList of instances of the Contact class below. We've left this class very plain and ready to be expanded to suit your needs.
ContactList是一个基类,设计一个ArrayList数组用来存储Contact实例。这样降低了耦合度,可以随时扩大以满足需求。
package com.
import java.util.ArrayL
public class ContactList {
private ArrayList&Contact& contacts = new ArrayList&Contact&();
public ArrayList&Contact& getContacts() {
public void setContacts(ArrayList&Contact& contacts) {
this.contacts =
public void addContact(Contact contact) {
this.contacts.add(contact);
public ContactList() {
The Contact class is used to store the details about each contact. There are a series of private class variables to hold this data. Singular data such as name and database ID are stored as strings. Complex data is stored either as an instance or ArrayList
of data specific classes. This class is mainly getters and setters with a few methods to add to the internal ArrayLists.&
Contact类用来存储每个联系人的详细信息。该类定义了一些私有变量来保存这些数据。如名称和id定义成String型,而复杂的数据侧定义成数组或实例。这个类主要是生成一个getters和setters方法以便添加到数组中。
package com.
import java.util.ArrayL
public class Contact {
private String displayN
private ArrayList&Phone&
private ArrayList&Email&
private ArrayList&String&
private ArrayList&Address& addresses = new ArrayList&Address&();
private ArrayList&IM& imA
private Organ
public String getId() {
public void setId(String id) {
public String getDisplayName() {
return displayN
public void setDisplayName(String displayName) {
this.displayName = displayN
public ArrayList&Phone& getPhone() {
public void setPhone(ArrayList&Phone& phone) {
this.phone =
public void addPhone(Phone phone) {
this.phone.add(phone);
public ArrayList&Email& getEmail() {
public void setEmail(ArrayList&Email& email) {
this.email =
public void addEmail(Email email) {
this.email.add(email);
public ArrayList&String& getNotes() {
public void setNotes(ArrayList&String& notes) {
this.notes =
public void AddNotes(String notes){
this.notes.add(notes);
public ArrayList&Address& getAddresses() {
public void setAddresses(ArrayList&Address& addresses) {
this.addresses =
public void addAddress(Address address) {
this.addresses.add(address);
public ArrayList&IM& getImAddresses() {
return imA
public void setImAddresses(ArrayList&IM& imAddresses) {
this.imAddresses = imA
public void addImAddresses(IM imAddr) {
this.imAddresses.add(imAddr);
public Organization getOrganization() {
public void setOrganization(Organization organization) {
this.organization =
The Address class is the only class in the ContactList framework that actually does any work. Due to differences in data storage between 1.x and 2.0 versions of Android the Address class has to determine if the input address is free-form from 1.x or was
input from the formatted data structure from 2.0. Android 2.0 has individual data columns for PO-Box, street, city, region, postal code, country while 1.x was just a text field with the entire address. This is handled in the toString() method that returns
a free-form address from either input type. Unfortunately when using Android 1.x all the individual address getters will return null.
地址类是联系人列表框架中负责所有操作的唯一类。鉴于1.x&和2.0 版本中数据存储不同,Android类必须确认输入的地址是1.x 以上的自由格式还是2.0的格式化数据结构。Android 2.0 有单独的数据列给PO-Box,street,city,region,postal code,country 而1.x整个地址中只是一个文本域,并在toString()方法中处理,该方法返回一个没有格式的地址或者输入类型。不幸的是使用Android1.x 所有的单独地址获取都将返回null 。
package com.
public class Address {
private String poB
private String postalC
private String asString = &&;
public String getType() {
public void setType(String type) {
this.type =
public String getPoBox() {
return poB
public void setPoBox(String poBox) {
this.poBox = poB
public String getStreet() {
public void setStreet(String street) {
this.street =
public String getCity() {
public void setCity(String city) {
this.city =
public String getState() {
public void setState(String state) {
this.state =
public String getPostalCode() {
return postalC
public void setPostalCode(String postalCode) {
this.postalCode = postalC
public String getCountry() {
public void setCountry(String country) {
this.country =
public String toString() {
if (this.asString.length() & 0) {
return(this.asString);
String addr = &&;
if (this.getPoBox() != null) {
addr = addr + this.getPoBox() + &n&;
if (this.getStreet() != null) {
addr = addr + this.getStreet() + &n&;
if (this.getCity() != null) {
addr = addr + this.getCity() + &, &;
if (this.getState() != null) {
addr = addr + this.getState() + & &;
if (this.getPostalCode() != null) {
addr = addr + this.getPostalCode() + & &;
if (this.getCountry() != null) {
addr = addr + this.getCountry();
return(addr);
public Address(String asString, String type) {
this.asString = asS
this.type =
public Address(String poBox, String street, String city, String state,
String postal, String country, String type) {
this.setPoBox(poBox);
this.setStreet(street);
this.setCity(city);
this.setState(state);
this.setPostalCode(postal);
this.setCountry(country);
this.setType(type);
Another getter/setter and data storage class. The email class stores the email address and address type (work, home, etc).
另一个生成getter/setter的数据存储类。该类存储的是email的邮件地址和地址类型(工作、家庭等)。
package com.
public class Email {
public String getAddress() {
public void setAddress(String address) {
this.address =
public String getType() {
public void setType(String t) {
this.type =
public Email(String a, String t) {
this.address =
this.type =
Class to hold instant messenger data. 该类用来保存即时消息数据。
package com.
public class IM {
public String getName() {
public void setName(String name) {
this.name =
public String getType() {
public void setType(String type) {
this.type =
public IM(String name, String type) {
this.name =
this.type =
Class to hold the contacts organizational data. 该类用来保存联系人组织数据。
package com.
public class Organization {
private String organization = &&;
private String title = &&;
public String getOrganization() {
public void setOrganization(String organization) {
this.organization =
public String getTitle() {
public void setTitle(String title) {
this.title =
public Organization() {
public Organization(String org, String title) {
this.organization =
this.title =
Class to hold the phone records.&&该类用来保存电话记录。
package com.
public class Phone {
public String getNumber() {
public void setNumber(String number) {
this.number =
public String getType() {
public void setType(String type) {
this.type =
public Phone(String n, String t) {
this.number =
this.type =
The wrapper class below is what will be invoked by applications. This class will determine the API level running on the device/emulator and load the correct class created on the next pages. To determine the correct Android API running the Build.VERSION.SDK
variable is queried. This version code is then compared against the Eclair (2.0) version code stored in Build.VERSION_CODES.ECLAIR. Finally the proper API class is loaded.
下面的这个封装类是由应用程序所调用的。该类确认运行在设备/模拟器上API的版本。要确认正确的运行着的Android API需要访问VERSION.SDK变量。取得版本号然后跟存于Build.VERSION_CODES.ECLAIR的Eclair(2.0)版本号相比较。最好加载相应的API。
package com.
import android.content.ContentR
import android.content.I
import android.database.C
import android.os.B
public abstract class ContactAPI {
private static ContactAPI
public static ContactAPI getAPI() {
if (api == null) {
String apiC
if (Integer.parseInt(Build.VERSION.SDK) &= Build.VERSION_CODES.ECLAIR) {
apiClass = &com.highercollaboration.android.ContactAPI.ContactAPISdk5&;
apiClass = &com.highercollaboration.android.ContactAPI.ContactAPISdk3&;
Class&? extends ContactAPI& realClass = Class.forName(apiClass).
asSubclass(ContactAPI.class);
api = realClass.newInstance();
} catch (Exception e) {
throw new IllegalStateException(e);
public abstract Intent getContactIntent();
public abstract ContactList newContactList();
public abstract Cursor getCur();
public abstract void setCur(Cursor cur);
public abstract ContentResolver getCr();
public abstract void setCr(ContentResolver cr);
This class takes what was covered on page 1 of the tutorial about the Android 2.0 Contact API and turns it into a class. This class extends and will be invoked by the wrapper class created previously.
这个类在前面的Android Contacts的使用(一)中介绍了关于Android 2.0 联系人API 的教程并把他封装成一个类。这个类扩展并由先前建立的封装类调用。
package com.
import java.util.ArrayL
import android.content.ContentR
import android.content.I
import android.database.C
import android.provider.ContactsC
public class ContactAPISdk5 extends ContactAPI {
private ContentR
public Cursor getCur() {
public void setCur(Cursor cur) {
this.cur =
public ContentResolver getCr() {
public void setCr(ContentResolver cr) {
public Intent getContactIntent() {
return(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI));
public ContactList newContactList() {
ContactList contacts = new ContactList();
this.cur = this.cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (this.cur.getCount() & 0) {
while (cur.moveToNext()) {
Contact c = new Contact();
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
c.setId(id);
c.setDisplayName(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) & 0) {
c.setPhone(this.getPhoneNumbers(id));
c.setEmail(this.getEmailAddresses(id));
c.setNotes(this.getContactNotes(id));
c.setAddresses(this.getContactAddresses(id));
c.setImAddresses(this.getIM(id));
c.setOrganization(this.getContactOrg(id));
contacts.addContact(c);
return(contacts);
public ArrayList&Phone& getPhoneNumbers(String id) {
ArrayList&Phone& phones = new ArrayList&Phone&();
Cursor pCur = this.cr.query(
monDataKinds.Phone.CONTENT_URI,
monDataKinds.Phone.CONTACT_ID +& = ?&,
new String[]{id}, null);
while (pCur.moveToNext()) {
phones.add(new Phone(
pCur.getString(pCur.monDataKinds.Phone.NUMBER))
, pCur.getString(pCur.monDataKinds.Phone.TYPE))
pCur.close();
return(phones);
public ArrayList&Email& getEmailAddresses(String id) {
ArrayList&Email& emails = new ArrayList&Email&();
Cursor emailCur = this.cr.query(
monDataKinds.Email.CONTENT_URI,
monDataKinds.Email.CONTACT_ID + & = ?&,
new String[]{id}, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
Email e = new Email(emailCur.getString(emailCur.monDataKinds.Email.DATA))
,emailCur.getString(emailCur.monDataKinds.Email.TYPE))
emails.add(e);
emailCur.close();
return(emails);
public ArrayList&String& getContactNotes(String id) {
ArrayList&String& notes = new ArrayList&String&();
String where = ContactsContract.Data.CONTACT_ID + & = ? AND & + ContactsContract.Data.MIMETYPE + & = ?&;
String[] whereParameters = new String[]{id,
monDataKinds.Note.CONTENT_ITEM_TYPE};
Cursor noteCur = this.cr.query(ContactsContract.Data.CONTENT_URI, null, where, whereParameters, null);
if (noteCur.moveToFirst()) {
String note = noteCur.getString(noteCur.monDataKinds.Note.NOTE));
if (note.length() & 0) {
notes.add(note);
noteCur.close();
return(notes);
public ArrayList&Address& getContactAddresses(String id) {
ArrayList&Address& addrList = new ArrayList&Address&();
String where = ContactsContract.Data.CONTACT_ID + & = ? AND & + ContactsContract.Data.MIMETYPE + & = ?&;
String[] whereParameters = new String[]{id,
monDataKinds.StructuredPostal.CONTENT_ITEM_TYPE};
Cursor addrCur = this.cr.query(ContactsContract.Data.CONTENT_URI, null, where, whereParameters, null);
while(addrCur.moveToNext()) {
String poBox = addrCur.getString(addrCur.monDataKinds.StructuredPostal.POBOX));
String street = addrCur.getString(addrCur.monDataKinds.StructuredPostal.STREET));
String city = addrCur.getString(addrCur.monDataKinds.StructuredPostal.CITY));
String state = addrCur.getString(addrCur.monDataKinds.StructuredPostal.REGION));
String postalCode = addrCur.getString(addrCur.monDataKinds.StructuredPostal.POSTCODE));
String country = addrCur.getString(addrCur.monDataKinds.StructuredPostal.COUNTRY));
String type = addrCur.getString(addrCur.monDataKinds.StructuredPostal.TYPE));
Address a = new Address(poBox, street, city, state, postalCode, country, type);
addrList.add(a);
addrCur.close();
return(addrList);
public ArrayList&IM& getIM(String id) {
ArrayList&IM& imList = new ArrayList&IM&();
String where = ContactsContract.Data.CONTACT_ID + & = ? AND & + ContactsContract.Data.MIMETYPE + & = ?&;
String[] whereParameters = new String[]{id,
monDataKinds.Im.CONTENT_ITEM_TYPE};
Cursor imCur = this.cr.query(ContactsContract.Data.CONTENT_URI, null, where, whereParameters, null);
if (imCur.moveToFirst()) {
String imName = imCur.getString(imCur.monDataKinds.Im.DATA));
String imT
imType = imCur.getString(imCur.monDataKinds.Im.TYPE));
if (imName.length() & 0) {
IM im = new IM(imName, imType);
imList.add(im);
imCur.close();
return(imList);
public Organization getContactOrg(String id) {
Organization org = new Organization();
String where = ContactsContract.Data.CONTACT_ID + & = ? AND & + ContactsContract.Data.MIMETYPE + & = ?&;
String[] whereParameters = new String[]{id,
monDataKinds.Organization.CONTENT_ITEM_TYPE};
Cursor orgCur = this.cr.query(ContactsContract.Data.CONTENT_URI, null, where, whereParameters, null);
if (orgCur.moveToFirst()) {
String orgName = orgCur.getString(orgCur.monDataKinds.Organization.DATA));
String title = orgCur.getString(orgCur.monDataKinds.Organization.TITLE));
if (orgName.length() & 0) {
org.setOrganization(orgName);
org.setTitle(title);
orgCur.close();
return(org);
This class is made up of the code explained covering version 1.x of the Android contact API. As with the 2.0 class this class extends the ContactAPI wrapper class and will be invoked by it if the 1.x version of Android is in use.
这个类由Android 1.0 联系人版本介绍的代码组成。正如2.0中,这个类扩展了ContactAPI封装类,并且封装类调用这个类,当然前提是使用1.x 版本。
package com.
import java.util.ArrayL
import android.content.ContentR
import android.content.I
import android.database.C
import android.provider.C
import android.provider.Contacts.P
public class ContactAPISdk3 extends ContactAPI {
private ContentR
public Cursor getCur() {
public void setCur(Cursor cur) {
this.cur =
public ContentResolver getCr() {
public void setCr(ContentResolver cr) {
public Intent getContactIntent() {
return(new Intent(Intent.ACTION_PICK, People.CONTENT_URI));
public ContactList newContactList() {
ContactList contacts = new ContactList();
String id=&&;
this.cur = this.cr.query(People.CONTENT_URI,
null, null, null, null);
if (this.cur.getCount() & 0) {
while (cur.moveToNext()) {
Contact c = new Contact();
id = cur.getString(cur.getColumnIndex(People._ID));
c.setId(id);
c.setDisplayName(cur.getString(cur.getColumnIndex(People.DISPLAY_NAME)));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(People.PRIMARY_PHONE_ID))) & 0) {
c.setPhone(this.getPhoneNumbers(id));
c.setEmail(this.getEmailAddresses(id));
ArrayList&String& notes = new ArrayList&String&();
notes.add(cur.getString(cur.getColumnIndex(People.NOTES)));
c.setNotes(notes);
c.setAddresses(this.getContactAddresses(id));
c.setImAddresses(this.getIM(id));
c.setOrganization(this.getContactOrg(id));
contacts.addContact(c);
return(contacts);
public ArrayList&Phone& getPhoneNumbers(String id) {
ArrayList&Phone& phones = new ArrayList&Phone&();
Cursor pCur = this.cr.query(
Contacts.Phones.CONTENT_URI,
Contacts.Phones.PERSON_ID +& = ?&,
new String[]{id}, null);
while (pCur.moveToNext()) {
phones.add(new Phone(
pCur.getString(pCur.getColumnIndex(Contacts.Phones.NUMBER))
, pCur.getString(pCur.getColumnIndex(Contacts.Phones.TYPE))
pCur.close();
return(phones);
public ArrayList&Email& getEmailAddresses(String id) {
ArrayList&Email& emails = new ArrayList&Email&();
Cursor emailCur = this.cr.query(
Contacts.ContactMethods.CONTENT_EMAIL_URI,
Contacts.ContactMethods.PERSON_ID + & = ?&,
new String[]{id}, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
Email e = new Email(emailCur.getString(emailCur.getColumnIndex(Contacts.ContactMethods.DATA))
,emailCur.getString(emailCur.getColumnIndex(Contacts.ContactMethods.CONTENT_EMAIL_TYPE))
emails.add(e);
emailCur.close();
return(emails);
public ArrayList&Address& getContactAddresses(String id) {
ArrayList&Address& addrList = new ArrayList&Address&();
String where = Contacts.ContactMethods.PERSON_ID + & = ? AND & + Contacts.ContactMethods.KIND + & = ?&;
String[] whereParameters = new String[]{id,
Contacts.ContactMethods.CONTENT_POSTAL_ITEM_TYPE};
Cursor addrCur = this.cr.query(Contacts.ContactMethods.CONTENT_URI, null, where, whereParameters, null);
while(addrCur.moveToNext()) {
String addr = addrCur.getString(addrCur.getColumnIndex(Contacts.ContactMethodsColumns.DATA));
String type = addrCur.getString(addrCur.getColumnIndex(Contacts.ContactMethodsColumns.TYPE));
Address a = new Address(addr, type);
addrList.add(a);
addrCur.close();
return(addrList);
public ArrayList&IM& getIM(String id) {
ArrayList&IM& imList = new ArrayList&IM&();
String where = Contacts.ContactMethods.PERSON_ID + & = ? AND & + Contacts.ContactMethods.KIND + & = ?&;
String[] whereParameters = new String[]{id,
Contacts.ContactMethods.CONTENT_IM_ITEM_TYPE};
Cursor imCur = this.cr.query(Contacts.ContactMethods.CONTENT_URI, null, where, whereParameters, null);
if (imCur.moveToFirst()) {
String imName = imCur.getString(imCur.getColumnIndex(Contacts.ContactMethodsColumns.DATA));
String imType = imCur.getString(imCur.getColumnIndex(Contacts.ContactMethodsColumns.TYPE));
if (imName.length() & 0) {
IM im = new IM(imName, imType);
imList.add(im);
imCur.close();
return(imList);
public Organization getContactOrg(String id) {
Organization org = new Organization();
String where = Contacts.ContactMethods.PERSON_ID + & = ?&;
String[] whereParameters = new String[]{id};
Cursor orgCur = this.cr.query(Contacts.Organizations.CONTENT_URI, null, where, whereParameters, null);
if (orgCur.moveToFirst()) {
String orgName = orgCur.getString(orgCur.getColumnIndex(PANY));
String title = orgCur.getString(orgCur.getColumnIndex(Contacts.Organizations.TITLE));
if (orgName.length() & 0) {
org.setOrganization(orgName);
org.setTitle(title);
orgCur.close();
return(org);
至此,Android Contacts的使用三大部分已经介绍完成,不得不承认的是有很多地方翻译的很烂很勉强,自己看着都有点头晕。嘿嘿,建议大家多看代码。
作者博客:/lycoris/archive//2037716.html
英文原文地址:/Android/Tutorials/Working-With-Android-Contacts/
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1042750次
积分:17380
积分:17380
排名:第351名
原创:700篇
转载:115篇
译文:12篇
评论:105条
(2)(10)(12)(22)(23)(9)(6)(4)(11)(11)(11)(11)(31)(49)(31)(91)(73)(30)(24)(46)(38)(47)(35)(25)(29)(6)(10)(18)(17)(15)(77)(2)(1)}

我要回帖

更多关于 逍遥安卓怎么导入图片 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信