How to show custom type contact inside Contact lik

2019-03-21 02:10发布

/**
     * 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

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条回答
闹够了就滚
2楼-- · 2019-03-21 02:40

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.

查看更多
登录 后发表回答