2

I'm using the following piece of codes to create a new contact. It follow closely the ContactManager example provided by Android. The problem is, the created contacts do not appear in the Contacts app that shipped with Android. Nevertheless, when I load all the contacts from the phonebook, I can see the newly created contacts.

private void insertPBEntry() throws RemoteException, OperationApplicationException {

     ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

     ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
             .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, "Account type")
             .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, "Account name")
             .build());

     ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
             .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
             .withValue(ContactsContract.Data.MIMETYPE,
                     ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
             .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "TOTAL_NEW")
             .build());
     ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
             .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
             .withValue(ContactsContract.Data.MIMETYPE,
                     ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
             .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, "9090")
             .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,Phone.TYPE_MOBILE)
             .build());     
     getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

}

I've searched hard but have yet to find the answer. I found one answer suggesting that the problem might have (sth) to do with my strings "Account type" and "Account name". For my case, I do not need to create any account whatsoever. All I want is to add a new contact with a name, email/mail address, phones.

Thanks, guys!

Paul Hoang
  • 1,014
  • 2
  • 11
  • 21
  • All contacts must be inserted under an account. Investigate further. – Pentium10 Jul 26 '10 at 17:33
  • Upon further investigations, I found this: http://osdir.com/ml/Android-Developers/2010-05/msg03859.html So, those contacts created using the codes above are not shown may be because they don't be long to a group whose visibility is turned on. As such, everything boils down to finding the group id of those contacts created in Contacts app when using its interface. I tried to get all groups from ContactsContracts.Groups and get ZERO group. Similarly, fetching data from GroupMembership returns ZERO group membership. Interesting! Let's see how... – Paul Hoang Jul 27 '10 at 15:36

4 Answers4

7

The sample codes provided by Google work. Just that when it's run on the emulator, no account or group can be found to attach the created contact to. And by default, this newly created contact is not visible.

Using the actual phone (for my case, HTC Dream), after detecting the account name and type to feed in the codes, it works. Alternatively, we can get the visible group ids available and attach the new contact to one of those groups.

To get the available accounts:

//accounts
    Account[] accounts = AccountManager.get(act).getAccounts(); 
    for (Account acc : accounts){
        Log.d(TAG, "account name = " + acc.name + ", type = " + acc.type);
    }

To get the list of groups:

//group membership info
    String[] tempFields = new String[] {
            GroupMembership.GROUP_ROW_ID, GroupMembership.GROUP_SOURCE_ID};
    Cursor tempCur = act.managedQuery(Data.CONTENT_URI, tempFields,
             Data.MIMETYPE + "='" + GroupMembership.CONTENT_ITEM_TYPE + "'",
             null, null);

Now, if we want to associate the new contact to a group instead of an account:

ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
            .withValueBackReference(Data.RAW_CONTACT_ID, 0)
            .withValue(ContactsContract.Data.MIMETYPE, GroupMembership.CONTENT_ITEM_TYPE)
            .withValue(GroupMembership.GROUP_SOURCE_ID, *<THE_IDENTIFIED_GROUP_ID>*)
            .build());

Hope it helps.

Paul Hoang
  • 1,014
  • 2
  • 11
  • 21
  • hungh3, is it possible to add contacts directly to a group without having an account ? – Mikey Oct 14 '10 at 09:44
  • Yes. But on the emulator, I wasn't able to get any group. On a real phone, there should be >= 1 group. Then attach the new contact to your chosen group as illustrated above. – Paul Hoang Oct 15 '10 at 06:06
5

To add an account in emulator that has no groups or accounts, just put "null" as your account or group id, replace the line of code like this

ops.add(ContentProviderOperation
    .newInsert(ContactsContract.RawContacts.CONTENT_URI)
    .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)  
    .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
    .build());
Luís Ramalho
  • 10,018
  • 4
  • 52
  • 67
user402057
  • 91
  • 1
  • 2
0

Did you try to set the visibility of your group to true?

In Contacts Tab press the menu button, than "Display options" > your Account and than check the boxes and "Done".

white_gecko
  • 4,808
  • 4
  • 55
  • 76
  • I'm using the HTC Dream and I couldn't find the exact same details as you mentioned. However all of my contacts seem to fall into some default group and the group is already set to visible (as all other contacts in this group, which were created manually, are visible). Anyway, thanks for your suggestions. – Paul Hoang Aug 30 '10 at 14:10
0

HTC Sense and MOTOBLUR can be problematic with contacts. I don't know if any of the information here (http://stackoverflow.com/questions/4431101/created-contacts-not-showing-up-on-htc-evo) is useful.

Jon O
  • 6,532
  • 1
  • 46
  • 57