gpt4 book ai didi

android - 如何从联系人列表中选择电话号码

转载 作者:行者123 更新时间:2023-11-29 00:17:51 25 4
gpt4 key购买 nike

我正在使用以下代码读取联系人和选择姓名,但它只允许提取第一个联系人信息。请建议从多个电话号码中选择一个的任何方法

//request to open contacts intent
startActivityForResult(new Intent(Intent.ACTION_GET_CONTENT, ContactsContract.Contacts.CONTENT_URI), REQUEST_CODE_PICK_CONTACTS);

//on Activity result that reads first phone number

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_PICK_CONTACTS && resultCode == RESULT_OK) {
Log.d(TAG, "Response: " + data.toString());
uriContact = data.getData();

retrieveContactName();
retrieveContactNumber();
//retrieveContactPhoto();

}
}

最佳答案

    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...
}
}
}

关于android - 如何从联系人列表中选择电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25318867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com