有时应用开发需要访问通讯录,对手机通讯录进行读写操作。这时就需要用到ContentProvider。
我们通过一个小的案例来学习如何实现此功能。
package cn.meng.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.test.AndroidTestCase; import android.util.Log; public class ContactsTest extends AndroidTestCase{ private static final String TAG = "ContactsTest"; //获取所有联系人 public void testContacts(){ Uri uri = Uri.parse("content://com.android.contacts/contacts"); //获得ContentResolver ContentResolver resolver = this.getContext().getContentResolver(); Cursor cursor = resolver.query(uri, new String[]{"_id"}, null, null, null); while(cursor.moveToNext()){ int contactsId = cursor.getInt(0); //访问某个联系人下的所有数据 uri = Uri.parse("content://com.android.contacts/contacts/" + contactsId + "/data"); StringBuilder sb = new StringBuilder("contactsId="+contactsId); Cursor datacursor = resolver.query(uri, new String[]{"mimetype","data1","data2"}, null, null, null); while(datacursor.moveToNext()){ String data = datacursor.getString(datacursor.getColumnIndex("data1")); String type = datacursor.getString(datacursor.getColumnIndex("mimetype")); if("vnd.android.cursor.item/name".equals(type)){ //姓名 sb.append(",name="+data); }else if("vnd.android.cursor.item/email_v2".equals(type)){ //邮件 sb.append(",email="+data); }else if("vnd.android.cursor.item/phone_v2".equals(type)){ //电话 sb.append(",phone="+data); } } Log.i(TAG, sb.toString()); } } //根据号码查找联系人的姓名 public void testContactNameByPhone(){ String phoneNum = "5633288"; Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/"+phoneNum); //获得ContentResolver ContentResolver resolver = this.getContext().getContentResolver(); Cursor cursor = resolver.query(uri, new String[]{"display_name"}, null, null, null); while(cursor.moveToNext()){ String name = cursor.getString(0); Log.i(TAG, name); } cursor.close(); } //向通讯录中添加联系人,此步骤是分开的,如果有部分错误,错误的添加不进去,不影响其他,有弊端 public void testAddContact()throws Exception{ //向raw_contacts表中添加数据 Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); //获得ContentResolver ContentResolver resolver = this.getContext().getContentResolver(); long contactsId = ContentUris.parseId(resolver.insert(uri, new ContentValues())); //向data表中添加数据 uri = Uri.parse("content://com.android.contacts/data"); ContentValues values = new ContentValues(); values.put("raw_contact_id", contactsId); values.put("mimetype", "vnd.android.cursor.item/name");//姓名 values.put("data2", "大大"); resolver.insert(uri,values); values.clear(); values.put("raw_contact_id", contactsId); values.put("mimetype", "vnd.android.cursor.item/phone_v2");//电话 values.put("data1", "13831056895"); values.put("data2", "2"); resolver.insert(uri,values); values.clear(); values.put("raw_contact_id", contactsId); values.put("mimetype", "vnd.android.cursor.item/email_v2");//邮箱 values.put("data1", "[email protected]"); values.put("data2", "1"); resolver.insert(uri,values); } //向通讯录中添加联系人,整体作为一个步骤,有部分出错,就添加不进去联系人信息 public void testAddContact2()throws Exception{ //向raw_contacts表中添加数据 Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); //获得ContentResolver ContentResolver resolver = this.getContext().getContentResolver(); ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(); ContentProviderOperation op1 = ContentProviderOperation.newInsert(uri) .withValue("account_name", null).build(); operations.add(op1); //向data表中添加数据 uri = Uri.parse("content://com.android.contacts/data"); //0指的是operations集合下标为0的第一个元素,获得Id ContentProviderOperation op2 = ContentProviderOperation.newInsert(uri) .withValueBackReference("raw_contact_id", 0) .withValue("mimetype", "vnd.android.cursor.item/name") .withValue("data2", "小小") .build(); operations.add(op2); ContentProviderOperation op3 = ContentProviderOperation.newInsert(uri) .withValueBackReference("raw_contact_id", 0) .withValue("mimetype", "vnd.android.cursor.item/phone_v2") .withValue("data1", "13523568963") .withValue("data2", "2") .build(); operations.add(op3); ContentProviderOperation op4 = ContentProviderOperation.newInsert(uri) .withValueBackReference("raw_contact_id", 0) .withValue("mimetype", "vnd.android.cursor.item/email_v2") .withValue("data1", "[email protected]") .withValue("data2", "1") .build(); operations.add(op4); resolver.applyBatch("com.android.contacts", operations); } }
项目清单配置如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.meng.contacts" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" /> <!-- 申请读写权限 --> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".MainActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 单元测试 --> <uses-library android:name="android.test.runner"/> </application> <!-- 单元测试 --> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="cn.meng.contacts"></instrumentation> </manifest>