Breaking News


Sunday 26 January 2014

How to Access Contacts In Android

In this blog  I  will describe how to access or read Conatcts/Phonebook in your Android Application.

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. 
find more  Information here

Here variable  personName will contain the name and number will contain the phone number of the Contact

Code to read/access the contacts


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();
                                   

                                }
                        }
                }
                catch(Exception e)
                {
                    
                }
                finally
                {
                        c1.close();
                }
Share This
Blogger
Facebook
Disqus

comments powered by Disqus

No comments :

Post a Comment

Subscribe
Labels
Popular Posts

Subscribe Via Email

About Us

THIS IS ANDROID AND JAVA FREE TUTORIAL AND ALL PROVIDE SOLUTION IN ANDROID AND JAVA INTERVIEW QUE AND ANS IS AVAILABLE IN MY BLOG AND ANY QUERY PLEASE CONTACT ME GO NOW CONTACT US PAGE.

Total Pageviews

© Android and Java solution All rights reserved | Designed By Fireandroids