Android - 显示电话簿联系人并选择一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12123302/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - Showing Phonebook contacts and selecting one
提问by mdanishs
I want to show the list of contacts in phonebook on a click of a button and then select one of the contacts from it and then retrieve its contact number? I dont want to make my custom list, is there a way to use androids built in functionality?
我想通过单击按钮显示电话簿中的联系人列表,然后从中选择一个联系人,然后检索其联系号码?我不想制作我的自定义列表,有没有办法使用 androids 的内置功能?
回答by Mohit
TRY THIS-->
试试这个-->
setContentView(R.layout.main);
contactNumber = (TextView)findViewById(R.id.contactnumber);
Button buttonPickContact = (Button)findViewById(R.id.pickcontact);
buttonPickContact.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// 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 == RQS_PICK_CONTACT){
if(resultCode == 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);
contactNumber.setText(number);
//contactEmail.setText(email);
}
}
}
}
EDIT XML ADDED;
编辑 XML 添加;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/pickcontact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pick Contact" />
<TextView
android:id="@+id/contactnumber"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
回答by Bibaswann Bandyopadhyay
I see this question is old, but I was doing same thing in my application and thought to post my code here, if that helps anybody in future.
我看到这个问题很老,但我在我的应用程序中做了同样的事情,并想在这里发布我的代码,如果这对将来有帮助的话。
This is how to start new intent for showing the list
这是如何开始显示列表的新意图
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
And this is how to process the result
这是如何处理结果
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_CONTACT) {
if (resultCode == RESULT_OK) {
Uri contactData = data.getData();
String number = "";
Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
cursor.moveToFirst();
String hasPhone = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
if (hasPhone.equals("1")) {
Cursor phones = getContentResolver().query
(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
while (phones.moveToNext()) {
number = phones.getString(phones.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER)).replaceAll("[-() ]", "");
}
phones.close();
//Do something with number
} else {
Toast.makeText(getApplicationContext(), "This contact has no phone number", Toast.LENGTH_LONG).show();
}
cursor.close();
}
}
}
}
PICK_CONTACT is a constant defined in the class.
PICK_CONTACT 是类中定义的常量。
回答by CommonsWare
Intent i=new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(i, PICK_REQUEST);
The Intent
delivered to your onActivityResult()
method will contain the Uri
of the chosen contact -- you will get this by calling getData()
on that Intent
.
在Intent
交付给您的onActivityResult()
方法将包含Uri
选中的联系人的-你会通过调用得到这个getData()
上Intent
。
Here is a sample projectthat demonstrates this, with the logic being implemented in a retained fragment, so we hang onto the selected contact across configuration changes (e.g., user rotating the screen).
这是一个示例项目,演示了这一点,逻辑在保留的片段中实现,因此我们在配置更改(例如,用户旋转屏幕)时挂在选定的联系人上。
You can also use ACTION_GET_CONTENT
for this, and I think that's the more modern pattern, though ACTION_PICK
certainly works and is all I have sample code for at the time of this writing. If you are reading this in the future (hi, future!), it's possible that the linked-to sample has been updated to use ACTION_GET_CONTENT
.
您也可以使用ACTION_GET_CONTENT
它,我认为这是更现代的模式,虽然ACTION_PICK
肯定有效,并且在撰写本文时我只有示例代码。如果您将来正在阅读本文(嗨,未来!),则链接示例可能已更新为使用ACTION_GET_CONTENT
.
回答by Raja Jawahar
For Kotlin ,RequestCode for fetching Contact
对于 Kotlin,用于获取联系人的 RequestCode
private val REQUEST_CONTACT = 201
Intent to launch the PhoneBook
打算推出 PhoneBook
private fun fetchPhoneNo() {
val intent = Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI)
startActivityForResult(intent, REQUEST_CONTACT)
}
Get the PhoneNo in onActivityResult
在 onActivityResult 中获取 PhoneNo
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_CONTACT && data?.data != null) {
val contactUri = data.data;
val crContacts = contentResolver.query(contactUri, null, null, null, null);
crContacts.moveToFirst()
val id = crContacts.getString(crContacts.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer.parseInt(crContacts.getString(crContacts.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
val crPhones = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", arrayOf(id), null)
crPhones.moveToFirst()
var phoneNo = crPhones.getString(
crPhones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
crPhones.close()
}
crContacts.close()
}
}
回答by Shahab Saalami
add PICK_CONTACT
feild :
添加PICK_CONTACT
字段:
private final int PICK_CONTACT = 55;
in onCreate()
:
在onCreate()
:
viewPhonebook.setOnClickListener(v -> {
try {
Uri uri = Uri.parse("content://contacts");
Intent intent = new Intent(Intent.ACTION_PICK, uri);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
} catch (Exception e) {
e.printStackTrace();
}
});
in onActivityResult()
:
在onActivityResult()
:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case PICK_CONTACT:
Uri contactData = data.getData();
Cursor cursor = managedQuery(contactData, null, null, null, null);
cursor.moveToFirst();
String number = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
edtName.setText(contactName);
edtNumber.setText(number);
break;
}
}
}
回答by David
in nexus 5x emulator that I tested this:
在我测试过的 nexus 5x 模拟器中:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 1);
or:
或者:
Uri uri = Uri.parse("content://contacts");
Intent intent = new Intent(Intent.ACTION_PICK, uri);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);
not worked for all contacts. I don't know why. but this:
不适用于所有联系人。我不知道为什么。但是这个:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
works.
作品。