我想获取原生联系人的所有详细信息,我可以获得名字,姓氏,手机号码,家庭号码,工作号码。但是对于其他多个号码我只能获得另一个号码。如何获取所有其他输入的数字?
Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;
ContentResolver cr = getContentResolver();
// Querying the table ContactsContract.Contacts to retrieve all the contacts
Cursor contactsCursor = getContentResolver().query(contactsUri, null, null, null, ContactsContract.Contacts.DISPLAY_NAME + " ASC ");
if(contactsCursor != null && contactsCursor.getCount() > 0){
contactsCursor.moveToFirst();
do {
long contactId = contactsCursor.getLong(contactsCursor.getColumnIndex("_ID"));
Uri dataUri = ContactsContract.Data.CONTENT_URI;
// Querying the table ContactsContract.Data to retrieve individual items like
// home phone, mobile phone, work email etc corresponding to each contact
Cursor dataCursor = getContentResolver().query(dataUri, null, ContactsContract.Data.CONTACT_ID + "=" + contactId +" AND "+ContactsContract.CommonDataKinds.Phone.TYPE_OTHER +"=7", null, null);
if(dataCursor != null && dataCursor.getCount() > 0){
dataCursor.moveToFirst();
// Getting Display Name
displayName = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME ));
do{
// Getting NickName
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE))
nickName = dataCursor.getString(dataCursor.getColumnIndex("data1"));
// Getting Phone numbers
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){
switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE :
mobilePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME :
homePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK :
workPhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER :
otherPhone1 = dataCursor.getString(dataCursor.getColumnIndex("data1"));
otherPhone2 = dataCursor.getString(dataCursor.getColumnIndex("data2"));
otherPhone3 = dataCursor.getString(dataCursor.getColumnIndex("data3"));
otherPhone4= dataCursor.getString(dataCursor.getColumnIndex("data4"));
otherPhone5 = dataCursor.getString(dataCursor.getColumnIndex("data5"));
Log.d("othertypes of numbersssssssss ", "other types of numbersssss "+otherPhone1+" "+otherPhone2+" "+otherPhone3);
break;
}
}
答案 0 :(得分:0)
试试这个
字符串id,名称; ContentResolver cr = getContentResolver();
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, sortOrder);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
name = cur.getString( cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.i(tag, "Id is "+ id+"\t Name is"+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);
//此第二个循环将检索特定联系人ID的所有联系号码 while(pCur.moveToNext()){
// Do something with phones
int phNumber = pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);
String phn = pCur.getString(phNumber);
Log.i("phn number", phn);
}
pCur.close();
}
}
}