How to show custom type contact inside Contact like WhatsApp android

*爱你&永不变心* 提交于 2019-12-09 06:31:15

问题


/**
     * 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);
        }
    }

Problem - Currently the code is adding new contact but not merging it into existing contact based on Phone number. Is there anything I have to do before adding the contact? I would like to display my app account inside Contact same as Whats App.

I have implemented SyncService, SyncAdapter, Authenticator, contacts.xml and other classes required for the project. The only thing not working is showing contact inside the default Contact app instead of creating new contact.

<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:


I had the same problem on Android 6.1 and as I heard, this issue is present since Lollipop. Every implementation on the web shows that the contact matching should work based on phone number. And it works on earlier systems - I tried it on KitKat and it works like a charm. But since Android 5.0 contacts don't match somehow.

Fortunately phone number is not the only parameter the matching depends on - there is also displayname. So the working implementation should look like this:

// 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();
        }

    }

I left the phoneNumber in case the contact doesn't have any displayname - then it would at least match on earlier Android versions.



来源:https://stackoverflow.com/questions/30480215/how-to-show-custom-type-contact-inside-contact-like-whatsapp-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!