Android 2.0 provides new Contact API, which is defined in android.provider.ContactsContract. After some try on contact api I found a better and easy solution for insertion and modification of contact.
Insert contact -
Intent addContactIntent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
addContactIntent.putExtra(ContactsContract.Intents.Insert.NAME,
"xyz");
addContactIntent.putExtra(ContactsContract.Intents.Insert.EMAIL,
"abc@gmail.com");
addContactIntent.putExtra(ContactsContract.Intents.Insert.PHONE,"012345678");startActivity(addContactIntent);
ACTION_INSERT will start .EditContact activity.
Edit contacts -
- If _ID of contact is known -
Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setData(ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, _ID));
startActivity(intent);
- If _ID of contact is not known -
i) Find out conatct _ID by phone number .
// CONTENT_FILTER_URI allow to search contact by phone number
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(getPhone()));
// This query will return NAME and ID of conatct, associated with phone //number.
Cursor mcursor = getContentResolver().query(lookupUri,new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup._ID },null, null, null);
//Now retrive _ID from query result
long idPhone = 0;try {
if (mcursor != null) {
if (mcursor.moveToFirst()) {
idPhone = Long.valueOf(mcursor.getString(mcursor .getColumnIndex(PhoneLookup._ID)));
Log.d("", "Contact id::" + idPhone);
}
}
} finally {
mcursor.close();
}
ii) Then Edit contact
if (idPhone > 0) {Intent intent = new Intent(Intent.ACTION_EDIT);
intent.setData(ContentUris.withAppendedId( ContactsContract.Contacts.CONTENT_URI, idPhone));
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "contact not in list",
Toast.LENGTH_SHORT).show();
}
You can also search _ID by partial name, for this instead of PhoneLookup.CONTENT_FILTER_URI use ContactsContract.Contacts.CONTENT_FILTER_URI
To work with contact, you have to define special permission in AndroidManifest.xml .
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
December 11th, 2011 on 2:17 PM
I thought it was going to be some ok old blog post, but frankly it compensated for my time. I will post a link to this page on my web site. I am sure my site visitors will find that very handy.
January 3rd, 2012 on 4:13 PM
i think this code can be helpful to developers but never mind this is a great post.
February 13th, 2012 on 6:59 PM
It is really very helpful for developers..thanks for sharing..
But I want to insert website also..but thr is not constant for website as it is for email like,
ContactsContract.Intents.Insert.EMAIL.
So how can I insert website using this method?..
thanks in advance….
February 14th, 2012 on 3:43 PM
Hi Chirag,
There is no constant provided by android to insert website. But still you can do this. After inserting contact you can save website for that contact. Try this code –
ContentValues row = new ContentValues();row.put(
android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID,
Integer.valueOf(idPhone));
row.put(Data.MIMETYPE, Website.CONTENT_ITEM_TYPE);
row.put(Website.URL, "www.techjini.com");
row.put(Website.TYPE, Website.TYPE_WORK);
getContentResolver().insert(ContactsContract.Data.CONTENT_URI, row);
idPhone is _ID of contact. So you can do this in onActivityResult(). Where you will get an intent, which will be having _ID of contact.