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
@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.
Add to AndroidManifest.xml uses-permission for WRITE_PROFILE
<uses-permission android:name="android.permission.WRITE_PROFILE"/>
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
}
var values = new ContentValues (); values.Put (ContactsContract.Contacts.InterfaceConsts.is_user_profile,"1"); ontentResolver.Update (ContactsContract.Profile.ContentRawContactsUri,values, null, null);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With