gpt4 book ai didi

java - startActivityForResult() 之后检索联系人姓名和电话

转载 作者:行者123 更新时间:2023-12-02 05:49:23 25 4
gpt4 key购买 nike

我已经开始一项新的意向 Activity 以获得结果代码:

    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
}
}
}

电话号码没问题,但我无法检索到联系人的名字!

最佳答案

 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));

关于java - startActivityForResult() 之后检索联系人姓名和电话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23710002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com