两行代码实现 读取手机联系人

可以直接获取系统数据库对应的游标指针,直接上代码:

Cursor cursor=null;

try{

cursor=getContentResolver().query(

ContactsContract.CommonDataKinds.Phone.CONTENT_URI

,null,null,null,null);

Log.i("tag",cursor.toString()+"============================");

while(cursor.moveToNext()){

String displayname=cursor.getString(cursor.getColumnIndex(

ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME

));

String number=cursor.getString(cursor.getColumnIndex(

ContactsContract.CommonDataKinds.Phone.NUMBER

));

//                contentList.add(displayname+"\n"+number);

LogUtils.i("TAG","readContacts: "+displayname+number);

}

}catch(Exception e){

ToastUtil.showLong(getApplicationContext(),"请开启通讯录权限");

e.printStackTrace();

}finally{

if(cursor!=null)

cursor.close();

}

记得加读取通讯录权限哦

你可能感兴趣的:(两行代码实现 读取手机联系人)