Подтвердить что ты не робот

Получите контактное имя с указанием номера телефона в Android

Я пытаюсь получить имена контактов, учитывая номер контактного телефона. Я сделал функцию, которая должна работать во всех версиях 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; 
}
4b9b3361

Ответ 1

Используйте сравнения вместо сравнения версии 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; 
}

Ответ 2

В последних версиях это отлично работает:

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

Ответ 3

 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

Ответ 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;
}