In this blog I will describe how to access Conatcts/Phonebook in your Android Application.
Contacts are stored in separate tables(in Row and Column form)
Cursor c1;
// list Columns to retive , pass null to get all the columns
String col[]={ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME};
c1 = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, col, null, null, ContactsContract.Contacts.DISPLAY_NAME);
String personName = null, number = "";
if(c1==null)
return;
// Fetch the Corresponding Phone Number of Person Name
try
{
if(c1.getCount() > 0)
{
while(c1.moveToNext())
{
String id = c1.getString(c1.getColumnIndex(Contacts._ID));
personName = c1.getString(c1.getColumnIndex(Contacts.DISPLAY_NAME));
if(id==null||personName==null)
continue;
Cursor cur = mContext.getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI, null, CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
if(cur==null)
continue;
number = "";
while(cur.moveToNext())
{
number = cur.getString(cur.getColumnIndex(CommonDataKinds.Phone.NUMBER));
}
cur.close();
if(!number.equals(""))
{
number=removeUnnecessaryCharacters(number);
numberList.add(number);
nameList.add(personName);
}
}
}
}
catch(Exception e)
{
}
finally
{
c1.close();
}
Contacts are stored in separate tables(in Row and Column form)
ContactsContract
ContactsContract defines an extensible database of contact-related
information. Contact information is stored in a three-tier data model:
-
A row in the
ContactsContract.Data
table can store any kind of personal data, such as a phone number or email addresses. The set of data kinds that can be stored in this table is open-ended. There is a predefined set of common kinds, but any application can add its own data kinds. -
A row in the
ContactsContract.RawContacts
table represents a set of data describing a person and associated with a single account (for example, one of the user's Gmail accounts). -
A row in the
ContactsContract.Contacts
table represents an aggregate of one or more RawContacts presumably describing the same person. When data in or associated with the RawContacts table is changed, the affected aggregate contacts are updated as necessary.
Cursor c1;
// list Columns to retive , pass null to get all the columns
String col[]={ContactsContract.Contacts._ID,ContactsContract.Contacts.DISPLAY_NAME};
c1 = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, col, null, null, ContactsContract.Contacts.DISPLAY_NAME);
String personName = null, number = "";
if(c1==null)
return;
// Fetch the Corresponding Phone Number of Person Name
try
{
if(c1.getCount() > 0)
{
while(c1.moveToNext())
{
String id = c1.getString(c1.getColumnIndex(Contacts._ID));
personName = c1.getString(c1.getColumnIndex(Contacts.DISPLAY_NAME));
if(id==null||personName==null)
continue;
Cursor cur = mContext.getContentResolver().query(CommonDataKinds.Phone.CONTENT_URI, null, CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null);
if(cur==null)
continue;
number = "";
while(cur.moveToNext())
{
number = cur.getString(cur.getColumnIndex(CommonDataKinds.Phone.NUMBER));
}
cur.close();
if(!number.equals(""))
{
number=removeUnnecessaryCharacters(number);
numberList.add(number);
nameList.add(personName);
}
}
}
}
catch(Exception e)
{
}
finally
{
c1.close();
}
Hi, I wish for tο subscribe for this blog to obtaіn neωеst updates, thuѕ where can i do іt plеaѕe help out.
ReplyDeleteHeгe іs my blοg :: Hämorrhoiden
Woω! Finally I got a blog fгom whеre ӏ
ReplyDeletebe capable of trulу оbtаin hеlpful data conceгning my study and knowledge.
Looκ at my websitе - gochatroulettego.de
Generally I do not learn post on blogs, but
ReplyDeleteI wish to saу that this writе-up verу forсed me to chеck
out аnԁ ԁo it! Your writing style has been amazeԁ me.
Thаnk yοu, verу nice article.
Also visіt my web-sіte: http://cellulitee.de/
Wow, amazіng blоg layout! How long
ReplyDeletehave yοu been bloggіng for? you made blogging look easy.
The overall look of yоur web site is wonderful, let alоne the сontent!
Fеel free tο visit my web page: les hémorroïdeѕ (Hiredream.com)
Its like you read my mind! You seem to know so much about this, such as you wrote the e-book in it or something.
ReplyDeleteI think that you could do with a few percent to force
the message house a little bit, but instead of that, that is great blog.
An excellent read. I will definitely be back.
An interesting discussion is worth comment. I do believe that you ought
ReplyDeleteto write more about this topic, it may not be a taboo subject but generally
folks don't speak about such issues. To the next!
Best wishes!!
I like the valuable info you provide in your articles.
ReplyDeleteI will bookmark your blog and check again here frequently.
I'm quite sure I will learn many new stuff right here!
Good luck for the next!
Thank you for the auspicious writeup. It in fact was a amusement account it.
ReplyDeleteLook advanced to far added agreeable from you!
However, how can we communicate?
I think that is among the most significant information for me.
ReplyDeleteAnd i am satisfied reading your article. But want to
remark on some basic things, The website style is ideal, the articles is in point of fact nice : D.
Good task, cheers
Hi there, I enjoy reading all of your post.
ReplyDeleteI like to write a little comment to support you.
Amazing! Its truly amazing article, I have got much clear idea concerning from this post.
ReplyDelete