• Have Any Question?
  • contact@eitworld.com

Reading Phone Contacts

    • The query used for reading the content is as following-
getContentResolver().query(uri, projection, selection, selectionArgs, sortOrder)


    • The first parameter is the uri for the action we are doing. Other parameters are for filtering the selection of the contacts. For now, we will keep all of them to null. The only thing that is mandatory is the uri. The above statement returns a Cursor.
    • First of all, import the following packages-
import android.database.Cursor;

import android.net.Uri;

import android.provider.ContactsContract.CommonDataKinds.Phone;

import android.provider.ContactsContract.Contacts;


    • The URI for reading phone contacts will be obtained by Phone.CONTENT_URI. So use it in the query() and save the result of query in a cursor.


Cursor cur=getContentResolver().query(Phone.CONTENT_URI,null, null, null, null);


    • Now the cursor has all information of all phone contacts. Start a while loop with moving the cursor to the next position. The loop will be ended when there will be no item left in the cursor.



    • For retrieving the information from the cursor, use getColumnIndex() method of the class Cursor. Cursor is a kind of table that has different contacts in different rows and their different information in different columns.
    • String name=cur.getString(cur.getColumnIndex(Phone.DISPLAY_NAME));


    • Above statement will retrieve the name of the content at which the cur is pointing at that time and store it in a String variable name.
    • Similarly Phone number can be accessed using-
String number=cur.getString(cur.getColumnIndex(Phone.NUMBER));


    • Thus the code becomes-
Cursor cur=getContentResolver().query(Phone.CONTENT_URI,null, null, null, null);


String name=cur.getString(cur.getColumnIndex(Phone.DISPLAY_NAME));

String number=cur.getString(cur.getColumnIndex(Phone.NUMBER));}


    • In this way different information can be retrieved. Now you can use this information as you want.
    • Give the following permission in AndroidManifest.xml-
<uses-permission android:name=”android.permission.READ_CONTACTS” />


Real Time Web Analytics