I am trying to write a method that determines if a contact has at least one phone number, at the moment I have this:
public boolean hasPhone() {
Cursor phones = this.map.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
开发者_StackOverflow中文版null,
ContactsContract.Contacts._ID + "=" + this.contactId,
null,
null
);
boolean has = false;
if(phones.moveToFirst()) {
do {
if(Integer.parseInt(phones.getString(phones.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
has = true;
break;
}
} while(phones.moveToNext());
}
return has;
}
the method always returns false, even though I know the contact in question has a phone number. Also I know the contactId is correct as I also use it to get the postal address, etc.
Any help would be greatly appreciated, as I am about to tear my hair out :p
Thanks.
Try this. Replace the following line
ContactsContract.Contacts._ID + "=" + this.contactId,
with this line
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + this.contactId,
See if that works.
精彩评论