短信内容提供商与联系人之间的关系

时间:2011-07-02 07:58:16

标签: android android-contentprovider

我有一个很大的问题,我可以在短信内容提供商和联系人内容提供商之间获得的唯一关系是电话号码但是短信内容提供商以不同的格式存储号码与号码存储在联系人中的方式相比较内容提供商。因此,我怎么能比较两个表之间的数字,因为我没有看到任何其他关系或列绑定它们。我的国家电话号码格式是+254728306203,这是短信提供商存储号码的方式,但联系人提供商将其存储为072-830-6203任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

试试这个(适用于SDK 2.0+)。让数字为任何格式..只需将其作为字符串传递......

在清单中添加权限android.permission.READ_CONTACTS

/**
     * Retrieves display name for the provided contact number
     * 
     * @param context
     *            Context
     * @param number
     *            The contact number for which display name is to be retrieved
     *            form android contacts
     * @return Returns displayName for the contact if available. If display name
     *         is not available then it returns the same number
     */
    public static String getContactNameFromNumber(Context context, String number) {
        // If name is not found, number will be returned
        String contactDisplayName = number;

        Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
                Uri.encode(number));
        Cursor cursor = context.getContentResolver().query(uri,
                new String[] { PhoneLookup.DISPLAY_NAME }, null, null, null);
        if (cursor.moveToFirst()) {
            contactDisplayName = cursor.getString(cursor
                    .getColumnIndex(PhoneLookup.DISPLAY_NAME));
        }
        cursor.close();
        Log.d("GetNumber", "Retrived DisplayName for contact number:" + number
                    + " DisplayName:" + contactDisplayName);

        return contactDisplayName;

    }