2013年6月3日 星期一

Get contact name given a phone number in Android

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

沒有留言:

張貼留言

Related Posts Plugin for WordPress, Blogger...