如何在现有联系人中添加多个号码

时间:2017-06-29 06:17:50

标签: android android-contentprovider android-contacts

我可以使用此更新联系人中的号码。

String where = ContactsContract.Data.CONTACT_ID + " = ? AND " +
            ContactsContract.Data.MIMETYPE + " = ? ";
String[] params = new String[] {contacts.getContactId(),
            ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE};
ops.add(ContentProviderOperation
                    .newUpdate(ContactsContract.Data.CONTENT_URI)
                    .withSelection(where, params)
                    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,"111111")
                    .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,type_home))
                    .build());

但我还想在现有联系人中插入新号码。我试过这个

ops.add(ContentProviderOperation.
             newInsert(ContactsContract.Data.CONTENT_URI)
            .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,id)
            .withValue(ContactsContract.Data.MIMETYPE,
                    ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
            .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "111111666")
            .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
            .build());

但我无法在现有联系人中添加号码。

1 个答案:

答案 0 :(得分:0)

尝试使用以下功能添加联系人和呼叫功能,如

addContact(getApplicationContext(),"Abhi","+910123456789","+919876543210","1122334455","ImDroid","Developer");

AddContact方法

public void addContact(Context ctx, String displayname, String homenumber,
                String mobilenumber, String worknumber,String companyname, String jobtitle) {

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

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

    //------------------------------ Names ---------------------------------
    if (displayname!= null) {
        contentProviderOperation.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,
                        displayname).build());
    }

    // -------------------- Mobile Number----------------------------------
    if (mobilenumber!= null) {
        contentProviderOperation.add(ContentProviderOperation
                .newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(
                        ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(
                        ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
                        mobilenumber)
                .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
                        ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
                .build());
    }

    //---------------------- Home Numbers --------------------------------
    if (homenumber!= null) {
        contentProviderOperation.add(ContentProviderOperation
                .newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(
                        ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(
                        ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
                        homenumber)
                .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
                        ContactsContract.CommonDataKinds.Phone.TYPE_HOME)
                .build());
    }

    //----------------------------- Work Numbers -------------------------
    if (worknumber!= null) {
        contentProviderOperation.add(ContentProviderOperation
                .newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(
                        ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(
                        ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
                        worknumber)
                .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
                        ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
                .build());
    }

    // ---------------------------- Organization--------------------------
    if (!companyname.equals("") && !jobtitle.equals("")) {
        contentProviderOperation.add(ContentProviderOperation
                .newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(
                        ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(
                        ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
                .withValue(
                        ContactsContract.CommonDataKinds.Organization.COMPANY,
                        companyname)
                .withValue(
                        ContactsContract.CommonDataKinds.Organization.TYPE,
                        ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
                .withValue(
                        ContactsContract.CommonDataKinds.Organization.TITLE,
                        jobtitle)
                .withValue(
                        ContactsContract.CommonDataKinds.Organization.TYPE,
                        ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
                .build());
    }
    // Asking the Contact provider to create a new contact
    try {
        ctx.getContentResolver()
                .applyBatch(ContactsContract.AUTHORITY, contentProviderOperation);
    } catch (Exception e) {
        e.printStackTrace();
        //show exception in toast
        Toast.makeText(ctx, "Exception: " + e.getMessage(),
                Toast.LENGTH_SHORT).show();
    }
}