1

How to get all the real mobile numbers from androids phone book?

I used ContactsContract.Contacts and created respective cursors. while it's working fine, I am stuckup with only fetching VALID MOBILE NUMBERS

We can use ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE to filter Mobile Numbers, but we can also save valid mobile numbers into other fields like ContactsContract.CommonDataKinds.Phone.TYPE_HOME and vice versa

    String phoneNumber = null;
    String email = null;

    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;

    StringBuffer output = new StringBuffer();

    ContentResolver contentResolver = getContentResolver();

    Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null, null);

    // Loop for every contact in the phone
    if (cursor.getCount() > 0) {
        Log.d(": ", "count ::: " +  cursor.getCount());

        while (cursor.moveToNext()) {

            String contact_id = cursor.getString(cursor.getColumnIndex(_ID));
            String name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME));

            int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER)));

            if (hasPhoneNumber > 0) {

                output.append("\n First Name:" + name);

                // Query and loop for every phone number of the contact
                Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[]{contact_id}, null);

                while (phoneCursor.moveToNext()) {
                    phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
                    //String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    int type = phoneCursor.getInt(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                    switch (type) {
                        case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                            // do something with the Home number here...
                            break;
                        case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                            output.append("\n Phone number:" + phoneNumber);
                            break;
                        case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                            // do something with the Work number here...
                            break;
                    }
                }
                /*
                while (phoneCursor.moveToNext()) {
                    phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
                    output.append("\n Phone number:" + phoneNumber);
                }
                */

                phoneCursor.close();


            }

            output.append("\n");
        }

        outputText.setText(output);
    }
Rinav
  • 2,527
  • 8
  • 33
  • 55

2 Answers2

0

i do not understand completely. what do you mean with "real" mobile numbers or "valid" one. all numbers are valid as long nobody stores the latest lottery results in one of the telephone number fields :-) are you are looking for a method to verify, if a number is a mobile number and not a landline number ? is it what you are asking for ?

David
  • 15,894
  • 22
  • 55
  • 66
  • Yes exactly, I am looking for looping phone book, but I only want mobile numbers. Problem is my phone is synced with Google+, so there are lots of empty contacts without numbers. secondly Mobile numbers can be saved in landline or fax or any other field in phones contact app. So I cant use Phone.TYPE_MOBILE. I hope to clarify my question – Rinav Apr 26 '14 at 14:41
  • in this situation i can't help. so you need for each country all prefixes of mobile telephone providers. this table shouldn't be too large. and it should be possible to find this in the internet, as there is almost nothing, which you can't find. // hans –  Apr 27 '14 at 17:55
0

This function uses some pre-coded stuff in Android, like the Patterns.PHONE.matcher... This might be useful:

//Returns true if phone number is valid
private boolean isValidPhoneNumber(CharSequence phoneNumber) {
    return !TextUtils.isEmpty(phoneNumber) && Patterns.PHONE.matcher(phoneNumber).matches();
}
Mario
  • 405
  • 7
  • 15