private void initData() {
Cursor cursor = null;
try{
//查询联系人数据
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
while (cursor.moveToNext()) {
//获取联系人姓名
String name = cursor.getString(cursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
//获取联系人手机号
String number = cursor.getString(cursor.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
//存入hashmap中
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("name", name);
hashMap.put("number", number);
phoneData.add(hashMap);
}
}catch (Exception e) {
e.printStackTrace();
}finally{
cursor.close();
}
}
权限
<uses-permission android:name="android.permission.READ_CONTACTS"/>