private void getPhoneNumberById(String id) { //HashMap map = new HashMap();
Cursor cursor = null; Cursor phones = null; try { cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts._ID + "=" + id, null, null); if (cursor.moveToFirst()) { int phoneCount = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); Log.v("phoneCount", phoneCount+""); if (phoneCount > 0) { // 获得联系人的电话号码
phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null, null); if (phones.moveToFirst()) { do { HashMap<String, Object> map = new HashMap<String, Object>(); String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); String phoneType = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
if (phoneType.equals("1")) { // 家庭
map.put("type","家庭"); } else if (phoneType.equals("2")) { // 手机
map.put("type","手机"); } else if (phoneType.equals("3")) { // 单位
map.put("type","单位"); }else if(phoneType.equals("17")){ map.put("type","手机"); } else { map.put("type","其他"); } map.put("phoneNumber", phoneNumber); Log.v("type-phoneNumber", phoneNumber+"-"+phoneType); list.add(map); } while (phones.moveToNext()); } } } } catch (Exception e) { // TODO: handle exception
} finally { if (!cursor.isClosed() || cursor != null) cursor.close(); if (!phones.isClosed() || phones != null) phones.close(); }
}
|