Search code examples
androidandroid-contactsaccountmanagerandroid-syncadapter

How to make a custom account show up like Google/LinkedIn/Facebook in the native Contacts app?


I'm currently working on an application, where we are going to be adding contacts from our own application, similar to how LinkedIn has connections and Facebook has friends. Therefore we want our custom account, that is shown in the images below (as "MyAppName") with the contacts added from our application:

We currently have a SyncAdapter as seen from the first image, as just wish for this to be shown in the Contacts application. We've been looking at documentation, but couldn't find anything specific to this.

This is not about adding contacts, but getting the account to show up in the contacts application under "Accounts".


Solution

  • After having studied Budius' suggestions, I finally figured out how to do it. Here is a more precise link to the place where it's stated in the documentation. Basically you just need to make your account visible. Other than that, I found the answer on how to do that here.

    ContentProviderClient client = getContentResolver().acquireContentProviderClient(ContactsContract.AUTHORITY_URI);
    ContentValues values = new ContentValues();
    values.put(ContactsContract.Groups.ACCOUNT_NAME, account.name);
    values.put(Groups.ACCOUNT_TYPE, account.type);
    values.put(Settings.UNGROUPED_VISIBLE, true);
    try
    {
       client.insert(Settings.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build(), values);
    }
    catch (RemoteException e)
    {
       e.printStackTrace();
    }
    

    All credit for this code goes to Henry Pushel.