public class MainActivity extends AppCompatActivity {
private ContentResolver cr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cr = getContentResolver();
}
public void getContact(View view){
Uri uri=Uri.parse("content://com.android.contacts/raw_contacts");
Cursor cursor=cr.query(uri,null,null,null,null);
while(cursor.moveToNext()){
//获取所有id
int _id=cursor.getInt(cursor.getColumnIndex("_id"));
//获取所有联系人
String display_name=cursor.getString(cursor.getColumnIndex("display_name"));
Log.i("test",_id+""+display_name);
Uri uriData=Uri.parse("content://com.android.contacts/raw_contacts/"+_id+"/data");
Cursor cursorData=cr.query(uriData,null,null,null,null);
while(cursorData.moveToNext()){
//获取所有类型
String mimetype=cursorData.getString(cursorData.getColumnIndex("mimetype"));
//获取所有信息
String datal=cursorData.getString(cursorData.getColumnIndex("data1"));
//根据单个号码来获取所有信息
if("vnd.android.cursor.item/phone_v2".equals(mimetype)){
Log.i("test"," "+mimetype+" "+datal);
}
}
}
}
}
<!--读取联系人信息的权限-->
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>