我可以使用以下代码从联系人中检索联系人姓名和号码。我的问题是,当一个联系人有多个号码时,它会以某种方式选择其中一个,这是我不想要的。
如果一个联系人有多个电话号码,我需要用户能够选择他想要选择的电话号码。
这是我的代码。
private void pickContacts() {
// TODO Auto-generated method stub
Intent it = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(it,1);
}
在 onActivityResult(int requestCode,int resultCode,Intent data)
if(requestCode==1){
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst())
{
String id = c.getString(
c.getColumnIndex(ContactsContract.Contacts._ID));
name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
localEditor.putString("contactName", name);
tv2.setText(name);
int hasPhone=c.getInt(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if(hasPhone==1){
Cursor pCur = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while(pCur.moveToNext()){
number =pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
// Toast.makeText(getApplicationContext(), number, Toast.LENGTH_LONG).show();
localEditor.putString("contactNumber", number);
tv3.setText(number);
}
pCur.close();
}
}while(c.moveToNext());
localEditor.commit();
// tv2.setText(name);
// tv3.setText(number);
super.onActivityResult(requestCode, resultCode, data);
}
提前感谢您的帮助..