Android examples for Account:Contact Number
get Contact Phone Number
/**/* ww w .j a va2s . c o 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 int[] typesPhone = new int[] { ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE, ContactsContract.CommonDataKinds.Phone.TYPE_MAIN, ContactsContract.CommonDataKinds.Phone.TYPE_HOME, ContactsContract.CommonDataKinds.Phone.TYPE_WORK }; /** * @param c * @param contactID * @return Phone number, if available. The search order is: mobile, main, home, work. */ public static HashMap<String, String> getContactPhoneNumber(Context c, String contactID) { HashMap<String, String> phoneNums = new HashMap<String, String>(); for (int i = 0; i <= typesPhone.length - 1; i++) { String[] whereArgs = new String[] { String.valueOf(contactID), String.valueOf(typesPhone[i]) }; String phoneNum = queryContactForPhoneNum(c, whereArgs); if (phoneNum != null) { if (ContactsContract.CommonDataKinds.Phone.TYPE_HOME == typesPhone[i]) { phoneNums.put("Home", phoneNum); } else if (ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE == typesPhone[i]) { phoneNums.put("Mobile", phoneNum); } else if (ContactsContract.CommonDataKinds.Phone.TYPE_WORK == typesPhone[i]) { phoneNums.put("Work", phoneNum); } else if (ContactsContract.CommonDataKinds.Phone.TYPE_MAIN == typesPhone[i]) { phoneNums.put("Main", phoneNum); } } } return phoneNums; } 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; } }