您好我想从我们的默认联系簿意图中选择联系人。 我尝试了几种方法来做到这一点。请在下面找到代码。所有这些代码的问题在于,他们打开一个中间文档屏幕,用户必须选择联系人,而不是打开联系簿。
private void openContactIntent() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, ContactsContract.Contacts.CONTENT_URI);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
startActivityForResult(intent, REQ_CONTACT_DIRECTORY);
}
我也试过
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
和
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
答案 0 :(得分:11)
我也有同样的问题。最后,我使用下面的代码摆脱了中间选择器屏幕,
Intent i=new Intent(Intent.ACTION_PICK);
i.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(i, SELECT_PHONE_NUMBER);
在onActivityResult
获取电话号码
if (requestCode == SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
// Get the URI and query the content provider for the phone number
Uri contactUri = data.getData();
String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor cursor = getContext().getContentResolver().query(contactUri, projection,
null, null, null);
// If the cursor returned is valid, get the phone number
if (cursor != null && cursor.moveToFirst()) {
int numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
String number = cursor.getString(numberIndex);
// Do something with the phone number
...
}
cursor.close();
}
答案 1 :(得分:4)
尝试使用以下代码选择联系人:
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI); startActivityForResult(contactPickerIntent,RESULT_PICK_CONTACT);
您可以按如下方式在onActivityResult中获取所需信息:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case RESULT_PICK_CONTACT:
Cursor cursor = null;
try {
String phoneNo = null;
String name = null;
Uri uri = data.getData();
cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int phoneIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int nameIndex =cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
phoneNo = cursor.getString(phoneIndex);
name = cursor.getString(nameIndex);
Log.e("Name and Contact number is",name+","+phoneNo);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
} else {
Log.e("Failed", "Not able to pick contact");
}
}
答案 2 :(得分:1)
这对我有用:
Intent it= new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(it, requestCode);
答案 3 :(得分:1)
private static final int RESULT_PICK_CONTACT1= 1;
public void pickContact1 (View v)
{
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, RESULT_PICK_CONTACT1);
}
@Override
protected void onActivityResult ( int requestCode, int resultCode, Intent data){
if (resultCode == RESULT_OK) {
switch (requestCode) {
case RESULT_PICK_CONTACT1:
contactPicked1(data);
break;
}
} else {
Log.e("SetupActivity", "Failed to pick contact");
}
}
private void contactPicked1 (Intent data){
Cursor cursor = null;
try {
String phoneNo = null;
String name = null;
Uri uri = data.getData();
cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int phoneIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
name = cursor.getString(nameIndex);
phoneNo = cursor.getString(phoneIndex);
ed11.setText(name);
ed12.setText(phoneNo);
} catch (Exception e) {
e.printStackTrace();
}
}
这肯定会起作用。
答案 4 :(得分:0)
Try This link may to be help you
http://stackandroid.com/tutorial/contact-picker-using-intent-android-tutorial/
答案 5 :(得分:0)
这是从Kotlin的联系人中选择姓名和电话号码的方法
private fun pickEmergencyFromContacts() {
val i = Intent(Intent.ACTION_PICK)
i.type = ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE
startActivityForResult(i, SELECT_PHONE_NUMBER)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == SELECT_PHONE_NUMBER && resultCode == Activity.RESULT_OK) {
val contactUri = data?.data ?: return
val projection = arrayOf(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER)
val cursor = requireContext().contentResolver.query(contactUri, projection,
null, null, null)
if (cursor != null && cursor.moveToFirst()) {
val nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
val numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
val name = cursor.getString(nameIndex)
val number = cursor.getString(numberIndex)
// do something with name and phone
}
cursor?.close()
}
}
companion object {
private const val SELECT_PHONE_NUMBER = 111
...
}
希望有帮助
答案 6 :(得分:0)
这是一个很好的实现(在Kotlin中),我将在此处添加该实现以帮助从游标获取联系信息。 您还需要授予权限并请求运行时权限
<uses-permission android:name="android.permission.READ_CONTACTS" />
@SuppressLint("Recycle")
private fun getContactDataFrom(
contentResolver: ContentResolver,
contactUri: Uri
): ContactModel? {
val projection = arrayOf(
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER
)
try {
val cursor = contentResolver.query(contactUri, projection, null, null, null) ?: return null
if (!cursor.moveToFirst()) {
return null
}
val id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID))
val name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))
val hasNumber = (cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))
cursor.close()
val email = getEmail(contentResolver, id)
val phoneNumber = if (hasNumber > 0) {
getPhoneNumber(contentResolver, id)
} else {
""
}
return ContactModel(name, phoneNumber, email)
} catch (e: Exception) {
Log.e("Contacts", "Could not get contact info.", e)
return null
}
}
@SuppressLint("Recycle")
private fun getPhoneNumber(contentResolver: ContentResolver, id: String?): String {
var phoneNumber = ""
val cursor = contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.Contacts._ID + " = ?",
arrayOf(id),
null
) ?: return phoneNumber
if (cursor.moveToFirst()) {
phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA))
}
cursor.close()
return phoneNumber
}
@SuppressLint("Recycle")
private fun getEmail(contentResolver: ContentResolver, id: String?): String {
var email = ""
val cursor = contentResolver.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
arrayOf(id),
null
) ?: return email
if (cursor.moveToFirst()) {
email = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA))
}
cursor.close()
return email
}
触发:
val intent = Intent(this, PickLabelActivity::class.java)
startActivityForResult(intent, PickLabelActivity.REQ_CODE_PICK_LABEL)
onActivityResult中的用法应类似于:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == RESULT_OK && requestCode == ConstantsHandler.INTENT_CODE_PICK_CONTACT) {
val contactUri = data?.data ?: return
val contactModel = getContactDataFrom(contentResolver, contactUri)
inputName.setText(contactModel?.name)
inputPhone.setText(contactModel?.phone)
inputEmail.setText(contactModel?.email)
}
}
ContactModel只是一个简单的数据类。
data class ContactModel(
var name: String? = null,
var phone: String? = null,
var email: String? = null
) : Serializable
答案 7 :(得分:0)
对于 Android 11+,需要在 AndroidManifest 中添加以下代码
......
</application>
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.dir/contact" />
</intent>
</queries>
</manifest>