开发者

Unable to resolve contact name from number

开发者 https://www.devze.com 2023-01-25 04:37 出处:网络
I\'m using a Android Wildfire and I used the following code to lookup the contact name of an existing phone number.

I'm using a Android Wildfire and I used the following code to lookup the contact name of an existing phone number.

private String getContactNameFromNumber(String number) {
    String[] projection = new String[] { Contacts.Phones.DISPLAY_NAME, 
            Contacts.Phones.NUMBER };

    Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(number));

    Log.d(TAG, contactUri.toString());

    // query time
    Cursor c = context.getContentResolver().query(contactUri, projection, null,
            null, null);

    // if the query returns 1 or more resu开发者_如何学Pythonlts
    // return the first result
    if (c.moveToFirst()) {
        String name = c.getString(c.getColumnIndex(Contacts.Phones.DISPLAY_NAME));
        Log.d(TAG, name);
        return name;
    }

    // return null if no match was found
    return null;
}

I am unable to resolve the contact name. I'm new to this topic any assistance will be appreciated. Many thanks in advance. The logcat output shows the following

11-14 13:45:35.879: DEBUG/Test(3342): content://contacts/phones/filter/%2B919773653345


Your method of getting the contacts is deprecated. You should use ContactsContract.PhoneLookup unless you're programming for older phones.

As LucaB said, the Cursor should probably be closed.


I think your problem is that you don't call the contacts correctly. Try changing

Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, Uri.encode(number));

to

Uri lookupUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, lookupKey)
Cursor c = getContentResolver().query(lookupUri, projection, null, null, null);
try {
    c.moveToFirst();
    String displayName = c.getString(0);
} finally {
    c.close();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号