简体   繁体   English

在 Android 中获取给定电话号码的联系人姓名

[英]Get contact name given a phone number in Android

I am trying to retrieve contact names given the contact phone number.我正在尝试在给定联系人电话号码的情况下检索联系人姓名。 I made a function that should work in all API versions, by I can't make it work in 1.6 and I can't see the problem, maybe someone can spot it?我制作了一个 function,它应该适用于所有 API 版本,因为我无法让它在 1.6 中工作,我看不到问题,也许有人能发现它?

Note that, I've replaced the API constants for strings so I don't have deprecated warning problems.请注意,我已经替换了字符串的 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; 
}

This seems to work fine in the latest versions:这似乎在最新版本中工作正常:

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

Use reflections instead of comparing sdk version.使用反射而不是比较 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; 
}
 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
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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM