read/parse only Mobile numbers from android's phonebook (ContactsContract.Contacts)

被刻印的时光 ゝ 提交于 2019-12-13 01:54:13

问题


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);
    }

回答1:


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 ?




回答2:


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();
}


来源:https://stackoverflow.com/questions/23293985/read-parse-only-mobile-numbers-from-androids-phonebook-contactscontract-contac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!