把聯系人的姓名和電話號碼讀取出來就行了!
非常簡單
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
搞定况脆!
使用規(guī)則饭宾,以后再使用的時候,就一起考過去格了,直接用就行了看铆,就不需要再寫了!