仅读取/解析安卓电话簿中的手机号码(ContactsContract.Contacts)



如何从机器人电话簿中获取所有真实的手机号码?

我使用了ContactsContract.Contact.Conths并创建了各自的游标。虽然它运行良好,我只获取有效的手机号码

我们可以使用ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE来过滤手机号码,但我们也可以将有效的手机号码保存到其他字段中,如ContactsContract.CommonDataKinds.Phone.TYPE_HOME,反之亦然

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

我不完全理解。你说的"真实"手机号码或"有效"手机号码是什么意思。只要没有人在其中一个电话号码字段中存储最新的彩票结果,所有号码都是有效的:-)如果一个号码是手机号码而不是固定电话号码,你正在寻找一种验证方法吗?这是你想要的吗?

这个函数在Android中使用了一些预先编码的东西,比如Patterns.PHONE.matcher…这可能很有用:

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

相关内容

  • 没有找到相关文章

最新更新