联系人中添加我的应用程序连接

联系人中添加我的应用程序连接

本文介绍了如何像Whatsapp和Viber一样在电话簿/联系人中添加我的应用程序连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要我的Android应用连接,将其添加到联系人中。我将根据用户需要在现有联系人中添加连接的结果来检查用户是否正在使用我的应用。

I want my Android apps connection to be added in contact. I will check whether the user is using my app or not, based o the result I need to add connection in the existing contacts.

I已经获得了很多参考,但是它增加了新的联系人并增加了连接。我使用的示例之一是

I have taken many reference but it adds a new contact with addition of a connection. One of the sample I have used is here

那么谁能告诉我在现有联系人中添加帐户的程序是什么?

So can anyone tell me what is the procedure to add account in the existing contact?

我能够创建帐户,该帐户在设置电话菜单。

I am able to create account which is visible in Setting menu of phone.

推荐答案

我也使用了本教程,但是您必须进行一项更改在 addContact()

中。在本教程中,您偏爱于此,必须更改以下代码。

I also use this tutorial but one change that you have to make is in addContact()
In this tutorial, that you are preferring, you have to change the following code.


  1. addContact()删除所有具有您的包裹帐户类型的联系人,因此您必须添加删除该特定联系人的更多条件。

  1. addContact() deletes all the contacts that has account type of your package, so you have to add more conditions of deleting that particular contact.

您必须添加要添加其现有帐户的现有联系人中已添加的详细信息。数字,电子邮件或姓名等。

you have to add the details that has already added in existing contact in which you want to add your account that is contact number or email or name etc.





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, "[email protected]")
                 .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一样在电话簿/联系人中添加我的应用程序连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 17:34