在联系人中添加应用程序链接,例如whatsApp

时间:2018-06-28 11:31:58

标签: android-contact-mimetype

我想在WhatsApp之类的联系人中添加四个Mimetype。代码如下:

 public static void addContact(Context context, MyContact contact){

        ContentResolver resolver = context.getContentResolver();
        boolean mHasAccount = isAlreadyRegistered(resolver, contact.Id);


        if(mHasAccount){
            Log.v(TAG,"Count..Account exist");
            updateMyContact(context,contact.Id);

        } else {

            Log.v(TAG,"Count..Adding account ");
            ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

            // insert account name and account type
            ops.add(ContentProviderOperation
                    .newInsert(addCallerIsSyncAdapterParameter(ContactsContract.RawContacts.CONTENT_URI, true))
                    .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, Constant.ACCOUNT_NAME)
                    .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, Constant.ACCOUNT_TYPE)
                    .withValue(ContactsContract.RawContacts.AGGREGATION_MODE, ContactsContract.RawContacts.AGGREGATION_MODE_DEFAULT)
                    .build());

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

            // insert contact name
           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(CommonDataKinds.StructuredName.DISPLAY_NAME, contact.name)
                    .build());

            // insert mime-type data
            ops.add(ContentProviderOperation
                    .newInsert(addCallerIsSyncAdapterParameter(ContactsContract.Data.CONTENT_URI, true))
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                    .withValue(ContactsContract.Data.MIMETYPE, MIMETYPE)
                    .withValue(ContactsContract.Data.DATA1,  contact.Id)
                    .withValue(ContactsContract.Data.DATA2, "Option1")
                    .withValue(ContactsContract.Data.DATA3, "Option1 text")
                    .build());

            ops.add(ContentProviderOperation
                    .newInsert(addCallerIsSyncAdapterParameter(ContactsContract.Data.CONTENT_URI, true))
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                    .withValue(ContactsContract.Data.MIMETYPE, MIMETYPE1)
                    .withValue(ContactsContract.Data.DATA4,  contact.Id)
                    .withValue(ContactsContract.Data.DATA5, "Option2")
                    .withValue(ContactsContract.Data.DATA6, "Option2 text")
                    .build());


            ops.add(ContentProviderOperation
                    .newInsert(addCallerIsSyncAdapterParameter(ContactsContract.Data.CONTENT_URI, true))
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                    .withValue(ContactsContract.Data.MIMETYPE, MIMETYPE2)
                    .withValue(ContactsContract.Data.DATA7, contact.Id)
                    .withValue(ContactsContract.Data.DATA8, "Option3")
                    .withValue(ContactsContract.Data.DATA9, "Option3 text")
                    .build());

            ops.add(ContentProviderOperation
                    .newInsert(addCallerIsSyncAdapterParameter(ContactsContract.Data.CONTENT_URI, true))
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
                    .withValue(ContactsContract.Data.MIMETYPE, MIMETYPE3)
                    .withValue(ContactsContract.Data.DATA11,  contact.Id)
                    .withValue(ContactsContract.Data.DATA12, "Option4")
                    .withValue(ContactsContract.Data.DATA13, "Option4 text)
                    .build());
            try {
                Log.v(TAG,"Count..Apply batch");
                resolver.applyBatch(ContactsContract.AUTHORITY, ops);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

和contact.xml如下:

<ContactsSource
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ContactsDataKind
        android:mimeType="vnd.android.cursor.item/packagename.prescription"
        android:icon="@mipmap/launcher_icon"
        android:summaryColumn="data2"
        android:detailColumn="data3"
        />
    <ContactsDataKind
        android:mimeType="vnd.android.cursor.item/packagename.report"
        android:icon="@mipmap/launcher_icon"
        android:summaryColumn="data5"
        android:detailColumn="data6"

        />
    <ContactsDataKind
        android:mimeType="vnd.android.cursor.item/packagename.invoice"
        android:icon="@mipmap/launcher_icon"
        android:summaryColumn="data8"
        android:detailColumn="data9"

        />
    <ContactsDataKind
        android:mimeType="vnd.android.cursor.item/packagename.addpatient"
        android:icon="@mipmap/launcher_icon"
        android:summaryColumn="data11"
        android:detailColumn="data12"

        />

</ContactsSource >

但是只有两个正在与我的联系人同步。参考代码在这里 http://blogs.quovantis.com/syncing-contacts-with-an-android-application-2/?  还有其他方法可以添加联系人链接吗?请帮助我。

0 个答案:

没有答案