【发布时间】:2014-05-26 19:15:49
【问题描述】:
您好,我正在尝试从联系人列表中获取一个电话号码。我找到了可以让我获得整个联系人列表电话号码的代码,我想要的只是所点击项目的电话号码。任何帮助将不胜感激。谢谢..
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();
}
}
}
}
}
});
【问题讨论】:
-
哪个联系人?随机的?还是第一个?
-
打开联系人列表后点击的联系人..谢谢
标签: android android-contacts phone-number