2

I am following this example retrieve list of contacts to retrieve contacts from android device. It gives all the contacts which includes email contacts and some empty contacts.

I tried modifying ContactsContract like ContactsContract.Contacts.HAS_PHONE_NUMBERin the projection and selection to get mobile contacts but nothing worked out.

String[] PROJECTION = {ContactsContract.Contacts._ID,ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.Contacts._ID,
SORT_ORDER,ContactsContract.Contacts.HAS_PHONE_NUMBER};


String SELECTION =
 ContactsContract.Contacts.DISPLAY_NAME +
 "<>''" + " AND "ContactsContract.Contacts.IN_VISIBLE_GROUP + "=1";

Any idea to get only contacts which has mobile numbers?

Mukesh
  • 515
  • 7
  • 21
  • use `ContactsContract.CommonDataKinds.Phone.CONTENT_URI` – pskink Nov 15 '15 at 14:38
  • @pskink i tried passing Phone.CONTENT_URI in selection but gives unrecognized token exception. – Mukesh Nov 15 '15 at 15:08
  • 1
    Why do you have SORT_ORDER and HAS_PHONE_NUMBER in projection ? HAS_PHONE_NUMBER should be in selection - since you seem to be wanting to select based on it. Use selection = "HAS_PHONE_NUMBER = 1" and pass appropriate sort order in the correct parameter for query. – RocketRandom Nov 17 '15 at 09:25
  • 1
    Changing ContactsContract.Contacts.CONTENT_URI to ContactsContract.CommonDataKinds.Phone.CONTENT_URI and adding HAS_PHONE_NUMBER = 1 in Selection instead of Projection fixed this, @RocketRandom thanks for your hint. – Mukesh Nov 17 '15 at 17:59
  • but i do get same contact multiple times, any suggestion to avoid that – Mukesh Nov 19 '15 at 05:31

3 Answers3

3

I have created this library to solve all your queries. It will only save contacts with at least one email or phone number. Also it will remove duplicates from emails and phone numbers from same contacts (created by 3rd party apps like whatsapp).

Suggestions are welcome.

Link : https://github.com/raghavsatyadev/ContactFetcher/

Raghav Satyadev
  • 728
  • 2
  • 11
  • 38
  • Thanks, just it crashes at line no : 34 in Helper.java number = PhoneNumberUtils.formatNumber(number, "IN").replaceAll("\\s+", ""); Everything else works like charm ! how can we search in this ? – Kathan Shah Oct 06 '18 at 17:00
  • @kathan shah I have added a library on given link that I have created, please check it – Raghav Satyadev Dec 04 '18 at 12:51
1

You can use this code to get all contact names and numbers

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
  String contactName=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
  String contactNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

}
phones.close();
Collins Abitekaniza
  • 4,496
  • 2
  • 28
  • 43
  • this really works where we can avoid getting email contacts, but 1.how to sort by display names in this query 2. how to eliminates repeated phone number – Mukesh Nov 15 '15 at 15:52
  • You can take advantage of the Collections class to perform the sort and other operations – Collins Abitekaniza Nov 15 '15 at 16:02
0

Show only contacts with phone number and open contact list intent:

Intent pickContactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); // Uri.parse("content://contacts")
// Show only user contacts with phone numbers
pickContactIntent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); // Show only user contacts with phone numbers
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);

Get the phone number from response/contact:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PICK_CONTACT_REQUEST) {
        if (resultCode == RESULT_OK) {
            Uri contactUri = data.getData();
            String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER};

            Cursor cursor = getActivity().getContentResolver().query(contactUri, projection, null, null, null);
            if (cursor == null) {
                // show error msg
                return;
            }
            cursor.moveToFirst();

            int column = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            if (column >= 0) {
                String phoneNo = cursor.getString(column);
                sendSmsMessage(phoneNo);
            } else {
                // show error msg
            }
            cursor.close();
        }
    }
}

Make sure, you also handle runtime permissions in API 23+

android.permission.READ_CONTACTS
Javatar
  • 2,518
  • 1
  • 31
  • 43