【问题标题】:Relationship between sms content provider and contacts短信内容提供者与联系人的关系
【发布时间】:2011-07-02 07:58:15
【问题描述】:

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

【问题讨论】:

    标签: android android-contentprovider


    【解决方案1】:

    试试这个(对于 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;
    
        }
    

    【讨论】:

    • 你是个天才,你能解释一下我的代码吗?因为它的工作方式就像一个魅力。
    • 感谢约翰接受答案。请参考此链接,以便您了解代码.. developer.android.com/reference/android/provider/…
    • 有一个问题,我将如何做与你的代码相反的操作,以便我将联系人表中的电话号码与 sms 表中的电话号码进行比较
    • 约翰,请为此创建一个不同的问题。它将帮助其他人找到问题和答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 2017-01-15
    相关资源
    最近更新 更多