Lookup the name of a contact in the system contacts app - Android Account

Android examples for Account:Contact Name

Description

Lookup the name of a contact in the system contacts app

Demo Code

/**/*from   w  ww.  ja v  a 2  s .c  om*/
 * Property of Matt Allen
 * mattallen092@gmail.com
 * http://mattallensoftware.co.uk/
 *
 * This software is distributed under the Apache v2.0 license and use
 * of the Repay name may not be used without explicit permission from the project owner.
 *
 */
//package com.java2s;
import android.content.Context;
import android.database.Cursor;

import android.net.Uri;
import android.provider.ContactsContract;

public class Main {
    /**
     * Lookup the name of a contact in the system contacts app
     * @param c For {@link android.content.ContentResolver}
     * @param lookupURI The LOOKUP_URI for the contact
     * @return String reresentation for the contacts display name as set in the Contacts app
     */
    public static String getNameForContact(Context c, String lookupURI) {
        String[] cols = { ContactsContract.Contacts.DISPLAY_NAME };
        Cursor cursor = c.getContentResolver().query(Uri.parse(lookupURI),
                cols, null, null, null);
        cursor.moveToFirst();

        String result = cursor.getString(0).replaceAll("[-+.^:,']", "");
        cursor.close();

        return result;
    }
}

Related Tutorials