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", "xxc@qq.com")
.build();
//存放內容提供者的操作的集合
ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
//將操作新增到集合裡,第一個引數是集合,第二個引數是可變引數(內容提供者的操作)
Collections.addAll(operations, operation1,operation2,operation3,operation4);
//執行集合裡的所有操作
getContext().getContentResolver().applyBatch("com.android.contacts", operations);
}
}