gpt4 book ai didi

android - 搜索联系人并从 android 中的电话联系人中获取联系电话

转载 作者:行者123 更新时间:2023-11-29 14:31:54 24 4
gpt4 key购买 nike

我正在使用一个应用程序,在此我必须在单击按钮并从电话联系人中选择一个联系人时搜索联系人。最后我想将它设置为 edittext。我可以从电话联系人中搜索联系人,但我无法从电话联系人中获取联系人。

Activity :

searchBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent,1);

}
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

if(requestCode==RESULT_OK){
Uri contactData = data.getData();
Cursor cursor = managedQuery(contactData, null, null, null, null);
cursor.moveToFirst();
String number = cursor.getString(cursor.getColumnIndexOrThrow
(ContactsContract.CommonDataKinds.Phone.NUMBER));

//contactName.setText(name);
ephoneNumber.setText(number);
//contactEmail.setText(email);
}
}

最佳答案

首先像这样为 StartActivityForResult 设置 Intent

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);
}

然后处理 native 联系人应用程序的 Intent 携带的结果

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

查看另一个引用链接

Link 1 Link 2

关于android - 搜索联系人并从 android 中的电话联系人中获取联系电话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22066142/

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