通讯录代码

package cn.test;
import java.util.ArrayList;
import android.content.ContentProviderOperation;
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.CommonDataKinds.Email;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.CommonDataKinds.StructuredName;
import android.test.AndroidTestCase;
import android.util.Log;

public class TestContact extends AndroidTestCase {
	private static final String TAG = "TESTCONTACT";
	private ContentResolver contentResolver;
	// 访问通讯录中的所有联系人
	public void testContacts() {
		contentResolver = this.getContext().getContentResolver();
		Uri allContactsUri = Uri.parse("content://com.android.contacts/contacts");
		Cursor allContactsCursor = contentResolver.query(allContactsUri, null,null, null, null);// 从contacts表中得到所有联系人
		StringBuilder builder = new StringBuilder();
		while (allContactsCursor.moveToNext()) {
			int _id = allContactsCursor.getInt(allContactsCursor.getColumnIndex("_id"));
			builder.append("_id=" + _id).append(",");
			Uri perContactUri = Uri.parse("content://com.android.contacts/contacts/" + _id+ "/data");
			Cursor perContactsCursor = contentResolver.query(perContactUri,null, null, null, null);// 得到某个id在data表中的所有数据
			while (perContactsCursor.moveToNext()) {
				// 以下两句涉及到关联查询  data表和mimetype表进行     
				String data1 = perContactsCursor.getString(perContactsCursor.getColumnIndex("data1"));// 得到每个联系人的每个data1
				String mimetype = perContactsCursor.getString(perContactsCursor.getColumnIndex("mimetype"));//判断此data1到底是什么含义
				if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
					builder.append("phone=").append(data1).append(",");
				} else if ("vnd.android.cursor.item/email_v2".equals(mimetype)) {
					builder.append("email=").append(data1).append(",");
				} else if ("vnd.android.cursor.item/name".equals(mimetype)) {
					builder.append("name=").append(data1).append(",");
				}
			}
			perContactsCursor.close();
		}
		allContactsCursor.close();
		Log.i(TAG, builder.toString());
	}
	//根据电话号码查询出联系人的姓名
	public void getNameByTele(){
		contentResolver = this.getContext().getContentResolver();
		Uri teleUri = Uri.parse("content://com.android.contacts/data/phones/filter/12345");
		// 以下两句涉及到关联查询  data表和raw_contacts表  
		//Cursor oneContactCursor = contentResolver.query(teleUri, null,null, null, null);
		//name=oneContactCursor.getString(oneContactCursor.getColumnIndex("display_name"));
		Cursor oneContactCursor = contentResolver.query(teleUri, null,null, null, null);
		String name="";
		while(oneContactCursor.moveToNext()){
			name=oneContactCursor.getString(oneContactCursor.getColumnIndex("display_name"));
		}
		Log.i(TAG,name);
	}
	
	//向通讯录中添加联系人
	//首先是更新raw_contacts表,表中有一个_id,然后再更新data表,把详细的信息放在此表中
	//并不是先更新data表后更新raw_contacts表.因为data表中有一个外键raw_contact_id与表Raw_contacts的主键_id是一致的	
	//raw_contacts表中除了_id以外,其实都是在data表更新后才更新的.
	//第一步:我们向raw_contacts表中添加一条空的数据即可.为了得到其自动增长的主键_id
	//第二步:更新data表中的数据
	//somebody's code
	public void testAddContact1() {
		//以下为第一步
		Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
		ContentResolver resolver = this.getContext().getContentResolver();
		ContentValues values = new ContentValues();
		Uri insertUri = resolver.insert(uri, values);
		long contactid = ContentUris.parseId(insertUri);
		//以下为第二步
		values.clear();
		values.put("raw_contact_id", contactid);
		values.put(ContactsContract.Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
		values.put(StructuredName.GIVEN_NAME, "张三");
		uri = Uri.parse("content://com.android.contacts/data");
		resolver.insert(uri, values);
		
		values.clear();
		values.put("raw_contact_id", contactid);
		values.put(ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
		values.put(Phone.NUMBER, "1386595955");
		values.put(Phone.TYPE, Phone.TYPE_MOBILE);
		uri = Uri.parse("content://com.android.contacts/data");
		resolver.insert(uri, values);
		
		values.clear();
		values.put("raw_contact_id", contactid);
		values.put(ContactsContract.Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
		values.put(Email.DATA, "test@sina.com");
		values.put(Email.TYPE, Email.TYPE_HOME);
		uri = Uri.parse("content://com.android.contacts/data");
		resolver.insert(uri, values);
	}
	
	//多个操作在同一个事务中执行
	//要求对以上的几个操作用事务进行管理
	//somebody's code
	public void testAddContact2() throws Exception{
		Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
		ContentResolver resolver = this.getContext().getContentResolver();
		ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();	
		ContentProviderOperation rawcontactOperation = ContentProviderOperation.newInsert(uri)
														.withValue("account_name", null).build();
		operations.add(rawcontactOperation);
		
		uri = Uri.parse("content://com.android.contacts/data");
		ContentProviderOperation dataOperation = ContentProviderOperation.newInsert(uri)
		.withValueBackReference("raw_contact_id", 0)
		.withValue("mimetype", "vnd.android.cursor.item/name")
		.withValue("data2", "李白").build();		
		operations.add(dataOperation);
		
		uri = Uri.parse("content://com.android.contacts/data");
		dataOperation = ContentProviderOperation.newInsert(uri)
		.withValueBackReference("raw_contact_id", 0)
		.withValue("mimetype", "vnd.android.cursor.item/phone_v2")
		.withValue("data1", "1386595955")
		.withValue("data2", Phone.TYPE_MOBILE)
		.build();		
		operations.add(dataOperation);
		
		uri = Uri.parse("content://com.android.contacts/data");
		dataOperation = ContentProviderOperation.newInsert(uri)
		.withValueBackReference("raw_contact_id", 0)
		.withValue("mimetype", "vnd.android.cursor.item/email_v2")
		.withValue("data1", "test@sina.com")
		.withValue("data2", Email.TYPE_HOME)
		.build();		
		operations.add(dataOperation);
		
		resolver.applyBatch("com.android.contacts", operations);
		
	}
}

上一篇:emacs go-mode 设置tab宽度


下一篇:Centos学习笔记--linux用户管理