gpt4 book ai didi

android - 从联系人 Android 获取单个电话号码

转载 作者:太空狗 更新时间:2023-10-29 13:24:29 25 4
gpt4 key购买 nike

您好,我正在尝试从联系人列表中获取一个电话号码。我找到了可以获取整个联系人列表电话号码的代码,我想要的只是所点击项目的电话号码。任何帮助将不胜感激。谢谢..

public void onClick(View v) {
Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,Contacts.CONTENT_URI);
startActivityForResult(contactPickerIntent,CONTACT_PICKER_RESULT);

ContentResolver cr = getActivity().getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null,null, null);

if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID+ " = ?",new String[] { id }, null);

while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(getActivity(), phoneNo,Toast.LENGTH_SHORT).show();
}
}
}
}
}
});

最佳答案

当您在联系人列表中并单击联系人时,您将获得 Intent.ACTION_PICK Intent 的结果。

因此,要处理结果,您必须覆盖 onActivityResult() 方法。

您会在 onActivityResult() 返回的数据 Intent 对象中收到联系人的查找 URI。

所以,你所要做的就是:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if(requestCode == CONTACT_PICKER_RESULT){
if(resultCode==Activity.RESULT_OK){

Uri uri = data.getData();
ContentResolver contentResolver = getContentResolver();
Cursor contentCursor = contentResolver.query(uri, null, null,null, null);

if(contentCursor.moveToFirst()){
String id = contentCursor.getString(contentCursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

String hasPhone =
contentCursor.getString(contentCursor.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();
String contactNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i("phoneNUmber", "The phone number is "+ contactNumber);

}
}
super.onActivityResult(requestCode, resultCode, data);
}
}
}

然后您只需使用 contactNumber 上的值做任何您想做的事情。

希望对你有帮助。

关于android - 从联系人 Android 获取单个电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23032812/

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