如何在Android中向现有用户添加电子邮件

时间:2015-04-20 02:27:22

标签: android android-contentprovider

我使用此代码添加新联系人:

ContentResolver mContentResolver = v.getContext().getContentResolver();

ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();

ContentProviderOperation.Builder builder;
builder = ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI);
builder.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, account.name);
builder.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, account.type);
operationList.add(builder.build());

builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
builder.withValueBackReference(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, 0);
builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name);
operationList.add(builder.build());

mContentResolver.applyBatch(ContactsContract.AUTHORITY, operationList);

关键是,在添加联系人后,如何向该联系人添加电子邮件?

我知道同时插入电子邮件:

builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
builder.withValue(ContactsContract.CommonDataKinds.Email.DATA, email);
builder.withValue(ContactsContract.CommonDataKinds.Email.TYPE,ContactsContract.CommonDataKinds.Email.TYPE_HOME);
operationList.add(builder.build());

2 个答案:

答案 0 :(得分:0)

这是解决方案。您需要raw_contact_id

private void InsertEmail(long raw_contact_id, String email){
    ContentValues values = new ContentValues();
    values.put(ContactsContract.Data.RAW_CONTACT_ID, raw_contact_id);
    values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
    values.put(ContactsContract.CommonDataKinds.Email.DATA, email);
    values.put(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_HOME);
    context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
}

答案 1 :(得分:0)

创建新联系人并插入数据库后,保存RAW_CONTACT_ID,然后在此ID下插入新电子邮件。

这是一个例子。

    ArrayList<ContentProviderOperation> ops = new ArrayList<>();

    ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI).withValue(
            ContactsContract.RawContacts.ACCOUNT_NAME, "null"
    ).withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "null").build());

    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "test").build());

    try {
        ContentProviderResult[] results = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        if (results != null && results.length >= 1) {
            ops.clear();

            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValue(ContactsContract.Data.RAW_CONTACT_ID, ContentUris.parseId(results[0].uri))
                    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.Email.DATA, "test@abc.com").build());

            getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
            Log.d(TAG, results[0].uri.toString());
        } else {
            Log.d(TAG, "result is empty");
        }
    } catch (RemoteException e) {
        e.printStackTrace();
    } catch (OperationApplicationException e) {
        e.printStackTrace();
    }