android写入联系人Contacts的信息,包括联系人的姓名,联系方式和邮箱

时间:2022-08-31 23:49:55

1:androidmanifest.xml的内容

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.itcast.contacts"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="android.test.runner" />
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="cn.itcast.contacts" android:label="Tests for My App" />
</manifest>


2:ContactTest类,主要提供insert的插入方法

package cn.itcast.contacts.test;import android.content.ContentUris;import android.content.ContentValues;import android.net.Uri;import android.provider.ContactsContract.Data;import android.provider.ContactsContract.RawContacts;import android.provider.ContactsContract.CommonDataKinds.Email;import android.provider.ContactsContract.CommonDataKinds.Phone;import android.provider.ContactsContract.CommonDataKinds.StructuredName;import android.test.AndroidTestCase;public class ContactTest extends AndroidTestCase {/* * 首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获得系统返回的rawContactId * 这时后面插入data表的数据,只有执行空值插入,才能使插入的联系人在通讯录里面可见 */public void testInsert() {ContentValues values = new ContentValues();/* * 首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获得系统返回的rawContactId */Uri rawContactUri = this.getContext().getContentResolver().insert(RawContacts.CONTENT_URI, values);long rawContactId = ContentUris.parseId(rawContactUri);//往data表里写入姓名数据values.clear();values.put(Data.RAW_CONTACT_ID, rawContactId);values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE); //内容类型    values.put(StructuredName.GIVEN_NAME, "李天山");    this.getContext().getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);        //往data表里写入电话数据    values.clear();    values.put(Data.RAW_CONTACT_ID, rawContactId);    values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);    values.put(Phone.NUMBER, "13921009789");    values.put(Phone.TYPE, Phone.TYPE_MOBILE);    this.getContext().getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);        //往data表里写入Email的数据    values.clear();    values.put(Data.RAW_CONTACT_ID, rawContactId);    values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);    values.put(Email.DATA, "liming@itcast.cn");    values.put(Email.TYPE, Email.TYPE_WORK);    this.getContext().getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);    }}


3:测试结果,表明插入联系人成功

android写入联系人Contacts的信息,包括联系人的姓名,联系方式和邮箱

4:另外一种方式来添加联系人的信息

/* * 批量添加,处于用一个事务中 */public void testSave() {ArrayList<ContentProviderOperation> ops =new ArrayList<ContentProviderOperation>();int rawContactInsertIndex = ops.size();ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)          .withValue(RawContacts.ACCOUNT_TYPE, null)          .withValue(RawContacts.ACCOUNT_NAME, null)          .build()); ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)          .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)          .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)          .withValue(StructuredName.DISPLAY_NAME, "良辰")          .build());  ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)          .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)          .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)          .withValue(Phone.NUMBER, "1234567890")          .withValue(Phone.TYPE, Phone.TYPE_MOBILE)          .withValue(Phone.LABEL, "手机号")          .build());  ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)          .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)          .withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)          .withValue(Email.DATA, "liangchen@itcast.cn")          .withValue(Email.TYPE, Email.TYPE_WORK)          .build());         ContentProviderResult[] results;try {results = this.getContext().getContentResolver()            .applyBatch(ContactsContract.AUTHORITY, ops); for(ContentProviderResult result : results) {         Log.i(TAG, result.uri.toString());         }} catch (RemoteException e) {e.printStackTrace();} catch (OperationApplicationException e) {e.printStackTrace();}}

这种方法的好处用文档的英文表述就是:
The batch method is by far preferred. It inserts the raw contact and its constituent data rows in a single database transaction and causes at most one aggregation pass.