2011-07-17 59 views
10

我試圖根據聯繫人電話號碼檢索聯繫人姓名。我做了一個可以在所有API版本中工作的函數,因爲我無法在1.6版本中使其工作,並且我看不到問題,也許有人可以發現它?在Android中給出聯繫人姓名給出一個電話號碼

請注意,我已經替換了字符串的API常量,因此我沒有棄用的警告問題。

public String getContactName(final String phoneNumber) 
{ 
    Uri uri; 
    String[] projection; 

    if (Build.VERSION.SDK_INT >= 5) 
    { 
     uri = Uri.parse("content://com.android.contacts/phone_lookup"); 
     projection = new String[] { "display_name" }; 
    } 
    else 
    { 
     uri = Uri.parse("content://contacts/phones/filter"); 
     projection = new String[] { "name" }; 
    } 

    uri = Uri.withAppendedPath(uri, Uri.encode(phoneNumber)); 
    Cursor cursor = this.getContentResolver().query(uri, projection, null, null, null); 

    String contactName = ""; 

    if (cursor.moveToFirst()) 
    { 
     contactName = cursor.getString(0); 
    } 

    cursor.close(); 
    cursor = null; 

    return contactName; 
} 
+1

不支持1.6了! http://developer.android.com/resources/dashboard/platform-versions.html。它僅佔當前用戶羣的2.2%,這個數字將縮小,縮小,縮小。它可能永遠不會達到零,但這僅僅是因爲技術落後者無論如何都不會聽說你的新出血邊緣應用程序!不要浪費你的時間! –

+0

對於其他人的設施,我寫了一篇文章,其中包含了整個代碼,用於查詢姓名,照片,聯繫人ID等,並給出了正確的解釋。該代碼包含在不同答案中發現的片段,但更多地組織和測試。鏈接:http://hellafun.weebly.com/home/get-information-of-a-contact-from-number – Usman

回答

9

使用反射而不是比較sdk版本。

public String getContactName(final String phoneNumber) 
{ 
    Uri uri; 
    String[] projection; 
    mBaseUri = Contacts.Phones.CONTENT_FILTER_URL; 
    projection = new String[] { android.provider.Contacts.People.NAME }; 
    try { 
     Class<?> c =Class.forName("android.provider.ContactsContract$PhoneLookup"); 
     mBaseUri = (Uri) c.getField("CONTENT_FILTER_URI").get(mBaseUri); 
     projection = new String[] { "display_name" }; 
    } 
    catch (Exception e) { 
    } 


    uri = Uri.withAppendedPath(mBaseUri, Uri.encode(phoneNumber)); 
    Cursor cursor = this.getContentResolver().query(uri, projection, null, null, null); 

    String contactName = ""; 

    if (cursor.moveToFirst()) 
    { 
     contactName = cursor.getString(0); 
    } 

    cursor.close(); 
    cursor = null; 

    return contactName; 
} 
+0

你好,我嘗試了類似的東西,但它沒有奏效。這是我的問題,我非常感謝你的幫助! :) http://stackoverflow.com/questions/35097844/get-contact-name/35098111#35098111 –

19

這似乎在最新版本的做工精細:

private String getContactName(Context context, String number) { 

    String name = 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 != null) { 
     if (cursor.moveToFirst()) { 
      name =  cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)); 
      Log.v(TAG, "Started uploadcontactphoto: Contact Found @ " + number);    
      Log.v(TAG, "Started uploadcontactphoto: Contact name = " + name); 
     } else { 
      Log.v(TAG, "Contact Not Found @ " + number); 
     } 
     cursor.close(); 
    } 
    return name; 
} 
+1

感謝它的作品,但如果你會得到很多名字,你應該在asyncTask中,以避免不應答錯誤在主線程中負載很大。 – Aziz

0
private String getContactNameFromNumber(String 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()) 
{ 
    name = cursor.getString(cursor.getColumnIndex(PhoneLookup.D 
0
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; 
} 
相關問題