本文介绍了读/从Android的通讯录只能解析手机号码(ContactsContract.Contacts)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从机器人电话簿得到所有真正的手机号码?

我用ContactsContract.Contacts,创造各自的游标。
而它的做工精细,
我stuckup只取有效的手机号码

我们可以使用ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE过滤手机号码,
但我们也可以节约有效的手机号码到像ContactsContract.CommonDataKinds.Phone.TYPE_HOME等领域反之亦然

 字符串phoneNumber的= NULL;
    字符串email = NULL;    乌里CONTENT_URI = ContactsContract.Contacts.CONTENT_URI;
    字符串_ID = ContactsContract.Contacts._ID;
    字符串DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
    字符串HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;    乌里PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    字符串Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
    串号= ContactsContract.CommonDataKinds.Phone.NUMBER;    StringBuffer的输出=新的StringBuffer();    ContentResolver的ContentResolver的= getContentResolver();    光标光标= contentResolver.query(CONTENT_URI,NULL,NULL,NULL,NULL);    //循环手机中的每一个接触
    如果(cursor.getCount()大于0){
        Log.d(:,算:::+ cursor.getCount());        而(cursor.moveToNext()){            串CONTACT_ID = cursor.getString(cursor.getColumnIndex(_ID));
            字符串名称= cursor.getString(cursor.getColumnIndex(DISPLAY_NAME));            INT hasPhoneNumber =的Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER)));            如果(hasPhoneNumber大于0){                output.append(\\ n名字:+姓名);                //查询及回路接触的每一个电话号码
                光标phoneCursor = contentResolver.query(PhoneCONTENT_URI,空,Phone_CONTACT_ID +=?,新的String [] {} CONTACT_ID,NULL);                而(phoneCursor.moveToNext()){
                    phoneNumber的= phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
                    //串号= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    整型= phoneCursor.getInt(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                    开关(类型){
                        案例ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                            //做这里的家庭号码的东西...
                            打破;
                        案例ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                            output.append(\\ n电话号码:+ phoneNumber的);
                            打破;
                        案例ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                            //做这里的工作数量的东西...
                            打破;
                    }
                }
                / *
                而(phoneCursor.moveToNext()){
                    phoneNumber的= phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER));
                    output.append(\\ n电话号码:+ phoneNumber的);
                }
                * /                phoneCursor.close();
            }            output.append(\\ n);
        }        outputText.setText(输出);
    }


解决方案

这个功能在使用Android的某些pre-codeD的东西,像Patterns.PHONE.matcher ......这可能是有用的:

  //如果电话号码是有效的,则返回true
私人布尔isValidPhoneNumber(CharSequence的phoneNumber的){
    返回TextUtils.isEmpty(phoneNumber的)及!&安培; Patterns.PHONE.matcher(phoneNumber的).matches();
}

How to get all the real mobile numbers from androids phone book?

I used ContactsContract.Contacts and created respective cursors.while it's working fine, I am stuckup with only fetching VALID MOBILE NUMBERS

We can use ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE to filter Mobile Numbers,but we can also save valid mobile numbers into other fields like ContactsContract.CommonDataKinds.Phone.TYPE_HOME and vice versa

    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);
    }
解决方案

This function uses some pre-coded stuff in Android, like the Patterns.PHONE.matcher... This might be useful:

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

这篇关于读/从Android的通讯录只能解析手机号码(ContactsContract.Contacts)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 17:22