我想通过点击按钮显示电话簿中的联系人列表,然后从中选择一个联系人然后检索其联系号码?我不想制作我的自定义列表,有没有办法使用内置功能的机器人?
答案 0 :(得分:18)
尝试这个 - >
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 version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/pickcontact"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pick Contact" />
<TextView
android:id="@+id/contactnumber"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
答案 1 :(得分:3)
Intent i=new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(i, PICK_REQUEST);
发送到Intent
方法的onActivityResult()
将包含所选联系人的Uri
- 您可以通过getData()
上的Intent
致电来获取此信息
Here is a sample project证明了这一点,逻辑是在保留的片段中实现的,因此我们会在配置更改中挂起选定的联系人(例如,用户旋转屏幕)。
你也可以使用ACTION_GET_CONTENT
,我认为这是更现代的模式,虽然ACTION_PICK
当然有效,但我在撰写本文时都有示例代码。如果您将来阅读此内容(嗨,将来!),链接样本可能已更新为使用ACTION_GET_CONTENT
。
答案 2 :(得分:0)
:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, 1);
或:
Uri uri = Uri.parse("content://contacts");
Intent intent = new Intent(Intent.ACTION_PICK, uri);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, 1);
并非适用于所有联系人。我不知道为什么。但是这个:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
作品。
答案 3 :(得分:0)
对于Kotlin来说,RequestCode用于获取联系人
$date = DateTime::createFromFormat('Y/m/d', $fd);
echo $date->format('Y-m-d');
打算启动电话簿
private val REQUEST_CONTACT = 201
在onActivityResult中获取电话号码
private fun fetchPhoneNo() {
val intent = Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI)
startActivityForResult(intent, REQUEST_CONTACT)
}
答案 4 :(得分:0)
添加PICK_CONTACT
领域:
private final int PICK_CONTACT = 55;
在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();
}
});
在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;
}
}
}
答案 5 :(得分:0)
这是我在 API Level 29+
中尝试的工作代码第一步:在AndroidManifest.xml中添加权限
<uses-permission android:name="android.permission.READ_CONTACTS" />
第 2 步:点击按钮时打开联系人对话框的代码
private static final int REQ_PICK_CONTACT = 3;
private static final int CONTACT_PERMISSION_CODE = 1;
//Onclick handler
public void openContacts(View view) {
if(checkContactPermission()) {
openContactsDialog();
} else {
requestContactPermission();
}
}
//If permission is granted, then open contact box
private void openContactsDialog() {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, REQ_PICK_CONTACT);
}
//Check if the user has the permission granted
private boolean checkContactPermission() {
boolean result = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED;
return result;
}
//Invoke request permission dialog
private void requestContactPermission() {
String[] permission = {Manifest.permission.READ_CONTACTS};
ActivityCompat.requestPermissions(this, permission, CONTACT_PERMISSION_CODE);
}
//After the permission is granted, open the contact dialog
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == CONTACT_PERMISSION_CODE){
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openContactsDialog();
}
}
}
第 3 步:处理联系人选择
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_PICK_CONTACT) {
if (resultCode == RESULT_OK) {
Cursor cursor1, cursor2;
Uri uri = data.getData();
cursor1 = getContentResolver().query(uri, null, null, null, null);
if(cursor1.moveToFirst()) {
String contactId = cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts._ID));
String contactName = cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String hasNumber = cursor1.getString(cursor1.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if("1".equals(hasNumber)) {
cursor2 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId, null, null);
while(cursor2.moveToNext()) {
String contactNumber = cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//Use contactName and contactNumber for your purposes
}
cursor2.close();
}
}
cursor1.close();
//edtName.setText(contactName);
//edtNumber.setText(number);
}
}
}