1

From my app, I want to invoke the Contacts editor to allow the user to add a new contact. I want to find a way for the app to pass to the editor an account to be used. How can this be done?

I have not found any documentation on how to do this. I did find some code on github which shows a contact editor retrieving account info (see ContactEditorActivity). It calls

  getParcelable(Intents.Insert.ACCOUNT);

I think this must be deprecated code though, as I can't find the value Intents.Insert.ACCOUNT anywhere in the references.

Overall, my code for invoking the editor is working; here's an extract:

  Intent intent = new Intent();
  intent.setAction (Intent.ACTION_INSERT);
  intent.setData (ContactsContract.Contacts.CONTENT_URI);
  intent.putExtra (ContactsContract.Intents.Insert.NAME, name);
  ... "put" other values ...
  startActivityForResult (intent, ACTIVITY_REQUEST_FULL_EDIT);

Thanks.

Peri Hartman
  • 19,314
  • 18
  • 55
  • 101

1 Answers1

1

I know this is an old question, but here is my solution which seemed to work for me, just in case someone has the same problem.

I was using Xamarin for Android but the Java code is pretty much the same.

For API 23+, use the ExtraAccount field EG:

Android.Accounts.Account account = new Android.Accounts.Account(accountName, accountType);

...
intent.PutExtra(ContactsContract.Intents.Insert.ExtraAccount, account);
...

And for older APIs I used the field "com.android.contacts.extra.ACCOUNT" EG:

Android.Accounts.Account account = new Android.Accounts.Account(accountName, accountType);

...
intent.PutExtra("com.android.contacts.extra.ACCOUNT", account);
...

This opened the contact intent, defaulting to the passed in account (by-passing any phone defaults). This was handy because I was setting a whole bunch of information like url, job title etc and it was getting lost because the contact intent was defaulting to SIM card - which can not hold this data!

In case your are wondering how to get the accounts on the phone, you can use:

AccountManager.Get(context).GetAccountsByType("com.google"); 

to get google accounts, or

AccountManager.Get(context).GetAccounts(); 

to get all accounts. However neither include the phone account or SIM account, so I used this code to get these:

    using Android.Accounts;

...

    public List<Account> GetAccounts()
    {
            if (CheckReadContactsPermission())
            {
                var accountList = new List<Account>();
                var uri = ContactsContract.Settings.ContentUri;

                string[] projection = { ContactsContract.Settings.InterfaceConsts.AccountName,
                                    ContactsContract.Settings.InterfaceConsts.AccountType };

                var loader = new CursorLoader(context, uri, projection, null, null, null);
                var cursor = (ICursor)loader.LoadInBackground();

                if (cursor.MoveToFirst())
                {
                    do
                    {
                        accountList.Add(new Account(cursor.GetString(cursor.GetColumnIndex(projection[0])),
                            cursor.GetString(cursor.GetColumnIndex(projection[1]))));
                    } while (cursor.MoveToNext());
                }

                return accountList;
            }

            return null;
        }