这是我的代码
//OnClick
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, CONTACT_PICKER_RESULT);
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (CONTACT_PICKER_RESULT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = getActivity().getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// TODO Whatever you want to do with the selected contact name.
Log.d("data",name);
}
}
break;
}
}
我在片段中使用它,它会打开联系人列表但是从不调用onActivityResult
答案 0 :(得分:0)
由于声誉低,我可以写一个评论。检查manifest.xml中是否有以下标志:
android:launchMode="singleInstance"
或
android:noHistory="true"
因为它们无法提供结果。这些标志之间使用
<activity>
代码
答案 1 :(得分:0)
我在我的应用中使用了这种方法: 首先为manifest.xml文件添加权限,如:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
然后在想要向用户显示联系人时调用launchContactPicker():
public void launchContactPicker() {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
}
然后在activityResult上添加:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(resultCode==RESULT_OK)
{
switch (requestCode) {
case CONTACT_PICKER_RESULT:
Cursor cursor;
Bundle extras = data.getExtras();
if(extras != null)
{
Set<String> keys = extras.keySet();
for (String key : keys) {
extras.get(key);
}
}
else
{
Log.v("Auto Respond", "No extras returned from contact.");
}
Uri result = data.getData();
String id = result.getLastPathSegment();
String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
cursor = getContentResolver().query(result, projection,
null, null, null);
String contactName="";
String contactNumber="";
if(cursor != null && cursor.moveToFirst())
{
String nameOfselectedContact=cursor.getString(cursor.getColumnIndex((ContactsContract.Contacts.DISPLAY_NAME)));
ArrayList<String> numbersOfSelectedContact=new ArrayList<String>();
Log.e("contact size", cursor.getCount()+"");
for(int i=0;i<cursor.getCount();i++)
{
numbersOfSelectedContact.add(cursor.getString(cursor.getColumnIndex((ContactsContract.CommonDataKinds.Phone.NUMBER))));
cursor.moveToNext();
}
cursor.close();
}
break;
}
}
}
现在您将联系人姓名作为名为&#34; nameOfselectedContact&#34;的字符串。其所有数字都作为ArrayList命名为&#34; numbersOfSelectedContact&#34;然后,您可以根据需要使用它们(可以在对话框中显示它们)。