I used this code to add the contact:
public static long addNewNameToContact(Context context, String name) {
long rawContactId = 0;
ContentValues values = new ContentValues();
values.put(Contacts.DISPLAY_NAME, name);
Uri rawContactUri = context.getContentResolver().insert(RawContacts.CONTENT_URI, values);
rawContactId = ContentUris.parseId(rawContactUri);
return rawContactId;
}
(I need to create a contact only with the name and then add phone and other data).
Can you help me?
Very thanks,
Mateus
First, you need to create a raw contact specifying the account type and name. The account type and name can be any string. For instance accountType="com.mateus.app" and accountName="user":
ContentValues values = new ContentValues();
values.put(RawContacts.ACCOUNT_TYPE, accountType);
values.put(RawContacts.ACCOUNT_NAME, accountName);
Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
Then, you can set the display name with the raw contact ID that is returned above:
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.DISPLAY_NAME, "Mike Sullivan");
getContentResolver().insert(Data.CONTENT_URI, values);
Related
I want to create a contact in the device's address book.
The answer to the question here and the documentation indicates that we must provide the ID for the contact.
Here is the example from the docs:
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "1-800-GOOG-411");
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, "free directory assistance");
Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);
How can I choose/generate this ID ? if I choose a random value, it might conflict with an existing contact ?
if you looked at the Documentation
you will find a table describes all fields, among them is RAW_CONTACT_ID, i quote
The id of the row in the ContactsContract.RawContacts table that this data belongs to.
so this is the ID of an inserted record at RawContacts table, as if it's master detail schema
check this link for more info about RawContacts
so what i think is you have to insert a rawContact first, get that (automatically generated id)
and use it for inserting a contact
something like this
ContentValues values = new ContentValues();
values.put(RawContacts.ACCOUNT_TYPE, accountType);
values.put(RawContacts.ACCOUNT_NAME, accountName);
Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
long insertedRawContactId = ContentUris.parseId(rawContactUri);
then use insertedRawContactId to insert contact.
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, insertedRawContactId );
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "1-800-GOOG-411");
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, "free directory assistance");
Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);
check this site for more info to make it clear.
and regarding the 3 types or 3 elements of contact, check this image
and check this answer points 1,2,3,... etc
hope this help you more understanding
try like this,
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactInsertIndex);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "1-800-GOOG-411");
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, "free directory assistance");
Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);
hope it will help you
i successfully updated the existing NOTE in a contact using
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
this method doesn't work for adding a new NOTE..
How to add a new NOTE with an existing CONTACT in ANDROID?
public void testInsert()
{
ContentValues values = new ContentValues();
// insert a empty note into RawContacts.CONTENT_URI, and get the rawContactId
Uri rawContactUri = this.getContext().getContentResolver().insert(RawContacts.CONTENT_URI, values);
// get id
long rawContactId = ContentUris.parseId(rawContactUri);
// insert name data
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId); // id
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);// MIMETYPE
values.put(StructuredName.GIVEN_NAME, "zetsin");// first name
this.getContext().getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
// insert phone
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "123456");
values.put(Phone.TYPE, Phone.TYPE_MOBILE);
this.getContext().getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
// insert email
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
values.put(Email.DATA, "zetsin#gmail.com");
values.put(Email.TYPE, Email.TYPE_WORK);
this.getContext().getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
}
I am adding new contacts as follows:-
ContentValues values = new ContentValues();
String accountType = "test#gmail.com";
values.put(RawContacts.ACCOUNT_TYPE, accountType );
String accountName = "com.google";
values.put(RawContacts.ACCOUNT_NAME, accountName );
Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
//Add contact number
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "123456789");
getContentResolver().insert(Data.CONTENT_URI, values);
//Type of phone number
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.TYPE, Phone.TYPE_WORK);
getContentResolver().insert(Data.CONTENT_URI, values);
//Add contact name
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(Data.DISPLAY_NAME, "First Second");
getContentResolver().insert(Data.CONTENT_URI, values);
Query runs successfully. But the added contacts are not visible in Android Emulator Contacts. Please help. Am I missing anything here.
I have name, phone number and E-mail infomation of a contact. I just want to insert the additional email and phone for the existing contact. My questions are
How to find the contact is already existing or not?
How to insert the values on the additional or secondary address option?
Thanks in Advance.
In the official document has new contancts api.
http://developer.android.com/reference/android/provider/ContactsContract.Data.html
First, look up raw contacts id with your criteria, such as name:
final String name = "reader";
// find "reader"'s contact
String select = String.format("%s=? AND %s='%s'",
Data.DISPLAY_NAME, Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
String[] project = new String[] { Data.RAW_CONTACT_ID };
Cursor c = getContentResolver().query(
Data.CONTENT_URI, project, select, new String[] { name }, null);
long rawContactId = -1;
if(c.moveToFirst()){
rawContactId = c.getLong(c.getColumnIndex(Data.RAW_CONTACT_ID));
}
c.close();
Second, use rawContactId to add an entry to contacts:
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "1-800-GOOG-411");
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, "free directory assistance");
Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);
PS. don't forget the permissions:
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
I read about
ContactsContract.CommonDataKinds.GroupMembership, but I can't figure out what URI use to insert to.
I have prepared this method:
public static Uri addToGroup(ContentResolver resolver, long personId,
long groupId) {
ContentValues values = new ContentValues();
values.put(ContactsContract.CommonDataKinds.GroupMembership.CONTACT_ID, personId);
values.put(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, groupId);
return resolver.insert(uri, values);//URI is not known
}
Can someone tell me what URI to use in SDK 2.0+?
I found the resolution and I post it here:
public Uri addToGroup(long personId, long groupId) {
//remove if exists
this.removeFromGroup(personId, groupId);
ContentValues values = new ContentValues();
values.put(ContactsContract.CommonDataKinds.GroupMembership.RAW_CONTACT_ID,
personId);
values.put(
ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID,
groupId);
values
.put(
ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE,
ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE);
return this.ctx.getContentResolver().insert(
ContactsContract.Data.CONTENT_URI, values);
}
But I don't get something, why do I have to use RAW_CONTACT_ID and not CONTACT_ID, the later raises nullpointerexception.