如何通过号码获取联系人姓名

4

我正在开发一个通话读取应用程序,在其中我能够查找来电的号码,但现在我希望我的应用程序能够说出打电话的联系人姓名。我无法找到联系人的名称。有人可以帮忙吗? 谢谢


这篇文章会对你有所帮助:http://www.androidhive.info/2012/01/android-text-to-speech-tutorial/ - Renjith Krishnan
2个回答

4
public static String getContactName(Context context, String phoneNumber) {
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor cursor = cr.query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, null, null, null);
    if (cursor == null) {
        return null;
    }
    String contactName = null;
    if(cursor.moveToFirst()) {
        contactName = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));
    }

    if(cursor != null && !cursor.isClosed()) {
        cursor.close();
    }

    return contactName;
}

For more Details Visit this


1
获取来电号码的名称,使用以下代码:

name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NAME));

我该如何与我得到的数字进行比较? - Varun Ajay Gupta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接