在 startActivityForResult() 之后检索联系人姓名和电话 [重复]
Posted
技术标签:
【中文标题】在 startActivityForResult() 之后检索联系人姓名和电话 [重复]【英文标题】:Retrieve contact name and phone after startActivityForResult() [duplicate] 【发布时间】:2014-07-05 18:36:58 【问题描述】:我已经为结果开始了一个新的意图活动 代码:
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK, Phone.CONTENT_URI);
startActivityForResult(contactPickerIntent, 1);
现在我想获取电话和号码:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (requestCode == 1)
if(resultCode == RESULT_OK)
Uri contactUri = data.getData();
String[] pN = Phone.NUMBER;
String[] pNa = Phone.CONTACT_ID;//idk
Cursor cP = getContentResolver().query(contactUri, pN, null, null, null);
cP.moveToFirst();
Cursor cPa = getContentResolver().query(contactUri, pNa, null, null, null);
cPa.moveToFirst();
int numc = cP.getColumnIndex(Phone.NUMBER);
String num = cP.getString(numc);
int namec = cPa.getColumnIndex(Phone.CONTACT_ID);//idk
String name = cPa.getString(namec);//idk
Log.i("", name);
if (resultCode == RESULT_CANCELED)
//DO OTHER STUFF
电话号码没问题,但我无法检索联系人的 GIVEN NAME!
【问题讨论】:
联系人的名字是DISPLAY_NAME
,而不是CONTACT_ID
【参考方案1】:
Uri uri = data.getData();
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
cursor.moveToFirst();
number = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
【讨论】:
【参考方案2】:试试这个代码,它可能有助于做一些小的改动...
static final int PICK_CONTACT_=1;
Intent intent_1 = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent_1, PICK_CONTACT_);
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data)
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode)
case (PICK_CONTACT_) :
if (resultCode == Activity.RESULT_OK)
Uri contactData = data.getData();
Cursor cursor = managedQuery(contactData, null, null, null, null);
if (cursor.moveToFirst())
String id =cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String hasPhone =cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1"))
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,
null, null);
phones.moveToFirst();
cNumber = phones.getString(phones.getColumnIndex("data_1"));
System.out.println("number is:"+cNumber);
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
break;
【讨论】:
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));抛出错误 这是因为,在你的情况下它是 'Number' 而不是 'HAS_PHONE_NUMBER' ,所以你应该做一些小的改变来适应你的需求。 好的等我测试 没有ContactsContract.Contacts.NUMBER这样的东西以上是关于在 startActivityForResult() 之后检索联系人姓名和电话 [重复]的主要内容,如果未能解决你的问题,请参考以下文章
Android Fragment 中的 startActivityForResult() 与 getActivity().startActivityForResult()
如何在 RecyclerView 中实现 StartActivityForResult
在视图中实现方法 startActivityForResult
Fragment的startActivityForResult和Activity的startActivityForResult的区别