Android Studio 如何在Android中添加新字段到联系人

zbsbpyhn  于 2023-01-26  发布在  Android
关注(0)|答案(1)|浏览(171)

我想为现有联系人添加新电子邮件地址。
我使用这个代码:

ArrayList<ContentProviderOperation> insertEmailList =
            new ArrayList<ContentProviderOperation>();
    Uri uri=null;
    for (ContactEmail contactEmail : emails) {
        values.put(ContactsContract.Data.RAW_CONTACT_ID, id);
        values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
        values.put(ContactsContract.CommonDataKinds.Email.ADDRESS, contactEmail.getEmailAddress());
        values.put(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_CUSTOM);
        uri=contentResolver.insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
    }

当运行此代码uricontent://com.android.contacts/data/2135,但我的联系人不是www.example.com应用程序运行无异常。updated.my app run without exception.

rekjcdws

rekjcdws1#

Get Contact Emails By Content Provider - Android Example读取Image

Cursor mRawContactCursor = context.getContentResolver().query(
            ContactsContract.RawContacts.CONTENT_URI,
            null,
            ContactsContract.Data.CONTACT_ID + " = ?",
            new String[] {ID},
            null);
    mRawContactCursor.moveToFirst();
    String rawId = mRawContactCursor.getString(mRawContactCursor.getColumnIndex(ContactsContract.RawContacts._ID));

使用此代码查找ID或RAW_CONTACT_ID,然后更新或插入到联系人。

for (ContactEmail contactEmail : emails) {
        values.put(ContactsContract.Data.RAW_CONTACT_ID, rawId);
        values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
        values.put(ContactsContract.CommonDataKinds.Email.ADDRESS, "a@a.com");
        values.put(ContactsContract.CommonDataKinds.Email.TYPE, type.home);
        contentResolver.insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
    }

相关问题