package com.example.mycontact;
import java.util.ArrayList;
import android.content.ContentProvider;
import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.net.Uri;
import android.os.RemoteException;
import android.test.AndroidTestCase;
public class ContactTest extends AndroidTestCase {
public Uri raw_contactsUri = Uri.parse("content://com.android.contacts/raw_contacts");
private Uri dataUri = Uri.parse("content://com.android.contacts/data");
//读通信录里面的信息
public void testContactRead(){
ContentResolver resolver = getContext().getContentResolver();
//在raw_contacts表中得到ID哪一列,用来查询
Cursor rawContactsCursor = resolver.query(raw_contactsUri, new String[]{ "_id" }, null, null, null);
while(rawContactsCursor.moveToNext()){
//得到ID
long id = rawContactsCursor.getLong(0);
//在data表用ID来查mimetype和data1
Cursor dataCursor = resolver.query(dataUri, new String[]{ "mimetype","data1" }, "raw_contact_id=?", new String[]{ id+"" }, null);
while(dataCursor.moveToNext()){
String mimetype = dataCursor.getString(0);//得到类形
String date1 = dataCursor.getString(1);//得到信息
System.out.println("mimetype 类形:"+mimetype+",date1 信息:"+date1);//打印
}
}
}
public void testContactWrite(){
ContentResolver rsolver = getContext().getContentResolver();
ContentValues values = new ContentValues();
Uri result = rsolver.insert(raw_contactsUri, values); //插入空值在id里,因为id是自动生成的
long id = ContentUris.parseId(result); //得到id
//插入名字
values.put("raw_contact_id", id); //插入ID
values.put("mimetype", "vnd.android.cursor.item/name"); //类形是名字
values.put("data1", "shibao2012012"); //联系人的名字
rsolver.insert(dataUri, values); //插入名字
//插入电话号码
values.put("raw_contact_id", id);
values.put("mimetype", "vnd.android.cursor.item/phone_v2");
values.put("data1", "13025882326");
rsolver.insert(dataUri, values);
//地址
values.put("raw_contact_id", id);
values.put("mimetype", "vnd.android.cursor.item/postal-address_v2");
values.put("data1", "china");
rsolver.insert(dataUri, values);
}
//批量操作.插入联系人到通信录
public void testContactWrithBatch() throws Exception{
ContentResolver resolver = getContext().getContentResolver();
ArrayList<ContentProviderOperation> operations =new ArrayList<ContentProviderOperation>();
//插入id,给空值,因为id是自动生成的
ContentProviderOperation operation1 = ContentProviderOperation.newInsert(raw_contactsUri)
.withValue("_id", null)
.build();
//插入名字
ContentProviderOperation operation2 = ContentProviderOperation.newInsert(dataUri)
.withValueBackReference("raw_contact_id", 0) //这里的0是代表第一个ContentProviderOperation operation1。
.withValue("mimetype", "vnd.android.cursor.item/name") //插入的类型
.withValue("data1", "shibao") //名字
.build();
//插入电话号码
ContentProviderOperation operation3= ContentProviderOperation.newInsert(dataUri)
.withValueBackReference("raw_contact_id", 0)
.withValue("mimetype", "vnd.android.cursor.item/phone_v2")
.withValue("data1", "110")
.build();
//插入邮箱
ContentProviderOperation operation4 = ContentProviderOperation.newInsert(dataUri)
.withValueBackReference("raw_contact_id", 0)
.withValue("mimetype", "vnd.android.cursor.item/email_v2")
.withValue("data1", "zzh@163.com")
.build();
//添加 到 ArrayList<ContentProviderOperation> 中去
operations.add(operation1);
operations.add(operation2);
operations.add(operation3);
operations.add(operation4);
resolver.applyBatch("com.android.contacts", operations); //执行批量操作
}
}
<img width="883" height="457" style="width: 863px; height: 377px;" src="https://img-blog.csdn.net/20150109202554888?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb0ppYW5MaWFu/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />