读取联系人

把联系人的姓名和电话号码读取出来就行了!


非常简单

1, 清单文件申请权限!---read_contacts 权限



/*

* 读取联系人并且打印出来就行了,先写写看,然后再封装起来,以后就不用再写了!

*

*/

private void readContacts() {

Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;

contentResolver = this.getContentResolver();

Cursor cursor = contentResolver.query(uri, null,

null,

null, null);

if(cursor!=null && cursor.getCount()>0){

while(cursor.moveToNext()){

int  numberIndex = cursor.

getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

String phone = cursor.getString(numberIndex);

int nameIndex = cursor.

getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);

String name = cursor.getString(nameIndex);

Person p = new Person(name,phone);

persons.add(p);

}

cursor.close();

}

for(Person p : persons){

Log.i("TAG",p.getName()+"+|"+p.getPhone());

}

}



还缺个bean 类, 非常简单person 类

public class Person {

private String name;

private String phone;

public Person() {

}

public Person(String name, String phone) {

this.name = name;

this.phone = phone;

}

public String getName() {

return name;

}

public String getPhone() {

return phone;

}

public void setName(String name) {

this.name = name;

}

public void setPhone(String phone) {

this.phone = phone;

}

@Override

public String toString() {

return "Person [name=" + name + ", phone=" + phone + "]";

}

}



好了运行起来就如下了

小明|1234567890

小花|2343434343

搞定!



使用规则,以后再使用的时候,就一起考过去,直接用就行了,就不需要再写了!

你可能感兴趣的:(读取联系人)