Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add my apps connection in Phonebook/Contacts as Whatsapp and Viber does?

enter image description here

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

like image 653
Arth Tilva Avatar asked Jul 09 '15 14:07

Arth Tilva


People also ask

How to add WhatsApp contacts to Android phone?

Step 1: Unlock your Android gadget and open the WhatsApp tool. In the conversation screen, you will find the ‘Contacts’ icon displayed at the right bottom corner of the screen. Tap the ‘Contacts’ icon and choose the ‘New Contact’ option on the next screen.

How do I add someone to my contacts on Viber?

When you first interact with someone on Viber, a banner will be displayed allowing you to add the person to your contacts list. Tap on Add to contacts and then the Tick (Android) or Save (iOS) to confirm. When someone shares a contact with you, tap Save contact on their info card and then tap the Tick (Android) or Save (iOS) to confirm.

How do I find my contacts on WhatsApp?

For this, you can just launch WhatsApp on your device and tap on the Contacts icon from the top (can be located at the bottom in some versions). This will display a list of all the contacts that are already saved on your WhatsApp account.

How do I Sync my WhatsApp account with my phone?

From settings go to “Accounts”. If your WhatsApp account is already added to your phone then it will show here. Otherwise click on Add Account and add your WhatsApp Account in your phone. Click on WhatsApp. Click on the 3 dots icon and then press “Sync your WhatsApp”


1 Answers

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() deletes all the contacts that has account type of your package, so you have to add more conditions of deleting that particular contact.

  2. 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();
        }
    }
like image 83
nidhi Avatar answered Oct 11 '22 08:10

nidhi