如何以编程方式在android中的联系人中添加后缀名称

时间:2015-11-02 15:48:19

标签: android android-contacts

我想在联系人列表中添加新联系人的一些详细信息。我想在这里添加姓名,联系电话和后缀名称。我可以添加姓名和联系电话。但我无法添加后缀名称。我不知道这是添加后缀名称的正确代码。我在下面给出了我的代码。

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        int rawContactId = ops.size();
        ops.add(ContentProviderOperation.newInsert(
                ContactsContract.RawContacts.CONTENT_URI)
                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
                .build());


            ops.add(ContentProviderOperation.newInsert(
                    ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
                    .withValue(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                    .withValue(
                            ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
                            groupInfo.getName()).build());

            ops.add(ContentProviderOperation.newInsert(
                    ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
                    .withValue(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                    .withValue(
                            ContactsContract.CommonDataKinds.StructuredName.SUFFIX,
                           groupInfo.getCompanyName()).build());



            ops.add(ContentProviderOperation.
                    newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
                    .withValue(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, groupInfo.getContactNumber())
                    .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
                            ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
                    .build());

        try {
            context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);


            Toast.makeText(context, "This person is added successfully to your contact list", Toast.LENGTH_LONG).show();
        } catch (RemoteException e) {
            Toast.makeText(context, "Can not add to your contact list", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        } catch (OperationApplicationException e) {
            e.printStackTrace();
            Toast.makeText(context, "Can not add to your contact list", Toast.LENGTH_LONG).show();
        }

1 个答案:

答案 0 :(得分:0)

最后,我找到了解决方案myselft ..

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        int rawContactId = ops.size();
        ops.add(ContentProviderOperation.newInsert(
                ContactsContract.RawContacts.CONTENT_URI)
                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
                .build());


           ops.add(ContentProviderOperation.newInsert(
                ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
                .withValue(ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                .withValue(
                        ContactsContract.CommonDataKinds.StructuredName.SUFFIX,
                        groupInfo.getCompanyName())
                .withValue(
                        ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
                        groupInfo.getName())
                .build());



            ops.add(ContentProviderOperation.
                    newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
                    .withValue(ContactsContract.Data.MIMETYPE,
                            ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, groupInfo.getContactNumber())
                    .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
                            ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
                    .build());

        try {
            context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);


            Toast.makeText(context, "This person is added successfully to your contact list", Toast.LENGTH_LONG).show();
        } catch (RemoteException e) {
            Toast.makeText(context, "Can not add to your contact list", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        } catch (OperationApplicationException e) {
            e.printStackTrace();
            Toast.makeText(context, "Can not add to your contact list", Toast.LENGTH_LONG).show();
        }