如何让用户从联系人中选择一个电话号码,然后获取电话号码,给定姓名和姓氏

时间:2014-01-12 13:02:35

标签: java android android-contacts

我到目前为止所做的是:

点击某个按钮时:

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, CONTACT_PICKER_RESULT); 

然后:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {  
        switch (requestCode) {  
        case CONTACT_PICKER_RESULT:  
            if (data != null) {
                Uri uri = data.getData();
                if (uri != null) {
                    Cursor cursor = null;
                    Cursor cursorStructuredName = null;
                    try{    
                        cursor = getContentResolver().query(uri, null, null, null, null);      
                        boolean hasNext = cursor.moveToNext();
                        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                        String hasPhoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                        String phoneNumber = null;      

                        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);

                        if (phones.moveToFirst()) {//does not get inside the if
                            phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        }
                        phones.close();


                        // projection
                        String[] projection = new String[] {ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, 
                                ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
                                ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME,
                                ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
                                ContactsContract.CommonDataKinds.StructuredName.PREFIX,
                                ContactsContract.CommonDataKinds.StructuredName.SUFFIX};


                        String where = ContactsContract.Data.RAW_CONTACT_ID + " =?"; 
                        String[] whereParameters = new String[]{contactId};

                        //Request
                        cursorStructuredName = getContentResolver().query(ContactsContract.Data.CONTENT_URI, projection, where, whereParameters, null);

                        String displayName = null;
                        String givenName = null;
                        String middleName = null;                           
                        String familyName = null;
                        String prefix = null;
                        String suffix = null;
                        hasNext = cursorStructuredName.moveToFirst();
                        if (cursorStructuredName != null && hasNext) {//does not get here because hasNext equals false
                            displayName = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME)); 
                            givenName = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)); 
                            middleName = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME)); 
                            familyName = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME)); 
                            prefix = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX)); 
                            suffix = cursorStructuredName.getString(cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.SUFFIX)); 
                        }

                    } finally {
                        if (cursor != null) {
                            cursor.close();
                        }
                        if (cursorStructuredName != null) {
                            cursorStructuredName.close();
                        }
                    }
                }
            }
            break;
        }
    }
}

我无法获得姓氏,名字等。 我无法获得所选的电话号码。

正如我在标题中所说,我想要的是当用户点击某个按钮时,将打开联系人列表,当他选择特定联系人时,会显示此联系人电话号码的列表,然后当他选择时一个数字onAtivityResult的方法将在那里被调用 我将“解析”电话号码,姓氏,姓名等。

2 个答案:

答案 0 :(得分:1)

嗯,经过更多的研究,我已经解决了我的问题。

下一个代码允许用户按下按钮,然后打开联系人应用程序,让用户选择联系人(仅显示至少有一个电话号码的联系人)。  在选择联系人之后,其所有号码的列表都是打开的,让用户可以选择一个(例如,在某些设备中,相同的联系人出现的次数超过一次,每个电话号码都有一个) )。 之后调用onActivityResult()方法,然后(如果一切正常)我将取消所选择的电话号码,给定名称和所选联系人的姓氏:

@Override
public void onClick(View v) {
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
    startActivityForResult(intent, CONTACT_PICKER_RESULT);  
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {  
        switch (requestCode) {  
        case CONTACT_PICKER_RESULT:  
            handleContactSelection(data);
            break;
        }
    }
}

private void handleContactSelection(Intent data) {
    if (data != null) {
        Uri uri = data.getData();
        if (uri != null) {
            Cursor cursor = null;
            Cursor nameCursor = null;
            try {
                cursor = getContentResolver().query(uri, new String[]{  
                        ContactsContract.CommonDataKinds.Phone.NUMBER,
                        CommonDataKinds.Phone.CONTACT_ID} ,                                
                        null, null, null);

                String phoneNumber = null;                  
                String contactId = null;
                if (cursor != null && cursor.moveToFirst()) {
                    contactId = cursor.getString(cursor.getColumnIndex(CommonDataKinds.Phone.CONTACT_ID));
                    phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));                               
                }

                String givenName = null;///first name.
                String familyName = null;//last name.

                String projection[] = new String[]{ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
                        ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME};
                String whereName = ContactsContract.Data.MIMETYPE + " = ? AND " + 
                        ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ?";
                String[] whereNameParams = new String[] { ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, contactId};

                nameCursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, 
                        projection, whereName, whereNameParams, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);

                if(nameCursor != null && nameCursor.moveToNext()) {
                    givenName = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));
                    familyName = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));
                }

                doSomething(phoneNumber,givenName,familyName);

            } finally {
                if (cursor != null) {
                    cursor.close();
                }

                if(nameCursor != null){
                    nameCursor.close();
                }
            }
        }
    }
}   

答案 1 :(得分:0)

我相信这个会解决你的一些问题 。这基本上需要一个phoneno并查询其联系号。我使用它来将数据上传到服务器。至于电话号码,我从收到的电话听众那里得到它。


String[] projection = new String[]{
                                ContactsContract.PhoneLookup.DISPLAY_NAME};
                        Uri contacturi= Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneno));
                    ContentResolver cr = getContentResolver();      
Cursor crc= cr.query(contacturi, projection, null, null, null);
                    if(crc.moveToNext()){
                            name=crc.getString(crc.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));

                        }
                        else{
                            name ="Unknown";
                        }


您需要在crc游标上添加所有必填字段。即家庭,电子邮件,家庭。 希望这能为您提供一些见解: - /