如何将应用程序连接添加到WhatsApp和Viber这样的现有联系人? [英] How to add app connections to an existing contact like WhatsApp and Viber does?

查看:121
本文介绍了如何将应用程序连接添加到WhatsApp和Viber这样的现有联系人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将Android应用程序的连接添加到现有联系人中.我正在将我的电话簿的所有联系人发送到服务器(带有姓名,电话号码和联系人ID),以检查哪些联系人已为我的应用程序注册.我会将匹配的联系人ID发送回客户端应用.

I want my android app's connection to be added in an existing contact. I am sending my all contacts of Phonebook to server(with Name,Phone Number, and Contact ID) to check which contacts are registered for my app. I will send back to client app the contact IDs which are matched.

现在,我想根据返回的ID将连接添加到电话簿中的那些联系人.

Now I want to add connection to those contacts in my Phonebook based on those returned IDs.

如何通过基于返回的联系人ID之一编辑联系人来添加应用程序连接?

How can I add app connection by editing the contact based on one of returned contact ID?

谢谢

推荐答案

尝试使用此代码,它将为您提供所需的输出

Try with this code, it will give you desired output

public static void addContact(Context context, MyContact contact) {
    ContentResolver resolver = context.getContentResolver();
     // add condition that you want to check
    String where= RawContacts.ACCOUNT_TYPE + " = ? AND " +RawContacts.DISPLAY_NAME_PRIMARY+"=?";
    //values of that condotion
    String[] value=new String[] { AccountGeneral.ACCOUNT_TYPE ,contact.name};
    resolver.delete(RawContacts.CONTENT_URI, where, value);

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

    ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(RawContacts.CONTENT_URI, true))
            .withValue(RawContacts.ACCOUNT_NAME, AccountGeneral.ACCOUNT_NAME)
            .withValue(RawContacts.ACCOUNT_TYPE, AccountGeneral.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, AccountGeneral.ACCOUNT_NAME)
            .withValue(RawContacts.ACCOUNT_TYPE, AccountGeneral.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.name) 
            .withValue(StructuredName.FAMILY_NAME, contact.lastName) 
            .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, "12342145")

            .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, "sample@email.com")
             .build());     


    ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(Data.CONTENT_URI, true))
            .withValueBackReference(Data.RAW_CONTACT_ID, 0)
            .withValue(Data.MIMETYPE, MIMETYPE)
            .withValue(Data.DATA1, 12345)
            .withValue(Data.DATA2, "sample")
            .withValue(Data.DATA3, "sample")
            .build());
    try {
        ContentProviderResult[] results = resolver.applyBatch(ContactsContract.AUTHORITY, ops);
        i++;
        if (results.length == 0)
            ;
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

这篇关于如何将应用程序连接添加到WhatsApp和Viber这样的现有联系人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆