Android SDK2.0 通讯录

GoAheadY 2011-07-18

数据是应用的核心,在Android的应用程序中我们经常需要调用通信录,比如给联系人发送贺卡,发送Email。
我们已经知道可以通过ContentProvider去拿到数据,但是其uri如何得到呢。
这就需要我们去查看文档,但是Android开发者指南已经很久没有更新过了,上面的URI有很多也是错误的,我们必须通过自己查看源文件来找到uri,然后才可以拿到正确的数据。

以下代码是通过测试类来测试该方法,注释里的部分是通过ContactsContract.Contacts这个类里的常量获取uri以及id。
而方法中确实通过查看SQLite来获取字段的

package wjh.android.contact;

import java.util.ArrayList;

import android.content.ContentProviderOperation;

import android.content.ContentProviderResult;

import android.content.ContentResolver;

import android.content.ContentUris;

import android.content.ContentValues;

import android.database.Cursor;

import android.net.Uri;

import android.provider.ContactsContract;

import android.provider.ContactsContract.RawContacts;

import android.provider.ContactsContract.CommonDataKinds.Email;

import android.provider.ContactsContract.CommonDataKinds.Phone;

import android.provider.ContactsContract.CommonDataKinds.StructuredName;

import android.provider.ContactsContract.Contacts.Data;

import android.test.AndroidTestCase;

import android.util.Log;

public class ContactTest extends AndroidTestCase {

private static final String TAG = "ContactTest";

public void testGetContacts() throws Throwable {

ContentResolver contentResolver = this.getContext().getContentResolver();

String uriStr = "content://com.android.contacts/contacts";

Uri uri = Uri.parse(uriStr);

Cursor cursor = contentResolver.query(uri, null, null, null, null);

// 遍历联系人

while (cursor.moveToNext()) {

// 联系人 ID

int contactId = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID));

// 联系人显示名称

String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

// 联系人电话号码需要对另一个表进行查询,所以用到另一个 uri:content://com.android.contacts/data/phones

Cursor phones = getContext().getContentResolver().query(

                       ContactsContract.CommonDataKinds.Phone.CONTENT_URI,

                       null,

                       ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = "   // 根据上一步获取的联系人 id 来查询

                                        + contactId, null, null);  

String phone = "";

while (phones.moveToNext()) {

                                // "data1"字段,所以此处也可以直接写 "data1",但不推荐

phone = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

}

// 再查询 Email。uri 为 : content://com.android.contacts/data/emails

Cursor emails = getContext().getContentResolver().query(

                      ContactsContract.CommonDataKinds.Email.CONTENT_URI,

                      null,

                      ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = "

                                       + contactId, null, null);

while (emails.moveToNext()) {

                               

String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

Log.i("RongActivity", "emailAddress=" + emailAddress);

}

emails.close();

Log.i(TAG, "Contact [contactId= "+ contactId +"name=" + name + ", phone=" + phone + "]");

}

}

public void testInsert() {

ContentValues values = new ContentValues();

//首先向RawContacts.CONTENT_URI执行一个空值插入(raw_contacts 表), 为了获取生成的联系人 ID

Uri rawContactUri = this.getContext().getContentResolver().insert(RawContacts.CONTENT_URI, values);

long rawContactId = ContentUris.parseId(rawContactUri);


//往data表入姓名数据

values.clear();

// raw_contacts_id 字段,是 raw_contacts表id 的外键,用于说明此记录属于哪一个联系人

values.put(Data.RAW_CONTACT_ID, rawContactId);

// mimitype_id 字段,用于描述此数据的类型,电话号码?Email?....

values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);   // 注意查看第二个参数的常量值

values.put(StructuredName.GIVEN_NAME, "文白菜");   // 这个名字真好听

this.getContext().getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);

//往data表入电话数据

values.clear();

values.put(Data.RAW_CONTACT_ID, rawContactId);

values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);

values.put(Phone.NUMBER, "15101689230");

values.put(Phone.TYPE, Phone.TYPE_MOBILE);

this.getContext().getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);

//往data表入Email数据

values.clear();

values.put(Data.RAW_CONTACT_ID, rawContactId);

values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);

values.put(Email.DATA, "[email protected]");

values.put(Email.TYPE, Email.TYPE_WORK);

this.getContext().getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);

}

public void testSave() throws Throwable{

//文档位置:reference/android/provider/ContactsContract.RawContacts.html

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

int rawContactInsertIndex = ops.size();

ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)

                         .withValue(RawContacts.ACCOUNT_TYPE, null)

                         .withValue(RawContacts.ACCOUNT_NAME, null)

                         .build());

//文档位置:reference/android/provider/ContactsContract.Data.html

ops.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI)

                         .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)

                         .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)

                         .withValue(StructuredName.GIVEN_NAME, "文萝卜")

                         .build());

// 更新手机号码:Data.RAW_CONTACT_ID 获取上一条语句插入联系人时产生的 ID

ops.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI)

                         .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)

                                         .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)

                                         .withValue(Phone.NUMBER, "15101689231")  // "data1"

                                         .withValue(Phone.TYPE, Phone.TYPE_MOBILE)

                                         .withValue(Phone.LABEL, "手机号")

                                         .build());

ops.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI)

                         .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)

                                         .withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)

                                         .withValue(Email.DATA, "[email protected]")

                                         .withValue(Email.TYPE, Email.TYPE_WORK)

                                         .build());

ContentProviderResult[] results = this.getContext().getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

for(ContentProviderResult result : results){

Log.i(TAG, result.uri.toString());

}

}

}

相关推荐