查询联系人的所有电话号码

时间:2012-03-21 04:35:03

标签: android

我设法编写添加程序,用户单击按钮后,他将能够检索将填充editText框的电话号码。问题是,如果联系人有多个号码,它将始终占据最多的号码。

我一直在阅读另一个帖子,Getting Number from Contacts Picker,有一个答案,但我不明白。因为我是Android编程新手, 如果有人能给我一步一步的指示,我将不胜感激。

1 个答案:

答案 0 :(得分:2)

首先,您可能需要查询电话簿中的所有联系人。

        // Run query
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME
        };
        String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '1'";
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

        // Build adapter with contact entries
        Cursor mCursor = managedQuery(uri, projection, selection, selectionArgs, sortOrder);
        //
        // Bind mCursor to to your Listview
        //

之后,当用户在列表视图中选择联系人时,您会进行第二次查询以获取该联系人的标签和号码。

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        mCursor.moveToPosition(position);
        startManagingCursor(mCursor);       
        String contactID = mCursor.getString(mCursor.getColumnIndex(ContactsContract.Contacts._ID));

        Cursor phoneNumCursor = getContentResolver().query(Phone.CONTENT_URI,  
                null, Phone.CONTACT_ID + "=?", new String[] { contactID }, null); 

        phoneNumCursor.moveToFirst();

        Vector<String> phoneTypeList = new Vector<String>();
        Vector<String> phoneNumberList = new Vector<String>();

        while (!phoneNumCursor.isAfterLast()){
            int type = phoneNumCursor.getInt(phoneNumCursor.getColumnIndex(Phone.TYPE));
            phoneTypeList.add(String.valueOf(Phone.getTypeLabel(getResources(),type,"")));
            phoneNumberList.add(phoneNumCursor.getString(phoneNumCursor.getColumnIndex(Phone.NUMBER)));
            phoneNumCursor.moveToNext();
        }
        //
        // Feel free to show label and phone number of that contact. ^^
        //

<强>更新

如果您想使用联系人选择器,下面是一个示例。

    private static final int CONTACT_PICKER_RESULT = 1001;

    protected void startContactPicker(){
        Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);  
        startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT); 
    }

    @Override  
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
        if (resultCode == RESULT_OK) {  
            switch (requestCode) {  
            case CONTACT_PICKER_RESULT:  
                Cursor cursor = null;  
                String phoneNumber = "";  
                try {  
                    Uri result = data.getData();
                    String id = result.getLastPathSegment();
                    cursor = getContentResolver().query(Phone.CONTENT_URI,  
                            null, Phone.CONTACT_ID + "=?", new String[] { id }, null);  

                    int phoneIdx = cursor.getColumnIndex(Phone.DATA);  
                    if (cursor.moveToFirst()) {  
                        while (!cursor.isAfterLast()){
                            phoneNumber = cursor.getString(phoneIdx);
                            //
                            // this will go through all phoneNumber of selected contact.
                            //

                            cursor.moveToNext();
                        }   
                    }  
                } catch (Exception e) {
                } finally {  
                    if (cursor != null) {  
                        cursor.close();  
                    } 
                    numberView.setText(phoneNumber);

                }  

                break;  
            }        
        }
    }