A few weeks ago I asked exactly the same question here. At first, I thought the answers solved my problem, but they didn't. I just didn't notice that I wasn't able to solve my problem with those answers.
However, what I've got now is:
final Cursor phoneCursor = context.getContentResolver().query(Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode("'" + incomingNumber + "'")), null, null, null, null);
phoneCursor.moveToFirst();
String lookupString = phoneCursor.getString(phoneCursor.getColumnIndex(PhoneLookup.LOOKUP_KEY));
final Cursor dataCursor = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, ContactsContract.Data.LOOKUP_KEY + "=" + "'" + lookupString + "'", null, null);
dataCursor.moveToFirst();
Log.e("smn", "display_name: " + dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)));
Log.e("smn", "nickname: " + dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DATA1)));
Output looks like this:
08-14 17:41:02.106: ERROR/smn(20146): display_name: Tom Tasche
08-14 17:41:02.106: ERROR/smn(20146): nickname: null
This answer told me that Nicknames ar开发者_如何学JAVAe held in Data-table, but although I'm querying Data-table, I don't retrieve the contact's alias saved in my addressbook.
I already tried it the other way: I inserted a new alias for this contact. And that worked fine. So, nicknames seem to work. Additionally, I've printed out every field that's saved in Data-table, again, without no luck.
Any ideas? Maybe I'm doing something completely wrong, but at the moment I don't see what's the problem...
Thanks for your help
Tom
I found a "workaround":
Uri uri = Uri.withAppendedPath(Contacts.CONTENT_VCARD_URI, lookup);
InputStream stream = settings.context.getContentResolver().openInputStream(uri);
int length = stream.available();
if (length <= 0) {
return;
}
byte[] vcard = new byte[length];
int bytesRead = stream.read(vcard, 0, length);
if (bytesRead < length) {
return;
}
String vcardString = new String(vcard, 0, bytesRead, "UTF-8");
String SPLIT = "X-ANDROID-CUSTOM:vnd.android.cursor.item/nickname;";
if (vcardString.contains(SPLIT)) {
vcardString = vcardString.substring(vcardString.indexOf(SPLIT) + SPLIT.length());
vcardString = vcardString.substring(0, vcardString.indexOf(';'));
name = vcardString;
}
It's really stupid, but it works and it's not that slow (~300 milliseconds).
Good luck
Tom
精彩评论