ITeye博客

管理联系人

dai_lm · 2010-06-01 09:47 · Android · 移动开发 · Java · Google · Mobile · SQL · Android · Markdown

通过Android系统提供的接口,可以很方便的管理联系人信息。

添加

添加联系人 1.6上的代码


String peopleName = "name";
ContentValues personValues = new ContentValues();
// name
personValues.put(Contacts.People.NAME, peopleName);
/* STARRED 0 = Contacts, 1 = Favorites */
personValues.put(Contacts.People.STARRED, 0);
Uri newPersonUri = Contacts.People.createPersonInMyContactsGroup(
	getContentResolver(), personValues);

2.1时需要用下面的代码才可以添加


String peopleName = "name";
ContentValues personValues = new ContentValues();
// name
personValues.put(Contacts.People.NAME, peopleName);
newPersonUri = getContentResolver().insert(People.CONTENT_URI, personValues);

添加电话号码 根据TYPE的不同,可以添加不同类型的电话号码。 1.6上支持的TYPE有:TYPE_CUSTOM,TYPE_FAX_HOME,TYPE_FAX_WORK,TYPE_HOME,TYPE_MOBILE,TYPE_OTHER,TYPE_PAGER,TYPE_WORK。


Uri uri = null;
ContentValues values = new ContentValues();

// add phone number
String tel = "86-21-65432100";

if (!AppUtils.isEmpty(tel)) {
	values.clear();
	uri = Uri.withAppendedPath(newPersonUri, Contacts.People.Phones.CONTENT_DIRECTORY);
	values.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_HOME);
	values.put(Contacts.Phones.NUMBER, tel);
	getContentResolver().insert(uri, values);
}

添加Email地址 通过改变KIND的值,可以添加不同的联系方式。 1.6支持KIND_EMAIL,KIND_IM,KIND_ORGANIZATION,KIND_PHONE,KIND_POSTAL


// add email address
String email = "abc@com.cn";

if (!AppUtils.isEmpty(email)) {
	values.clear();
	uri = Uri.withAppendedPath(newPersonUri, Contacts.People.ContactMethods.CONTENT_DIRECTORY);
	values.put(Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL);
	values.put(Contacts.ContactMethods.DATA, email);
	values.put(Contacts.ContactMethods.TYPE, Contacts.ContactMethods.TYPE_WORK);
	getContentResolver().insert(uri, values);
}

添加公司和职务


// add company name & title
String company = "Google?";
String position= "CEO!!!";

if (!AppUtils.isEmpty(company) || !AppUtils.isEmpty(position)) {
	values.clear();
	uri = Uri.withAppendedPath(newPersonUri, Contacts.Organizations.CONTENT_DIRECTORY);

	// company name
	if (!AppUtils.isEmpty(company)) {
		values.put(Contacts.Organizations.COMPANY, companyNameText);
	}
				
	// position
	if (!AppUtils.isEmpty(position)) {
		values.put(Contacts.Organizations.TITLE, positionNameText);
	}

	values.put(Contacts.Organizations.TYPE, Contacts.Organizations.TYPE_WORK);
	getContentResolver().insert(uri, values);
}

查询

要实现查询功能,可能需要有点SQL的基础。

查询人名


// the contents want to get
String projection[] = new String[] { People._ID, People.NAME };
// the name to be found
String name = "find me";
// start search
Cursor cur = getContentResolver().query(People.CONTENT_URI, 
projection, // select sentence
People.NAME + " = ?", // where sentence
new String[] { name }, // where values
People.NAME); // order by

if (cur.moveToFirst()) {
	// get the results
	do {
		String id = cur.getString(cur.getColumnIndex(People._ID));
		String name = cur.getString(cur.getColumnIndex(People.NAME));
	} while (cur.moveToNext());
}

// close while finish
if (cur != null) {
	cur.close();
}

通过修改projection的内容,可以取得不同的内容。 如果要获得电话号码,就可以改成(id由上面的代码获得)


String phoneProjection[] = new String[] { Contacts.Phones.PERSON_ID, Contacts.Phones.NUMBER };

Cursor phoneCursor = getContentResolver().query(Contacts.Phones.CONTENT_URI, 
phoneProjection, // select
Contacts.Phones.PERSON_ID + " = " + id, // where (another style)
null, 
Contacts.Phones.DEFAULT_SORT_ORDER); // order

if (phoneCursor.moveToFirst()) {
	// get the results
	do {
		String phone = phoneCursor.getString(phoneCursor.getColumnIndex(Contacts.Phones.NUMBER));
	} while (phoneCursor.moveToNext());
}

// close while finish
if (phoneCursor != null) {
	phoneCursor.close();
}

如果要获得email地址,稍微麻烦点


String emailProjection[] = new String[] { Contacts.Phones.PERSON_ID, Contacts.ContactMethods.KIND, Contacts.ContactMethods.DATA };

Cursor emailCursor = getContentResolver().query(Contacts.ContactMethods.CONTENT_URI, 
emailProjection, // select
Contacts.ContactMethods.PERSON_ID + " = " + id, // where
null, 
Contacts.ContactMethods.DEFAULT_SORT_ORDER); // order

if (emailCursor.moveToFirst()) {
	do {
		int kind = emailCursor.getInt(emailCursor.getColumnIndex(Contacts.ContactMethods.KIND));
		if (Contacts.KIND_EMAIL == kind) {
			email = emailCursor.getString(emailCursor.getColumnIndex(Contacts.ContactMethods.DATA));
		}
	} while (emailCursor.moveToNext());
}

// close while finish
if (emailCursor != null) {
	emailCursor.close();
}

修改

删除

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。