【问题标题】:Android Contacts - Get Phone numberAndroid 通讯录 - 获取电话号码
【发布时间】:2013-11-14 05:33:42
【问题描述】:

我正在尝试在 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 字符串,我怎么能真的只获取电话号码?

【问题讨论】:

    标签: java android android-contacts phone-number


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!我不允许投票赞成你的答案,但它真的帮助了我!
    【解决方案2】:

    我使用以下代码及其工作,请参阅:

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

    【讨论】:

      猜你喜欢
      • 2012-10-27
      • 1970-01-01
      • 2012-12-10
      • 2011-07-19
      • 2010-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多