get Contact Number by name - Android Account

Android examples for Account:Contact Name

Description

get Contact Number by name

Demo Code


import java.util.ArrayList;
import android.content.ContentProviderOperation;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Data;
import android.provider.ContactsContract.RawContacts;

public class Main{
    /* ww w. j a v  a  2 s.c o  m*/
    public static String getContactNumber(Context context, String name) {
        StringBuilder sb = new StringBuilder();
        ContentResolver cr = context.getContentResolver();
        Cursor pCur = cr.query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
                        + " = ?", new String[] { name }, null);
        if (pCur.moveToFirst()) {
            do {
                sb.append(pCur.getString(pCur
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
                        + ",");
            } while (pCur.moveToNext());
            pCur.close();
        }
        if (!StringUtil.isEmpty(sb.toString().trim())) {
            String phonec = sb.toString().trim();
            return phonec.substring(0, phonec.length() - 1).replaceAll(" ",
                    "");
        } else {
            return null;
        }
    }
}

Related Tutorials