2014-03-26 120 views
4

我正在制作一个电话阅读器应用程序,其中我能够找出来电的号码,但现在我希望我的应用程序说出联系人的姓名正在拨打电话。我无法找到联系人的姓名。任何人都可以帮忙 感谢如何从他的电话号码中获取联系人的姓名

+0

http://www.androidhive.info/2012/01/android-文本到语音教程/这将帮助你 –

回答

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

欲了解更多详情Visit this

+0

@P +1为您的_Awesome_答案。 –

+0

@简单计划谢谢 – Android

2

要获取来电号码的名称,使用

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

我将如何比较我的号码 –

相关问题