Reading only real contacts

69 views Asked by At

I have a program which reads the contacts and then displays it into the ListView. But the problem is that I have total 25k + contacts in my phone and 60% contacts are of google. So it takes too much time to read contacts. How do I read contacts having phone number ?

I'm using this code :

 Cursor phones = as.getApplicationContext().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
   String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
    String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));


}
phones.close();
2

There are 2 answers

1
Kartheek On

Where clause can be used to filter the contacts with the phone numbers. Use the below code to retrieve the contacts.

   CursorLoader cursorLoader = new CursorLoader(getActivity(), ContactsContract.Contacts.CONTENT_URI, new String[]{ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME},
            ContactsContract.Contacts.HAS_PHONE_NUMBER + " > 0 ", null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE NOCASE ASC;");

For reading the contacts you need the following permission.

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

You can use HAS_PHONE_NUMBER. ContactsContract.Contacts.HAS_PHONE_NUMBER . An Example

  String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

    if (hasPhone.equals(ContactsContract.Contacts.HAS_PHONE_NUMBER))
      //do your thing