Android how to add phone number to existing contact [duplicate] - android

Im trying to add a phone number to an already existing contact on a Droid-phone. Doing it at the same time as I create a contact is trivial, as the backreference I supply simply is 0 when creating a ContentProviderOperation. But trying to find the backreference through querying for a display name like this does not work:
Cursor rawContactsReferenceCursor =
contentResolver.query(Data.CONTENT_URI,
new String[]{Data.RAW_CONTACT_ID},
Data.DISPLAY_NAME+"=\""+displayName+"\"", null, null);
While I do get a raw contact ID, the following code just generates an IndexOutOfBoundException (rawConcactReferenceID is the variable I got from the previous query):
ArrayList<ContentProviderOperation> op_list =
new ArrayList<ContentProviderOperation>();
op_list.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawConcactReferenceID)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, testNumber)
.withValue(Phone.TYPE, Phone.TYPE_CUSTOM)
.withValue(Phone.LABEL, testLabel)
.build());
ContentProviderResult[] result =
contentResolver.applyBatch(ContactsContract.AUTHORITY, op_list);
The big challenge is a huge lack of good documentation. I would be very satisfied to just get my hands on some working copypasta to study.
Cheers,

I found an answer. It is not atomic if you want to add several things right away, but hey, who needs stupid atomicity?
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, new Integer(contactId).intValue());
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, dataValue);
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, customLabel);
Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);

I've had a similar problem with email addresses. Here's the solution I used that worked:
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValue(Data.RAW_CONTACT_ID, id)
.withValue(Email.DATA, value)
.withValue(Email.MIMETYPE, .Email.CONTENT_ITEM_TYPE)
.withValue(Email.LABEL, label)
.withValue(Email.TYPE, Email.TYPE_CUSTOM)
.build());
ContentProviderResult[] res = cr.applyBatch(ContactsContract.AUTHORITY, ops);
The same solution should work for telephone numbers.

These links may provide some help:
http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/1/
http://developer.android.com/resources/samples/ContactManager/src/com/example/android/contactmanager/ContactAdder.html

Related

After adding contacts from code, phoneBook appends the space after period(.) in android

I don't know what I am doing wrong while inserting contact into phonebook.
here is the code to insert.
public static void AddMultipleContact(Context context, String name , String numbers, String Data4){
ContentResolver resolver = context.getContentResolver();
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(ContactsContract.RawContacts.CONTENT_URI, true))
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, AccountGeneral.ACCOUNT_NAME)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, AccountGeneral.ACCOUNT_TYPE)
.withValue(ContactsContract.RawContacts.AGGREGATION_MODE, ContactsContract.RawContacts.AGGREGATION_MODE_DEFAULT)
.withValue(ContactsContract.RawContacts.SOURCE_ID, sourceId)
.build());
ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(ContactsContract.Data.CONTENT_URI, true))
.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, numbers) // Number of the person
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
.build()); // Type of mobile number
android.util.Log.e(TAG, "AddMultipleContact:-------------- NAME = " + name);
ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(ContactsContract.Data.CONTENT_URI, true))
.withValueBackReference(ContactsContract.RawContacts.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
.build());
ops.add(ContentProviderOperation.newInsert(addCallerIsSyncAdapterParameter(ContactsContract.Data.CONTENT_URI, true))
.withValueBackReference(ContactsContract.RawContacts.Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(ContactsContract.RawContacts.Data.MIMETYPE,MIMETYPE)
.withValue(Data.DATA1, sourceId)
.withValue(Data.DATA3, Data4)
.build());
try {
ContentProviderResult[] results = resolver.applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (Exception e) {
e.printStackTrace();
}
}
As you can see i am also printing log for name.
android.util.Log.e(TAG, "AddMultipleContact:-------------- NAME = " + name);
Here i am sending contactName and contactNumber to this method.
the name what I am passing is suppose ex: "A.Bcdef"
but the name what I am seeing in the phonebook is "A. Bcdef"
It is adding extra space after period(.).
Please help me. I am not getting any solution on google, or not able to search exactly what I want to search.
Thanks in advance.
I SOLVED THIS BY Changing this line
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
to
.withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, name)
GIVEN_NAME is taking the same name what we passed as value
There are other parameters like
DISPLAY_NAME, GIVEN_NAME, FAMILY_NAME, PREFIX, MIDDLE_NAME, SUFFIX, PHONETIC_GIVEN_NAME, PHONETIC_MIDDLE_NAME, PHONETIC_FAMILY_NAME, FULL_NAME_STYLE, PHONETIC_NAME_STYLE
I really don't know what actually these all means.
But when i added name as 'Testing,123' while creating a contact, then the name appears to be like '123 Testing'.
So I thought it is automatically thinking like what is 'middleName' and 'lastName' based on SpecialCharacters.

How to Add a contact with Date of Birth into device contacts programmatically?

I am using the following code for inserting a contact, but i am unable to find the Date_Of_Birth field to add it.
ContentValues values = new ContentValues();
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "0123456789");
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, "Ravi");
Uri dataUri = getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactID)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Event.START_DATE, "26-05-2015")
.withValue(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
.build());
As per my knowledge, we can add the birthday in the above format but only if the device supports. As lot of devices do not have birthday column in default device contacts. When you create a contact programatically its created on device, where the birthday field is not available. it later on gets synced with the google account and the birthday field gets visible.
I am posting this as it may be useful for someone at sometime.
values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY);
values.put(ContactsContract.CommonDataKinds.Event.START_DATE, birthdayStartDate);
Uri dataUri = getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
I think you need: ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY
But be careful: some OEMs do provide their own ContactProvider...

Android write contacts [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to add new contacts in android
how can i add a name to the contacts?
I have two spinners, one for the firstname and one for the lastname. The assigned variables are linkname1 and linkname2. When user pushes the OK button (these are all in a dialog), the name (linkname1 + " " + linkname2) should be added to the contacts. I can read the contacts but how do i write it?
Thanks
Update:
I also tried this:
newname = linkname1 + " " + linkname2;
ContentValues values = new ContentValues();
vales.put(ContactsContract.Contacts.DISPLAY_NAME, newname);
and this:
StringBuffer strBuf = new StringBuffer();
strBuf.append(linkname1);
strBuf.append(" ");
strBuf.append(linkname2);
ContentValues values = new ContentValues();
values.put(ContactsContract.Contacts.DISPLAY_NAME, strBuf.toString());
But the new name is not appearing in the contact list.
That content provider although will still work was replaced with Contacts Contracts in 2.2. The following code will work with new contacts contracts providers:
ArrayList<ContentProviderOperation> ops =
new ArrayList<ContentProviderOperation>();
...
int rawContactInsertIndex = ops.size();
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, accountType)
.withValue(RawContacts.ACCOUNT_NAME, accountName)
.build());
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.GIVEN_NAME, linkname1)
.withValue(StructuredName.FAMILY_NAME, linkname2)
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
The Account Name and Type you will have to query from the AccountManager service. Either by choosing the an account (e.g. Local or Google) or prompting the user.
Android how-to (create a new contact):
http://www.lacherstorfer.at/haris_blog/2008/03/android-howto-create-a-new-con.html
Be sure to look at the code edit in the comments, may help fix depending on version.

New contacts created using ContactsContract do not appear in Contacts app

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!
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.
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());
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".
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.

Add number to contact on Android 2.0

Im trying to add a phone number to an already existing contact on a Droid-phone. Doing it at the same time as I create a contact is trivial, as the backreference I supply simply is 0 when creating a ContentProviderOperation. But trying to find the backreference through querying for a display name like this does not work:
Cursor rawContactsReferenceCursor =
contentResolver.query(Data.CONTENT_URI,
new String[]{Data.RAW_CONTACT_ID},
Data.DISPLAY_NAME+"=\""+displayName+"\"", null, null);
While I do get a raw contact ID, the following code just generates an IndexOutOfBoundException (rawConcactReferenceID is the variable I got from the previous query):
ArrayList<ContentProviderOperation> op_list =
new ArrayList<ContentProviderOperation>();
op_list.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, rawConcactReferenceID)
.withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
.withValue(Phone.NUMBER, testNumber)
.withValue(Phone.TYPE, Phone.TYPE_CUSTOM)
.withValue(Phone.LABEL, testLabel)
.build());
ContentProviderResult[] result =
contentResolver.applyBatch(ContactsContract.AUTHORITY, op_list);
The big challenge is a huge lack of good documentation. I would be very satisfied to just get my hands on some working copypasta to study.
Cheers,
I found an answer. It is not atomic if you want to add several things right away, but hey, who needs stupid atomicity?
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, new Integer(contactId).intValue());
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, dataValue);
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, customLabel);
Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);
I've had a similar problem with email addresses. Here's the solution I used that worked:
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValue(Data.RAW_CONTACT_ID, id)
.withValue(Email.DATA, value)
.withValue(Email.MIMETYPE, .Email.CONTENT_ITEM_TYPE)
.withValue(Email.LABEL, label)
.withValue(Email.TYPE, Email.TYPE_CUSTOM)
.build());
ContentProviderResult[] res = cr.applyBatch(ContactsContract.AUTHORITY, ops);
The same solution should work for telephone numbers.
These links may provide some help:
http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/1/
http://developer.android.com/resources/samples/ContactManager/src/com/example/android/contactmanager/ContactAdder.html

Categories

Resources