仅读取/解析来自android电话簿的移动电话号码(ContactsContract.Contacts)

时间:2014-04-25 13:03:12

标签: android mobile contactscontract

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

我使用了ContactsContract.Contacts并创建了各自的游标。 虽然它工作正常, 我只是获取了有效的移动号码

我们可以使用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);
    }

2 个答案:

答案 0 :(得分:0)

我完全不明白。你是什​​么意思"真实"手机号码或"有效"一。所有号码都有效,因为很长时间没有人将最新的彩票结果存储在其中一个电话号码字段中:-)您是否正在寻找一种方法来验证,如果号码是手机号码而不是固定电话号码?这是你要求的吗?

答案 1 :(得分:0)

此功能使用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();
}