Android - 电话号码中的联系人ID - 多个数字案例

时间:2013-05-29 16:59:31

标签: android

我想通过给定的电话号码获取联系人的ID。我知道使用

的众所周知的解决方案
Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone_number));

我的问题是,如果联系人的详细信息中包含多个相同类型的号码,则无效。例如两个手机号码然后我从内容解析器的光标中得不到任何内容。

有什么了解这个吗?

2 个答案:

答案 0 :(得分:0)

这就是我所做的:

    if (c.getCount() > 0) 
    {   
        while (c.moveToNext())
        {
            String id = cur.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
            {
                System.out.println("name : " + name + ", ID : " + id);

                // get the phone number
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                                       ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                                       new String[]{id}, null);
                while (pCur.moveToNext())
                {
                      String phone = pCur.getString(
                             pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                      System.out.println("phone" + phone);
                } 
                pCur.close();


                // get email and type

               Cursor emailCur = cr.query(        
                        ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
                        null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",  
                        new String[]{id}, null); 
                while (emailCur.moveToNext()) { 
                    // This would allow you get several email addresses
                        // if the email addresses were stored in an array  
                    String email = emailCur.getString(
                                  emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    String emailType = emailCur.getString(
                                  emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));

                  System.out.println("Email " + email + " Email Type : " + emailType);
                } 
                emailCur.close();

                // Get note.......
                String noteWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
                String[] noteWhereParams = new String[]{id, 
                ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE}; 
                        Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI, null, noteWhere, noteWhereParams, null); 
                if (noteCur.moveToFirst()) { 
                    String note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
                  System.out.println("Note " + note);
                } 
                noteCur.close();

                //Get Postal Address....

                String addrWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
                String[] addrWhereParams = new String[]{id, 
                    ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE}; 
                Cursor addrCur = cr.query(ContactsContract.Data.CONTENT_URI, 
                            null, null, null, null); 
                while(addrCur.moveToNext()) {
                    String poBox = addrCur.getString(
                                 addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
                    String street = addrCur.getString(
                                 addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
                    String city = addrCur.getString(
                                 addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
                    String state = addrCur.getString(
                                 addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
                    String postalCode = addrCur.getString(
                                 addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
                    String country = addrCur.getString(
                                 addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
                    String type = addrCur.getString(
                                 addrCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));

                    // Do something with these....

                } 
                addrCur.close();

                // Get Instant Messenger.........
                String imWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
                String[] imWhereParams = new String[]{id, 
                    ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE}; 
                Cursor imCur = cr.query(ContactsContract.Data.CONTENT_URI, 
                        null, imWhere, imWhereParams, null); 
                if (imCur.moveToFirst()) { 
                    String imName = imCur.getString(
                             imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
                    String imType;
                    imType = imCur.getString(
                             imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE));
                } 
                imCur.close();

                // Get Organizations.........

                String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
                String[] orgWhereParams = new String[]{id, 
                    ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE}; 
                Cursor orgCur = cr.query(ContactsContract.Data.CONTENT_URI, 
                            null, orgWhere, orgWhereParams, null);
                if (orgCur.moveToFirst()) { 
                    String orgName = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
                    String title = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
                } 
                orgCur.close();
            }
        }

希望这证明对你有帮助。

答案 1 :(得分:0)

奇怪的是

`ContactsContract.PhoneLookup._ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID`

你可以尝试以下方法,首先以正确的格式获取phone_number(PhoneNumberUtils类的各种格式方法没有帮助)

Uri lookupUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone_number));
Cursor cursor = context.getContentResolver().query(lookupUri, 
                   new String[]{ContactsContract.PhoneLookup.NUMBER},
                   ContactsContract.PhoneLookup.NUMBER + "=?",
                   new String[] { phone_number}, null);
if (cursor.moveToFirst()
{
     phone_number = cursor.getString(0);
}

上面给出了相同的数字,但格式使下面的代码有效。所以现在将格式化的phone_number传递给下面的代码

String[] projection = {ContactsContract.CommonDataKinds.Phone._ID}
String selection = ContactsContract.CommonDataKinds.Phone.NUMBER + "=?";
String[] selectionArgs = {phone_number};
return context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
                                    projection, selection, selectionArgs, null);