Question

I'm trying to get all phone numbers of a contact in Android. My code looks like this:

ContentResolver cr = context.getContentResolver();
String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER };
String selection = ContactsContract.Data.CONTACT_ID + "=" + contactId;
Cursor nameCur = cr.query(ContactsContract.Data.CONTENT_URI, projection, selection, null, null);
while (nameCur.moveToNext()) {
    String contact = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}

In principle, it works but the variable "contact" sometimes has values like "null", "1", "4" or "phonenumber@whatsapp". How can I really only get the phone numbers without these stupid WhatsApp Id strings?

Was it helpful?

Solution

You can also check what is contact: phone number or something else:

public static boolean isPhoneNumberValid(CharSequence phone) {
    return !(TextUtils.isEmpty(phone)) && Patterns.PHONE.matcher(phone).matches();
}

OTHER TIPS

i use below code and its work, see this:

        Cursor cursor = getContentResolver().query(
                        ContactsContract.Contacts.CONTENT_URI, null, null,
                        null, null);

                cursor.moveToFirst();
                // data = new String[cursor.getCount()][12];
                if (cursor.getCount() > 0) {
                    do {
                        try {

                            contactId = cursor
                                    .getString(cursor
                                            .getColumnIndex(ContactsContract.Contacts._ID));



                            Uri contactUri = ContentUris.withAppendedId(
                                    Contacts.CONTENT_URI,
                                    Long.parseLong(contactId));
                            Uri dataUri = Uri.withAppendedPath(contactUri,
                                    Contacts.Data.CONTENT_DIRECTORY);

                            Cursor phones = getContentResolver()
                                    .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                            null,
                                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                    + " = " + contactId,
                                            null, null);
                            if (phones.getCount() > 0) {
                                ContactClass info = new ContactClass();
                                info.setid(contactId);

                                try {
                                    Cursor nameCursor = getContentResolver()
                                            .query(dataUri,
                                                    null,
                                                    Data.MIMETYPE + "=?",
                                                    new String[] { StructuredName.CONTENT_ITEM_TYPE },
                                                    null);
                                    nameCursor.moveToFirst();
                                    do {

                                        String firstName = nameCursor
                                                .getString(nameCursor
                                                        .getColumnIndex(Data.DATA2));

                                        String lastName = "";

                                        String displayname = cursor
                                                .getString(cursor
                                                        .getColumnIndex(Contacts.DISPLAY_NAME_ALTERNATIVE));
                                        if (!firstName.equals(displayname)) {
                                            lastName = nameCursor
                                                    .getString(nameCursor
                                                            .getColumnIndex(Data.DATA3));
                                        }

                                        if (firstName.equals(null)
                                                && lastName.equals(null)) {
                                            info.setfirstname("unknown name");
                                        } else if (firstName.equals(null)) {
                                            info.setlastname(lastName);
                                        } else if (lastName.equals(null)) {
                                            info.setfirstname(firstName);
                                        } else {
                                            info.setfirstname(firstName);
                                            info.setlastname(lastName);
                                        }

                                    } while (nameCursor.moveToNext());
                                    nameCursor.close();



                                } catch (Exception e) {

                                }
                            }
                            phones.close();
                        }

                        catch (Exception t) {

                        }

                    } while (cursor.moveToNext());
                    cursor.close();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top