Android Read Contact and Display in ListView

How to display Contact No. in Android ?

You can get contact using ContactsContract.And its provide bridge between contact provider and your applications.That contains URIs  and columns to get contact with information.Such as name,street address,city,various type of phone number,email address,postal address,notes,organization.


While retrieving contact, Don't forgot to add uses-permission

<uses-permission android:name="android.permission.READ_CONTACTS" />

you can retrieve all contacts as a cursor.

Cursor cursor = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);

now we have cusror with contacts and get diffrent value from cusror.

while (cursor.moveToNext()) {
String name =cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));


String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}


Above code,get all contact name and phone numbers from your phone and sim.



Get only SimCard's Contact ::

Cursor cursor = mContentResolver.query(RawContacts.CONTENT_URI,
   new String[]{RawContacts._ID,RawContacts.ACCOUNT_TYPE},
   RawContacts.ACCOUNT_TYPE + " <> 'com.anddroid.contacts.sim' "
    + " AND " + RawContacts.ACCOUNT_TYPE + " <> 'com.google' ",null,null);

we can get diffrent types of phone number from contacts.Like,Home,Mobile,Work.So first you need to get you contact id from cursor.using contact id you can get cursor of phone number.

Get diffrent type of phone number

String contact_Id = cursor.getString(cursor .getColumnIndex(ContactsContract.Contacts._ID));
Cursor cursor_phone = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = " + contact_Id, null, null);
while (cursor_phone.moveToNext()) {
String phNumber = cursor_phone.getString(cursor_phone
.getColumnIndex(Phone.NUMBER));
System.out.println(phNumber);
int PHONE_TYPE =cursor_phone.getInt(cursor_phone.getColumnIndex(Phone.TYPE));

switch (PHONE_TYPE) {
case Phone.TYPE_HOME:
// home number
break;
case Phone.TYPE_MOBILE:
// mobile number
break;
case Phone.TYPE_WORK:
// work(office) number
break;
}
}



Get Address From your ContactId


while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
            Cursor address_cursror = getContentResolver().query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,null,
                    ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID+ " = ?",
                    new String[] { id },null);
            while(address_cursror.moveToNext()) {
                String street = address_cursror.getString(address_cursror.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
                System.out.println("Street ::" +street);
                String city = address_cursror.getString(address_cursror.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
                System.out.println("City ::"+city);
                String state = address_cursror.getString(address_cursror.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
                System.out.println("State ::"+state);
                String postalCode = address_cursror.getString(address_cursror.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
                System.out.println("Postal Code ::"+postalCode);
            } 
            address_cursror.close();



How to call in android ?

when you want to call any number from application you have to add CALL_PHONE permission.

<uses-permission android:name="android.permission.CALL_PHONE" />
 

using Intent.ACTION_CALL you can call in android.

String phoneNumber = "tel:" + phoneNo;
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(phoneNumber));
startActivity(intent);

DownLoad Full Source Code From Here..
       Download Source Code