Android:从电话号码中检索联系人姓名

我想检索与传入电话号码关联的联系人姓名。 当我在有一个String的broascastreceiver中处理进来的号码时,来话呼叫者的名字将大大地帮助我的项目。

我会认为这涉及到一个查询使用SQL WHERE子句作为filter,但我需要sorting的联系人? 一个例子或提示将是非常有帮助的。

为此,您需要使用所描述的优化的PhoneLookup提供程序。

将权限添加到AndroidManifest.xml

 <uses-permission android:name="android.permission.READ_CONTACTS"/> 

然后:

 public String getContactName(final String phoneNumber, Context context) { Uri uri=Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,Uri.encode(phoneNumber)); String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}; String contactName=""; Cursor cursor=context.getContentResolver().query(uri,projection,null,null,null); if (cursor != null) { if(cursor.moveToFirst()) { contactName=cursor.getString(0); } cursor.close(); } return contactName; } 

虽然这已经被回答了,但这里是从数字获取联系人姓名的完整function。 希望它能帮助别人:

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

[根据Marcus的评论更新 ]

你将不得不要求这个权限:

<uses-permission android:name="android.permission.READ_CONTACTS"/>

这是非常有用的,这是我的最终代码来检索来电者的姓名,ID和照片:

 private void uploadContactPhoto(Context context, String number) { Log.v("ffnet", "Started uploadcontactphoto..."); String name = null; String contactId = null; InputStream input = null; // define the columns I want the query to return String[] projection = new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID}; // encode the phone number and build the filter URI Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); // query time Cursor cursor = context.getContentResolver().query(contactUri, projection, null, null, null); if (cursor.moveToFirst()) { // Get values from contacts database: contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID)); name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)); // Get photo of contactId as input stream: Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId)); input = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), uri); Log.v("ffnet", "Started uploadcontactphoto: Contact Found @ " + number); Log.v("ffnet", "Started uploadcontactphoto: Contact name = " + name); Log.v("ffnet", "Started uploadcontactphoto: Contact id = " + contactId); } else { Log.v("ffnet", "Started uploadcontactphoto: Contact Not Found @ " + number); return; // contact not found } // Only continue if we found a valid contact photo: if (input == null) { Log.v("ffnet", "Started uploadcontactphoto: No photo found, id = " + contactId + " name = " + name); return; // no photo } else { this.type = contactId; Log.v("ffnet", "Started uploadcontactphoto: Photo found, id = " + contactId + " name = " + name); } 

…然后只要用“input”(他们的照片作为InputStream),“name”和“contactId”做任何你想做的事情。

下面是列出你可以访问的约15列的文档,只需将它们添加到上面的代码开始附近的投影: http : //developer.android.com/reference/android/provider/ContactsContract。 PhoneLookup.html

此版本与Vikram.exe的答案相同,以避免ANR的代码

 interface GetContactNameListener { void contactName(String name); } public void getContactName(final String phoneNumber,final GetContactNameListener listener) { new Thread(new Runnable() { @Override public void run() { ContentResolver cr = getContentResolver(); Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null); if (cursor == null) { return; } String contactName = null; if(cursor.moveToFirst()) { contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)); } if(cursor != null && !cursor.isClosed()) { cursor.close(); } listener.contactName(contactName); } }).start(); } 

通过以下方法传递您接到呼叫的联络号码。 此方法将检查联系人是否保存在您的手机或不。 如果保存联系人,则会返回联系人姓名,否则返回string未知号码

将此代码添加到您的广播接收器类中

  public String getContactDisplayNameByNumber(String number,Context context) { Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number)); name = "Incoming call from"; ContentResolver contentResolver = context.getContentResolver(); Cursor contactLookup = contentResolver.query(uri, null, null, null, null); try { if (contactLookup != null && contactLookup.getCount() > 0) { contactLookup.moveToNext(); name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME)); // this.id = // contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.CONTACT_ID)); // String contactId = // contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID)); }else{ name = "Unknown number"; } } finally { if (contactLookup != null) { contactLookup.close(); } } return name; } 

获取源代码访问此链接