//获取联系人姓名的cursor Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); startManagingCursor(cursor); ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); if(cursor.getCount() > 0){ cursor.moveToFirst(); int nameCoulmn = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME); int idCoulmn = cursor.getColumnIndex(ContactsContract.Contacts._ID); String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); do{ HashMap<String, String> map = new HashMap<String, String>(); //将联系人姓名放到map中 map.put("name", cursor.getString(nameCoulmn) + " "); //获取对应联系人在ID String id = cursor.getString(idCoulmn); Log.d("liuwenshan", "name: " + cursor.getString(nameCoulmn)); if(hasPhone.compareTo("1") == 0){ //根据联系人的ID获取该联系人的电话号码的crusor,然后遍历该联系人的所有号码 Cursor phoneCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id , null, null); phoneCursor.moveToFirst(); int numberCoulmn = phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); if(phoneCursor.getCount() > 0){ do{ String[] number_array = phoneCursor.getString(numberCoulmn).split("-"); String number_map = arrayToString(number_array); map.put("number", number_map); Log.d("liuwenshan", "number: " + number_map); }while(phoneCursor.moveToNext()); } } list.add(map); }while(cursor.moveToNext()); 获取android2.2的联系人与之前的版本不一样:
获取android2.2的联系人及电话号码
最新推荐文章于 2024-07-25 22:24:14 发布