6

I am trying to create a user profile in ContactsContract...because there is not one, and I need one for testing. I don't have a real-life Android device, and only have the AVD Emulator for testing.

Here is the code block that I am working from:

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

ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
    .withValue(RawContacts.ACCOUNT_TYPE, null)
    .withValue(RawContacts.ACCOUNT_NAME, null)
    .build());        

ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
    .withValueBackReference(Data.RAW_CONTACT_ID, 0)
    .withValue(Data.MIMETYPE, Profile.CONTENT_RAW_CONTACTS_URI)
    .withValue(Profile.IS_USER_PROFILE, 1)
    .build()); 

ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
    .withValueBackReference(Data.RAW_CONTACT_ID, 0)
    .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
    .withValue(StructuredName.DISPLAY_NAME, name)
    .build());

this.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

If you remove the lines for making this entry a user profile, it works fine (i.e., will insert the name). However, I can not figure out how to set the entry with the IS_USER_PROFILE flag.

Can you create a user profile from an App? If so, any ideas on why this won't work?

Many thanks! Scott

Scott Wardlaw
  • 652
  • 1
  • 8
  • 13
  • I am struggling with the same problem. I cannot seem to add a profile contact when none exists! (See my question at: http://stackoverflow.com/questions/33981594/how-to-insert-profile-contact-programmatically-in-android) How did you solve this? – Pratik Thaker Nov 30 '15 at 07:21

3 Answers3

3

@ozmank gave a good pointer - thought of adding a more current working example from an app that targets API level 15-23

I encountered this issue mainly on Samsung devices - where the profile doesn't exist and it can be deleted when it does exist.

  1. Add to AndroidManifest.xml uses-permission for WRITE_PROFILE

    <uses-permission android:name="android.permission.WRITE_PROFILE"/>
    
  2. Create an empty raw contact that belongs to the profile - this will generate the profile entry

    ContentValues values = new ContentValues();            
    context.getContentResolver().insert(Profile.CONTENT_RAW_CONTACTS_URI, values);
    

from the Uri that is returned you can query the raw contact entry.

Note - This doesn't add info to the profile, so if you will query the profile for info using

Uri uri = Uri.withAppendedPath(Profile.CONTENT_URI, Contacts.Data.CONTENT_DIRECTORY);
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);

The result cursor will be empty, and maybe if you'll decide to run the code in #2 again and go into the stock contacts app you will see multiple empty entries under the Me profile - so my recommendation is to keep the me profile as clean as possible and track the raw contact that you created and check if it is still valid - before creating a new raw contact.

Uri uri = Uri.withAppendedPath(Profile.CONTENT_RAW_CONTACTS_URI, String.valueOf(lastLocalProfileManualRawId));
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);

if (cursor != null && cursor.getCount() > 0){
    // The profile exists
} else {
    // Create a new raw contact
}
Noa Drach
  • 2,381
  • 3
  • 26
  • 44
1

var values = new ContentValues (); values.Put (ContactsContract.Contacts.InterfaceConsts.is_user_profile,"1"); ontentResolver.Update (ContactsContract.Profile.ContentRawContactsUri,values, null, null);

ozmank
  • 763
  • 1
  • 8
  • 23
0

Run the Contacts app in the emulator and set up your profile with it.

You might need to first add a regular contact before you see the "Set up my profile" option.
For me the "Add Account" button did nothing, but the fab button in lower-right corner of screen worked to start adding a new regular contact.

After adding the contact, tap Back to go back to the main screen. Instead of saying you have no contacts it will now list the contact you just added plus the "Set up my profile" option in the "ME" group.

jk7
  • 1,958
  • 1
  • 22
  • 31