选择特定联系人并添加到应用程序列表视图

时间:2016-02-17 03:11:30

标签: android listview contacts

我想阅读所有联系人并在我的申请中使用这些联系人。我阅读了所有相关主题,但我的实施却非常不同......

我想要达到这样的形式:

see Image

1位用户可以选择特定联系人

2-他们的信息必须添加到应用程序列表视图

3 - 用户可以为他选择的联系人选择静音/正常状态。

我如何根据这种情况获得联系?

2 个答案:

答案 0 :(得分:0)

使用显式意图和startActivityForResult()获取联系人

static final int PICK_CONTACT_REQUEST = 1;  // The request code
...
private void pickContact() {
    Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts"));
    pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}

===============================

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request it is that we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // Get the URI that points to the selected contact
            Uri contactUri = data.getData();
            // We only need the NUMBER column, because there will be only one row in the result
            String[] projection = {Phone.NUMBER};

            // Perform the query on the contact to get the NUMBER column
            // We don't need a selection or sort order (there's only one result for the given URI)
            // CAUTION: The query() method should be called from a separate thread to avoid blocking
            // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
            // Consider using CursorLoader to perform the query.
            Cursor cursor = getContentResolver()
                    .query(contactUri, projection, null, null, null);
            cursor.moveToFirst();

            // Retrieve the phone number from the NUMBER column
            int column = cursor.getColumnIndex(Phone.NUMBER);
            String number = cursor.getString(column);

            // Do something with the phone number...
        }
    }
}

然后根据需要向应用程序li​​stview.do添加详细信息

答案 1 :(得分:0)

不知道你是否在一年后想出来,但这是第1步:

private static final int CONTACT_PICKER_RESULT = 1001;
...
    public void methodname() {

    // By using ContactsContract.CommonDataKinds.Phone.CONTENT_URI, the user is
    // presented with a list of contacts, with one entry per phone number.
    // The selected contact is guaranteed to have a name and phone number.

    Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
    startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
    return true;
    }