parent
b856ddad75
commit
ff40b156dc
@ -0,0 +1,40 @@
|
||||
package com.example.myapplication;
|
||||
public class PhoneDto {
|
||||
private String id; //序号
|
||||
private String name; //联系人姓名
|
||||
private String telPhone; //电话号码
|
||||
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getTelPhone() {
|
||||
return telPhone;
|
||||
}
|
||||
|
||||
public void setTelPhone(String telPhone) {
|
||||
this.telPhone = telPhone;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public PhoneDto() {
|
||||
}
|
||||
|
||||
public PhoneDto(String id, String name, String telPhone) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.telPhone = telPhone;
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package com.example.myapplication;
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.provider.ContactsContract;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PhoneUtil {
|
||||
//序号
|
||||
public final static String ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID;
|
||||
// 号码
|
||||
public final static String NUM = ContactsContract.CommonDataKinds.Phone.NUMBER;
|
||||
// 联系人姓名
|
||||
public final static String NAME = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME;
|
||||
|
||||
//上下文对象
|
||||
private Context context;
|
||||
//联系人提供者的uri
|
||||
private Uri phoneUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
|
||||
|
||||
public PhoneUtil(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
//获取所有联系人
|
||||
public List<PhoneDto> getPhone() {
|
||||
List<PhoneDto> phoneDtos = new ArrayList<>();
|
||||
ContentResolver cr = context.getContentResolver();
|
||||
Cursor cursor = cr.query(phoneUri, new String[]{ID, NUM, NAME}, null, null, null);
|
||||
while (cursor.moveToNext()) {
|
||||
@SuppressLint("Range") PhoneDto phoneDto = new PhoneDto(cursor.getString(cursor.getColumnIndex(ID)), cursor.getString(cursor.getColumnIndex(NAME)), cursor.getString(cursor.getColumnIndex(NUM)));
|
||||
phoneDtos.add(phoneDto);
|
||||
}
|
||||
return phoneDtos;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue