问题描述
- 在自己开发的应用程序中读取通讯录,想选择一个电话号码到自己的应用程序中
-
用下面的方法虽然可以得到一个电话号码,但不是自己选择的电话号码,每次都返回那个号码.求大神帮忙看看要怎么才能读取到自己所选的号码...先谢过了`enter code here @Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
ContentResolver cr = getContentResolver();
//取得电话本中开始一项的光标
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
//向下移动光标
while(cursor.moveToNext())
{
//取得联系人名字
int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = cursor.getString(nameFieldColumnIndex);
//取得电话号码
String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId, null, null);
Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId + " AND "
+ ContactsContract.CommonDataKinds.Phone.TYPE + "=" +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE, null, null);
if(phone.moveToFirst())
{
String PhoneNumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
string = PhoneNumber ;
cursor.close();
mNumber.setText(string);
break;
}
}}`
解决方案
Cursor cursor = mContentResolver.query(Contacts.CONTENT_URI, null,
null, null, null);
while (cursor.moveToNext()) {
//取得联系人ID,Name
int id = cursor.getInt(cursor.getColumnIndex(Contacts._ID));
String name = cursor.getString(cursor
.getColumnIndex(Contacts.DISPLAY_NAME));
//根据Id得到联系人的另一张表
Cursor cursorPhone = mContentResolver.query(Phone.CONTENT_URI,
null, Phone.CONTACT_ID + "=" + id, null, null);
while (cursorPhone.moveToNext()) {
//此表中有联系人号码
String phone = cursorPhone.getString(cursorPhone
.getColumnIndex(Phone.NUMBER));
Log.i("-------------------", phone);
ConstactVO vo = new ConstactVO(id, name, phone);
list.add(vo);
}
}
应该就是这样 参考一下吧