【问题标题】:Get contact name given a phone number in Android在Android中获取给定电话号码的联系人姓名
【发布时间】:2011-07-17 00:19:17
【问题描述】:

我正在尝试根据联系人电话号码检索联系人姓名。我制作了一个应该在所有 API 版本中都可以使用的功能,因为我无法使其在 1.6 中工作并且我看不到问题,也许有人可以发现它?

请注意,我已经替换了字符串的 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.6! developer.android.com/resources/dashboard/…。它仅占当前用户群的 2.2%,而且这个数字将不断缩小、缩小、缩小。它可能永远不会达到零,但这只是因为技术落后者无论如何都不会听到您的新前沿应用程序!不要浪费你的时间!
  • 为了方便他人,我写了一篇文章,其中包含查询姓名,照片,联系人ID等的完整代码,并附有适当的解释。该代码包含在不同答案中找到的 sn-ps,但更有条理和经过测试。链接:hellafun.weebly.com/home/…

标签: java android


【解决方案1】:

这似乎在最新版本中运行良好:

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

【讨论】:

  • 感谢它的工作原理,但如果你会得到很多名字,你应该在 asyncTask 中进行,以避免由于主线程中加载过多而导致不响应错误。
【解决方案2】:

使用反射而不是比较 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; 
}

【讨论】:

【解决方案3】:
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;
}

【讨论】:

    【解决方案4】:
     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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      • 1970-01-01
      • 2011-03-05
      • 2014-05-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多