繁体   English   中英

Android联系人-获取电话号码

[英]Android Contacts - Get Phone number

我正在尝试获取Android中联系人的所有电话号码。 我的代码如下所示:

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

原则上可以,但是变量“ contact”有时具有诸如“ null”,“ 1”,“ 4”或“ phonenumber @whatsapp”的值。 我如何才能真正获得没有这些愚蠢的WhatsApp ID字符串的电话号码?

您还可以检查什么是contact :电话号码或其他:

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

我使用下面的代码及其工作,请参见:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM