用ContentProvider查询通讯录和运用事务在ContentProvider操作上

import java.util.ArrayList;
import java.util.Collections;

import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.net.Uri;
import android.os.RemoteException;
import android.test.AndroidTestCase;

/**
 * raw_contacts
 * data
 * mimetype
 * 三张表关联的
 * 
 * 由于在查询和插入的时候data和mimetype是自动关联的
 * 
 * data表中的mumetype_id和mimetype表中的_id是关联的
 * 所以插入和查询的时候不用管mimetype表的设值
 * 
 * 查询data表中的mimetype_id列的时候,显示的数据就是mimetype表中对应id的mimetype属性
 * 插入data表中数据的时候,mimetupe_id列写mimetype属性就会自定将mimetype_id转换成相应的列
 */
public class TestContactRead extends AndroidTestCase {
	private Uri dataUri = Uri.parse("content://com.android.contacts/data");
	private Uri rawContactsUri = Uri.parse("content://com.android.contacts/raw_contacts");
	/**
	 * 查询通讯录的姓名,电话,邮箱 
	 */
	public void testContactRead(){
		ContentResolver resolver = getContext().getContentResolver();
		Cursor rawContactsCursor = resolver.query(dataUri, new String[]{"_id"}, null, null, null);
		while(rawContactsCursor.moveToNext()){
			String id = rawContactsCursor.getString(0);
			Cursor dataCursor = resolver.query(dataUri, new String[]{"data1","mimetype"}, "raw_contact_id=?", new String[]{id+""}, null);
			while(dataCursor.moveToNext()){
				String data1 = dataCursor.getString(dataCursor.getColumnIndex("data1"));
				String mimetype = dataCursor.getString(dataCursor.getColumnIndex("mimetype"));
				if("vnd.android.cursor.item/name".equals(mimetype)){
					System.out.println("姓名:"+data1);
				}else if("vnd.android.cursor.item/phone_v2".equals(mimetype)){
					System.out.println("电话:"+data1);
				}else if("vnd.android.cursor.item/email_v2".equals(mimetype)){
					System.out.println("邮箱:"+data1);
				}
			}
		}
	}
	/**
	 * 用内容提供者进行一个事务操作(添加通讯录记录操作)
	 */
	public void testContactWrite() throws RemoteException, OperationApplicationException{
		//内容提供者的一个添加操作
		ContentProviderOperation operation1 = ContentProviderOperation.newInsert(rawContactsUri)
				.withValue("_id", null)//设置要添加的数据(参数一:列名,参数二:值)
				.build();
		ContentProviderOperation operation2 = ContentProviderOperation.newInsert(dataUri)
				.withValueBackReference("raw_contact_id", 0)//用同组的第一个操作的返回值最为raw_contact_id的value插入
				.withValue("mimetype", "vnd.android.cursor.item/name")
				.withValue("data1", "xxc")
				.build();
		ContentProviderOperation operation3 = ContentProviderOperation.newInsert(dataUri)
				.withValueBackReference("raw_contact_id", 0)
				.withValue("mimetype", "vnd.android.cursor.item/phone_v2")
				.withValue("data1", "150987654321")
				.build();
		ContentProviderOperation operation4 = ContentProviderOperation.newInsert(dataUri)
				.withValueBackReference("raw_contact_id", 0)
				.withValue("mimetype", "vnd.android.cursor.item/email_v2")
				.withValue("data1", "[email protected]")
				.build();
		//存放内容提供者的操作的集合
		ArrayList operations = new ArrayList();
		//将操作添加到集合里,第一个参数是集合,第二个参数是可变参数(内容提供者的操作)
		Collections.addAll(operations, operation1,operation2,operation3,operation4);
		//执行集合里的所有操作
		getContext().getContentResolver().applyBatch("com.android.contacts", operations);
	}
}

你可能感兴趣的:(Android)