query Contact For Phone Number - Android Account

Android examples for Account:Contact Number

Description

query Contact For Phone Number

Demo Code

/**// w ww .j  a va2  s . co  m
 * 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.
 *
 */
import android.content.Context;
import android.database.Cursor;
import android.database.CursorIndexOutOfBoundsException;
import android.net.Uri;
import android.provider.ContactsContract;
import android.util.Log;
import java.util.HashMap;

public class Main{
    private static final String TAG = ContactsContractHelper.class
            .getName();
    private static String queryContactForPhoneNum(Context c,
            String[] whereArgs) {
        String phoneNumber = null;
        Cursor cursor = c.getContentResolver().query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                        + " = ? and "
                        + ContactsContract.CommonDataKinds.Phone.TYPE
                        + " = ?", whereArgs, null);

        int phoneNumberIndex = cursor
                .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);

        if (cursor.getCount() > 0) {
            cursor.moveToFirst();
            phoneNumber = cursor.getString(phoneNumberIndex);
            cursor.close();
        }
        Log.i(TAG, "Returning phone number: " + phoneNumber);
        return phoneNumber;
    }
}

Related Tutorials