Picking the correct rawContactID when adding a new number to existing contact

196 views Asked by At

I am building a custom phonebook application. As part of this, I need to add new contacts, or additional info (phone numbers or emails) under existing contacts. While this works well under simple test cases, there are certain situations (and it's not obvious when) where I end up with two issues:

  1. Multiple copies of a contact get created, often hundreds! These are all blank, and simply share the same display name with the original contact that was modified

  2. Multiple numbers (exactly the same) under the contact. The really unusual thing is that these numbers have the same _ID in the contactContracts.Data table, so can't understand how there can be multiple instances with the same unique ID. I checked to see if it's a display issue, but doesn't seem to be

Both of the above makes me question if I am inserting information correctly. So what I want to know is:

  1. When creating a new contact, is there a default account_name and account_type I should be specifying? I am currently using null for both

  2. When I add a new phone number to an existing contact, what is the right approach to inserting data into the contactContracts.Data table? I am currently inserting this number with rawContactID as a reference ID, and I am getting this rawContactID from the contactContracts.RawContacts table, by using my contact's contactID as reference. Note that because I often get multiple raw contacts, I end up picking up the last one (arbitrary choice) to do my phone number insert.

1

There are 1 answers

0
Android Dev On

You can use AccountManager Class for getting account name & account type information.

    final AccountManager accountTypes = AccountManager.get(getApplicationContext());
    final Account accounts[] = accountTypes.getAccounts();
    for (final Account account: accounts) {
        Log.i(TAG, account.name);
    }