简体   繁体   English

从所选联系人处获取多个电话号码

[英]Getting multiple phone numbers from selected contact

I am developing an application in which i am using startactivity result and accessing default android phonebook in my application, after that on selecting one contact i am getting name, one phone number of the selected contact. 我正在开发一个应用程序,我在其中使用startactivity结果并访问我的应用程序中的默认android电话簿,之后选择一个联系人我得到名字,所选联系人的一个电话号码。 I want to retrieve multiple phone numbers if any of the contacts have it and type of phone numbers like work, mobile etc. please help me in this, Any help would be appreciated. 我想检索多个电话号码,如果任何联系人有它和工作,移动等电话号码的类型,请帮助我,任何帮助将不胜感激。

try this as this works for me: 试试这个,因为这适合我:

Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
            startActivityForResult(intent, AppConstants.PICK_CONTACT);

then in onActivityResult do the following: 然后在onActivityResult中执行以下操作:

Cursor cursor = null;
            String phoneNumber = "", primaryMobile = "";

            List<String> allNumbers = new ArrayList<String>();
            int contactIdColumnId = 0, phoneColumnID = 0, nameColumnID = 0;
            try {
                Uri result = data.getData();
                Utils.printLog(TAG, result.toString());
                String id = result.getLastPathSegment();
                cursor = getContentResolver().query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + "=?", new String[] { id }, null);
                contactIdColumnId = cursor.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID);
                phoneColumnID = cursor.getColumnIndex(Phone.DATA);
                nameColumnID = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

                if (cursor.moveToFirst()) {
                    while (cursor.isAfterLast() == false) {
                        idContactBook = cursor.getString(contactIdColumnId);
                        displayName = cursor.getString(nameColumnID);
                        phoneNumber = cursor.getString(phoneColumnID);

                        if (phoneNumber.length() == 0)
                            continue;

                        int type = cursor.getInt(cursor.getColumnIndex(Phone.TYPE));
                        if (type == Phone.TYPE_MOBILE && primaryMobile.equals(""))
                            primaryMobile = phoneNumber;
                        allNumbers.add(phoneNumber);
                        cursor.moveToNext();
                    }
                } else {
                    // no results actions
                }
            } catch (Exception e) {
                // error actions
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
}

try this 尝试这个

Uri personUri = ContentUris.withAppendedId(People.CONTENT_URI, personId);
Uri phonesUri = Uri.withAppendedPath(personUri, People.Phones.CONTENT_DIRECTORY);
String[] proj = new String[] {Phones._ID, Phones.TYPE, Phones.NUMBER, Phones.LABEL}
Cursor cursor = contentResolver.query(phonesUri, proj, null, null, null);

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

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