This code is working fine for fetching name and phone but i don't know how to get email from this code. Here is my code :
public static ArrayList<ContentValues> getContactDetails(final Context mContext){
ArrayList<ContentValues> contactList = new ArrayList<ContentValues>();
String order = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC";
Cursor managedCursor = mContext.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, order);
int _number = managedCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int _name = managedCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int _id = managedCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);
while (managedCursor.moveToNext()) {
ContentValues values = new ContentValues();
Contact mContact = new Contact();
values.put(ContactClass.CONTACT_NAME, managedCursor.getString(_name));
values.put(ContactClass.CONTACT_MOBILE_NUMBER, managedCursor.getString(_number).replaceAll("\\s+",""));
mContact.setPhNo(managedCursor.getString(_number).replaceAll("\\s+",""));
mContact.setName(managedCursor.getString(_name));
contactList.add(values);
serverContactList.add(mContact);
}
}
return contactList;
}
here i want to get email and add to serverContactList list.
I edited your code, add rest of your code in 2 todos.
public static ArrayList<ContentValues> getContactDetails(final Context mContext) {
// todo rest of things
int _id = managedCursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);
while (managedCursor.moveToNext()) {
// we will get emails for a contact id
String id = managedCursor.getString(_id);
Cursor cur1 = mContext.getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
if (cur1 != null) {
while (cur1.moveToNext()) {
//to get the contact names
String name = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.e("Name :", name);
String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.e("Email", email);
}
}
cur1.close();
// todo rest of things
}
return contactList;
}
Don't use managedCursor, ever.
Query over Data.CONTENT_URI instead of Phone.CONTENT_URI to get access to all Data items (including Phone and Email and others if needed)
Limit the query by MIMETYPE to just the items you need
Map<Long, Contact> contacts = new HashMap<>();
String[] projection = {Data.CONTACT_ID, Data.DISPLAY_NAME, Data.MIMETYPE, Data.DATA1, Data.DATA2, Data.DATA3};
// query only emails/phones/events
String selection = Data.MIMETYPE + " IN ('" + Phone.CONTENT_ITEM_TYPE + "', '" + Email.CONTENT_ITEM_TYPE + "')";
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(Data.CONTENT_URI, projection, selection, null, null);
while (cur != null && cur.moveToNext()) {
long id = cur.getLong(0);
String name = cur.getString(1); // full name
String mime = cur.getString(2); // type of data (phone / email)
String data = cur.getString(3); // the actual info, e.g. +1-212-555-1234
Log.d(TAG, "got " + id + ", " + name + " - " + data);
// add info to existing list if this contact-id was already found, or create a new list in case it's new
Contact contact;
if (contacts.containsKey(id)) {
contact = contacts.get(id);
} else {
contact = new Contact();
contacts.put(id, contact);
contact.setName(name);
}
if (mime == Phone.CONTENT_ITEM_TYPE) {
contact.setPhNo(data);
} else {
contact.setEmail(data);
}
}
Related
I want to fetch only the first contact number from a list of contact numbers of a particular user, with the help of cursor. Here is my code:
private ArrayList<ArrayList<String>> getAllContacts() {
ArrayList<ArrayList<String>> nameList = new ArrayList<ArrayList<String>>();
ArrayList<String> person=new ArrayList<>();
ArrayList<String> number=new ArrayList<>();
ArrayList<String> temp=new ArrayList<>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if ((cur!=null ? cur.getCount() : 0) > 0) {
while (cur!=null && cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
person.add(name);
if (cur.getInt(cur.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
if(pCur.getCount()==1) {
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
number.add(phoneNo);
}
}
else{
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
temp.add(phoneNo);
}
number.add(temp.get(0));
temp.clear();
}
pCur.close();
}
}
}
if (cur!=null) {
cur.close();
}
Log.d("contacts",String.valueOf(number.size())+" "+String.valueOf(person.size())); //the lists aren't of equal size
if(person.size()==number.size()){
nameList.add(person);
nameList.add(number);
}
else{
//don't know what to do here
}
return nameList;
}
But, the code still fetches multiple contact numbers saved for a single user, in other words person.size() is not equal to number.size(). What do I do?
The array sizes are not the same because not all contacts have phone-numbers, you are correctly checking for HAS_PHONE_NUMBER and only if true, getting that contact's phone numbers - which means number.size() would be < person.size() on most phones.
I would suggest instead of keeping separate Arrays for names and phones, having a single Array with a simple java class that represents a person.
Other than that, your code is very inefficient, as you're doing a ton of queries, while you can do just one.
Here's an example with the two suggestions above:
class Person {
long id,
String name,
String firstPhone;
public Person(id, name, firstPhone) {
this.id = id;
this.name = name;
this.firstPhone = firstPhone;
}
}
Map<Long, Person> mapping = new HashMap<>(); // mapping between a contact-id to a Person object
String[] projection = {Data.CONTACT_ID, Data.DISPLAY_NAME, Phone.NUMBER};
// query phones only
String selection = Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'";
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(Data.CONTENT_URI, projection, selection, null, null);
while (cur != null && cur.moveToNext()) {
long id = cur.getLong(0);
String name = cur.getString(1); // full name
String phone = cur.getString(2); // phone
Log.d(TAG, "got " + id + ", " + name + " - " + data);
// only add a new object if we haven't seen this person before
if (!mapping.containsKey(id)) {
Person person = new Person(id, name, phone);
mapping.put(id, person);
}
}
cur.close();
Array<Person> people = mapping.values();
EDIT
Set<Long> ids = new HashSet<Long>();
Array<String> names = new ArrayList<String>();
Array<String> numbers = new ArrayList<String>();
String[] projection = {Data.CONTACT_ID, Data.DISPLAY_NAME, Phone.NUMBER};
// query phones only
String selection = Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'";
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(Data.CONTENT_URI, projection, selection, null, null);
while (cur != null && cur.moveToNext()) {
long id = cur.getLong(0);
String name = cur.getString(1); // full name
String phone = cur.getString(2); // phone
Log.d(TAG, "got " + id + ", " + name + " - " + data);
// only add a new object if we haven't seen this person before
if (ids.add(id)) {
names.add(name);
numbers.add(phone);
}
}
cur.close();
how can I get the email address correctly? Here's what I've done so far.
#android.webkit.JavascriptInterface
public void chooseContact(){
Intent pickContactIntent = new Intent(Intent.ACTION_PICK);
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(pickContactIntent, CONTACT_PICKER_RESULT);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == CONTACT_PICKER_RESULT){
Uri contactUri = data.getData();
// Perform the query.
// We don't need a selection or sort order (there's only one result for the given URI)
Cursor cursor = getContentResolver().query(contactUri, null, null, null, null);
cursor.moveToFirst();
// Retrieve the phone number from the NUMBER column.
int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String phoneNumber = cursor.getString(column);
if(phoneNumber != null)
phoneNumber = phoneNumber.trim();
if(phoneNumber == null || phoneNumber.equals("")) {
// This should never happen, but just in case we'll handle it
Log.e(TAG, "Phone number was null!");
Util.debugAsserts(false);
return;
}
// Retrieve the contact name.
column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Identity.DISPLAY_NAME);
String name = cursor.getString(column);
// Retrieve the contact email address.
column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
String email = cursor.getString(column);
cursor.close();
JSONObject contacts = new JSONObject();
String jsonString;
try {
contacts.put("name" , name);
contacts.put("email" , email);
contacts.put("phoneNumber" , phoneNumber);
jsonString = contacts.toString();
String encodedData = Base64.encodeToString(jsonString.getBytes(), Base64.DEFAULT);
String command = String.format("get_contact_details('%s');", encodedData);
eaWebView.submitJavascript(command);
} catch (JSONException e) {
throw new Error(e);
}
}
}
The above code get the name and phone number correctly but the email returns as phone number not the email address from the contact.
Thank you in advance!
The problem is that you're using a Phone-picker in your code, by setting setType(Phone.CONTENT_TYPE) you're telling the contacts app you're only interested in a phone number.
You need to switch to a Contact-picker, like so:
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
Then you read the result like this:
Uri contactData = data.getData();
// get contact-ID and name
String[] projection = new String[] { Contacts._ID, Contacts.DISPLAY_NAME };
Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null);
cursor.moveToFirst();
long id = cursor.getLong(0);
String name = cursor.getString(1);
Log.i("Picker", "got a contact: " + id + " - " + name);
cursor.close();
// get email and phone using the contact-ID
String[] projection2 = new String[] { Data.MIMETYPE, Data.DATA1 };
String selection2 = Data.CONTACT_ID + "=" + id + " AND " + Data.MIMETYPE + " IN ('" + Phone.CONTENT_ITEM_TYPE + "', '" + Email.CONTENT_ITEM_TYPE + "')";
Cursor cursor2 = getContentResolver().query(Data.CONTENT_URI, projection2, selection2, null, null);
while (cursor2 != null && cursor2.moveToNext()) {
String mimetype = cursor2.getString(0);
String data = cursor2.getString(1);
if (mimetype.equals(Phone.CONTENT_ITEM_TYPE)) {
Log.i("Picker", "got a phone: " + data);
} else {
Log.i("Picker", "got an email: " + data);
}
}
cursor2.close();
You Can Use Below Method To Fetch Contact Number, Email and Contact Name
public static void getContactDetails(Context context){
ArrayList<String> names = new ArrayList<String>();
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (cur1.moveToNext()) {
String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.e("Contact Details","\n Name is :"+name+" Email is: "+ email+ " Number is: "+number);
if(email!=null){
names.add(name);
}
}
cur1.close();
}
}
See Below Image For Output
I want to fetch id,name,birthday,addess from android contacts. I changed the uri (ContactsContract.CommonDataKinds, ContactsContract.Contacts etc.) and was able to fetch the address, but then the birthday disappeared. Anyway I tried, I can not find a solution to fetch both at once.
public void getBirthdays() {
// get data from contacts
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[]{
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.START_DATE,
ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS};
String selection =
ContactsContract.Data.MIMETYPE + "= ? AND " +
ContactsContract.CommonDataKinds.Event.TYPE + "=" + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
String[] selectionArgs = new String[]{ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE};
Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs, null);
int columnCount = cursor.getColumnCount();
cursor.moveToFirst();
for (int i = 0; i < columnCount; i++) {
Common.echo(cursor.getColumnName(i) + ", " + cursor.getString(i));
}
cursor.close();
}
the output of the above code is:
I: contact_id, 14046
I: display_name, Magret XXXX
I: data1, XXXX-05-31
I: data1, XXXX-05-31
The "data1"-field is listed twice, because START_DATE (birthday) and FORMATTED_ADDRESS are both stored in same virtual column "data1".
Is there a way to define a virtual column-names in a resolver like the SQL-equivalent "SELECT col1 AS name1, col2 AS name2, col3,col4 WHERE..."?
two issues: you're limiting your query to EVENTs only & you're only logging the first row found in the query.
Try this:
private class Contact {
public Long contactId;
public String name;
public String birthday;
public String address;
}
public void getBirthdaysAndAddress() {
// mapping from contactId to a contact object
Map<Long, Contact> contacts = new HashMap<>();
Uri uri = Data.CONTENT_URI;
String[] projection = new String[]{
Data.CONTACT_ID,
Data.DISPLAY_NAME,
Data.MIMETYPE,
Data.DATA1,
Event.TYPE};
// get ALL rows that represent either a birthday or a postal address
String selection = Data.MIMETYPE + "=? OR " + Data.MIMETYPE + "=?";
String[] selectionArgs = new String[]{Event.CONTENT_ITEM_TYPE, StructuredPostal.CONTENT_ITEM_TYPE};
Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs, null);
while (cursor.moveToNext()) {
Long id = cursor.getLong(0);
// get an existing contact object, or create a new one
Contact contact = contacts.get(id);
if (contact == null) {
contact = new Contact();
contact.contactId = id;
contact.name = cursor.getString(1);
contacts.put(id, contact);
}
String mimetype = cursor.getString(2);
if (mimetype.equals(Event.CONTENT_ITEM_TYPE)) {
// this is an event row, check that it is a birthday
int type = cursor.getInt(4);
if (type == Event.TYPE_BIRTHDAY) {
contact.birthday = cursor.getString(3);
}
} else {
// this is an address row
contact.address = cursor.getString(3);
}
}
cursor.close();
// print all contact objects containing a birthday
Iterator it = mp.values().iterator();
while (it.hasNext()) {
Contact contact = (Contact) it.next();
if (!TextUtils.isEmpty(contact.birthday)) {
Log.i("Birthday contacts", contact.contactId + " - " + contact.name + " - " + contact.birthday + " - " + contact.address);
}
}
}
I am trying to fetch contacts from phone by MIME-TYPE, so i can select contacts that has the type:
ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE
Here is the method i use:
public static ArrayList<Contact> fetchContactsFromPhone(#NonNull Context context) {
ArrayList<Contact> contacts = new ArrayList<>();
Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
String _ID = ContactsContract.Contacts._ID;
String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null, null);
if (cursor != null && cursor.getCount() > 0) {
while (cursor.moveToNext()) {
String contact_id = cursor.getString(cursor.getColumnIndex(_ID));
String name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME));
long hasPhoneNumber = Long.parseLong(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER)));
if (hasPhoneNumber > 0) {
Cursor phoneCursor = contentResolver.query(
PhoneCONTENT_URI,
null,
Phone_CONTACT_ID + " = " + contact_id + " AND " + ContactsContract.Data.MIMETYPE + " = " + "'"+ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE+"'"
, null, null);
if (phoneCursor != null) {
while (phoneCursor.moveToNext()) {
Contact contact = new Contact();
String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
phoneNumber = phoneNumber.replaceAll("[()\\-\\s]", "").trim();
contact.setName(name);
contact.setPhoneNum(phoneNumber);
contacts.add(contact);
}
phoneCursor.close();
}
}
}
cursor.close();
}
//return data
return contacts;
}
The problem is this query, return ZERO contacts.
Any Idea why?
You're querying the table CommonDataKinds.Phone.CONTENT_URI which contains only Data rows with MIMETYPE = CommonDataKinds.Phone.CONTENT_ITEM_TYPE.
But you're asking for rows with MIMETYPE CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE so you're getting an empty cursor.
It's an easy fix to your existing code, but even when fixed, you're code is very slow, it'll run a query per contact on the device, this can easily be hundreds or even thousands of queries. If you just need to get the name+number of all contacts, try the following code (one query to get all):
ArrayList<Contact> contacts = new ArrayList<>();
// Make sure you import Phone from ContactsContract.CommonDataKinds.Phone
String[] projection = { Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER };
Cursor cur = cr.query(Phone.CONTENT_URI, projection, null, null, null);
while (cur != null && cur.moveToNext()) {
long id = cur.getLong(0);
String name = cur.getString(1);
String number = cur.getString(2);
Log.d(TAG, "got " + id + ", " + name + ", " + number);
Contact contact = new Contact();
contact.setName(name);
contact.setPhoneNum(number);
contacts.add(contact);
}
if (cur != null) {
cur.close();
}
Note that similar to your code, this code might create multiple Contact objects for each contact, in case a contact has more then one phone.
If you want only one Contact object per contact, you need to modify your Contact object to contain a list of phones, and change the ArrayList<Contact> to HashMap<Long, Contact> so you can add a phone to an existing object instead of creating a new one.
Hello i am working on a contact directory,i have made a custom call logs,Noe i want to display the contact details when one of the contact is clicked,I have first fetched contact_id from the contact number,and then by searching over the internet i got a function to get all details from contact id,I have put it in my code,But its not working,not giving me results,
public void getDetials(String contactID) {
Uri myPhoneUri = Uri.withAppendedPath(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
Uri.encode(contactID));
// Query the table
#SuppressWarnings("deprecation")
Cursor phoneCursor = managedQuery(myPhoneUri, null, null, null, null);
// Get the phone numbers from the contact
for (phoneCursor.moveToFirst(); !phoneCursor.isAfterLast(); phoneCursor
.moveToNext()) {
// Get a phone number
String phoneNumber = phoneCursor
.getString(phoneCursor
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
//get name
String phonename = phoneCursor
.getString(phoneCursor
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
//get email
String phoneemail = phoneCursor
.getString(phoneCursor
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.DATA));
//get image uri..!!
String img_uri = phoneCursor
.getString(phoneCursor
.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
System.out
.println("=============my phone number ,is==================="
+ phoneNumber + "======in call info=="+"\n =========name is===="+phonename+"=================email is========"+phoneemail);
System.out.println("======myimgae url fopr the contact is============="+img_uri);
}
}
Here is method to read all phone numbers and emails for specified contact:
public void getContactDetails(int contactId) {
Log.d("Details", "---");
Log.d("Details", "Contact : " + contactId);
final Cursor phoneCursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[] {
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.PHOTO_URI,
},
Data.CONTACT_ID + "=?",
new String[] {String.valueOf(contactId)}, null);
try {
final int idxAvatarUri = phoneCursor.getColumnIndexOrThrow(
ContactsContract.CommonDataKinds.Phone.PHOTO_URI);
final int idxName = phoneCursor.getColumnIndexOrThrow(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
final int idxPhone = phoneCursor.getColumnIndexOrThrow(
ContactsContract.CommonDataKinds.Phone.NUMBER);
while (phoneCursor.moveToNext()) {
String phoneNumber = phoneCursor.getString(idxPhone);
String name = phoneCursor.getString(idxName);
String avatarUri = phoneCursor.getString(idxAvatarUri);
Log.d("Details", "Phone number: " + phoneNumber);
Log.d("Details", "Name: " + name);
Log.d("Details", "Avatar URI: " + avatarUri);
}
} finally {
phoneCursor.close();
}
final Cursor emailCursor = getContentResolver().query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[] {
ContactsContract.CommonDataKinds.Email.ADDRESS,
},
Data.CONTACT_ID + "=?",
new String[] {String.valueOf(contactId)}, null);
try {
final int idxAddress = emailCursor.getColumnIndexOrThrow(
ContactsContract.CommonDataKinds.Email.ADDRESS);
while (emailCursor.moveToNext()) {
String address = emailCursor.getString(idxAddress);
Log.d("Details", "Email: " + address);
}
} finally {
emailCursor.close();
}
}
Please note that single contact can hold multiple phone numbers and emails.
Here is how to get all contacts details:
final Cursor cur = getContentResolver().query(Data.CONTENT_URI,
new String[]{Data.CONTACT_ID}, null, null, null);
try {
final int idxId = cur.getColumnIndex(Data.CONTACT_ID);
while (cur.moveToNext()) {
final int id = cur.getInt(idxId);
getContactDetails(id);
}
} finally {
cur.close();
}
Don't forget to add permission:
<uses-permission android:name="android.permission.READ_CONTACTS"/>