代码之家  ›  专栏  ›  技术社区  ›  Ragunath Jawahar cephus

无法从号码解析联系人姓名

  •  1
  • Ragunath Jawahar cephus  · 技术社区  · 14 年前

    我使用的是Android WildFire,我使用以下代码查找现有电话号码的联系人姓名。

    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 results
        // 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;
    }
    

    我无法解析联系人姓名。我对这个话题不熟悉,任何帮助都将不胜感激。非常感谢。logcat输出显示以下内容

    11-14 13:45:35.879: DEBUG/Test(3342): content://contacts/phones/filter/%2B919773653345
    
    2 回复  |  直到 14 年前
        1
  •  1
  •   Barbarrosa    14 年前

    您获取联系人的方法已被弃用。你应该使用 ContactsContract.PhoneLookup 除非你在为老手机编程。

    正如Lucab所说,光标应该关闭。

        2
  •  1
  •   lbedogni    14 年前

    我认为你的问题是你没有正确地打电话给联系人。 尝试改变

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

    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();
    }