如何显示内部的自定义类型联系人像WhatsApp android

时间:2015-05-27 10:43:59

标签: android android-contacts contactscontract

/**
     * Account type id
     */
    public static final String ACCOUNT_TYPE = "com.test.app";

    /**
     * Account name
     */
    public static final String ACCOUNT_NAME = "Test";

public static void addContact(Context context, User contact) {
        ContentResolver resolver = context.getContentResolver();
        resolver.delete(RawContacts.CONTENT_URI, RawContacts.ACCOUNT_TYPE
                + " = ?", new String[] { AccountConstants.ACCOUNT_TYPE });

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

        ops.add(ContentProviderOperation
                .newInsert(
                        addCallerIsSyncAdapterParameter(
                                RawContacts.CONTENT_URI, true))
                .withValue(RawContacts.ACCOUNT_NAME,
                        AccountConstants.ACCOUNT_NAME)
                .withValue(RawContacts.ACCOUNT_TYPE,
                        AccountConstants.ACCOUNT_TYPE)
                // .withValue(RawContacts.SOURCE_ID, 12345)
                // .withValue(RawContacts.AGGREGATION_MODE,
                // RawContacts.AGGREGATION_MODE_DISABLED)
                .build());

        ops.add(ContentProviderOperation
                .newInsert(
                        addCallerIsSyncAdapterParameter(Settings.CONTENT_URI,
                                true))
                .withValue(RawContacts.ACCOUNT_NAME,
                        AccountConstants.ACCOUNT_NAME)
                .withValue(RawContacts.ACCOUNT_TYPE,
                        AccountConstants.ACCOUNT_TYPE)
                .withValue(Settings.UNGROUPED_VISIBLE, 1).build());

        ops.add(ContentProviderOperation
                .newInsert(
                        addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true))
                .withValueBackReference(Data.RAW_CONTACT_ID, 0)
                .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                .withValue(StructuredName.GIVEN_NAME, contact.getFullname())
                .withValue(StructuredName.FAMILY_NAME, contact.getFullname())
                .build());

        ops.add(ContentProviderOperation
                .newInsert(
                        addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true))
                .withValueBackReference(Data.RAW_CONTACT_ID, 0)
                .withValue(
                        ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
                        contact.getPhoneNumber()).build());

        ops.add(ContentProviderOperation
                .newInsert(
                        addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true))
                .withValueBackReference(Data.RAW_CONTACT_ID, 0)
                .withValue(
                        ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Email.DATA,
                        contact.getEmail()).build());

        ops.add(ContentProviderOperation
                .newInsert(
                        addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true))
                .withValueBackReference(Data.RAW_CONTACT_ID, 0)
                .withValue(Data.MIMETYPE, MIMETYPE)
                .withValue(Data.DATA1, contact.getFullname())
                .withValue(Data.DATA2, contact.getEmail())
                .withValue(Data.DATA3, contact.getHomeAddress()).build());
        try {
            ContentProviderResult[] results = resolver.applyBatch(
                    ContactsContract.AUTHORITY, ops);
            if (results.length == 0)
                AppLog.d(TAG, "Failed to add.");
        } catch (Exception e) {
            AppLog.e(TAG, e.getMessage(), e);
        }
    }

I would like to show my app account same as whats app

问题 - 目前代码正在添加新联系人,但未根据电话号码将其合并到现有联系人中。在添加联系人之前,我还有什么需要做的吗?我想在Whats App中显示我的应用程序帐户。

我已经实现了SyncService,SyncAdapter,Authenticator,contacts.xml和项目所需的其他类。唯一不起作用的是显示默认联系人应用内的联系,而不是创建新的联系人。

<ContactsSource xmlns:android="http://schemas.android.com/apk/res/android" >

    <ContactsDataKind
        android:detailColumn="data2"
        android:detailSocialSummary="true"
        android:icon="@drawable/ic_launcher"
        android:mimeType="vnd.android.cursor.item/com.test.app"
        android:summaryColumn="data3" />

</ContactsSource>

1 个答案:

答案 0 :(得分:0)

我在Android 6.1上遇到了同样的问题,正如我所听到的,这个问题出现在Lollipop之后。网络上的每个实施都表明联系人匹配应该根据电话号码工作。它适用于早期的系统 - 我在KitKat上尝试过,它就像一个魅力。但是,由于Android 5.0联系人不知何故不匹配。

幸运的是,电话号码不是匹配所依赖的唯一参数 - 还有displayname。因此,工作实现应如下所示:

// as displayName pass the retrieved   ContactsContract.Contacts.DISPLAY_NAME

@Override
public void addContact(@Nonnull final String displayName, @Nonnull final String phone) {

    final ContentResolver resolver = context.getContentResolver();

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

        ops.add(ContentProviderOperation
                .newInsert(addCallerIsSyncAdapterParameter(ContactsContract.RawContacts.CONTENT_URI, true))
                .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, ACCOUNT_NAME)
                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, ACCOUNT_TYPE)
                .withValue(ContactsContract.RawContacts.AGGREGATION_MODE,
                        ContactsContract.RawContacts.AGGREGATION_MODE_DEFAULT)
                .build());

        ops.add(ContentProviderOperation
                .newInsert(addCallerIsSyncAdapterParameter(ContactsContract.Data.CONTENT_URI, true))
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone)
                .build());

        ops.add(ContentProviderOperation
                    .newInsert(addCallerIsSyncAdapterParameter(ContactsContract.Data.CONTENT_URI, true))
                    .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());

        ops.add(ContentProviderOperation
                .newInsert(addCallerIsSyncAdapterParameter(ContactsContract.Data.CONTENT_URI, true))
                .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(ContactsContract.Data.MIMETYPE, YOUR_MIMETYPE)
                .withValue(ContactsContract.Data.DATA1, 12345)
                .withValue(ContactsContract.Data.DATA2, "user")
                .withValue(ContactsContract.Data.DATA3, "action")
                .build());

        try {
            resolver.applyBatch(ContactsContract.AUTHORITY, ops);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

我离开了phoneNumber,以防联系人没有任何显示名称 - 那么它至少会与之前的Android版本相匹配。