Tuesday, November 6, 2012

Fetch Android Contact

How fetch contact in android ?
contact save in phone memory and also in sim memory . 
To fetch  list of contact, first add permission in android manifest file for read contact like this

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

in android all contact store in contact database for fetch need to fetch contact from database using cursor see sample code below
Cursor c = this.managedQuery(ContactsContract.Data.CONTENT_URI, null,
  Data.MIMETYPE + "=?", 
  new String[] { ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE },
  null);
while (c.moveToNext()) {
 int type = c.getInt(c.getColumnIndex(Phone.TYPE));
 if (type == Phone.TYPE_MOBILE) {
  Log.i("contact name",c.getString(c.getColumnIndex(Contacts.DISPLAY_NAME)));, 
  Log.i("contact number",c.getString(c.getColumnIndex(Phone.NUMBER)));
   }
  }
c.close();
In result,  you see all contact in logcat.

No comments:

Post a Comment