需求:
1、获取手机联系人信息列表,包括(名称,手机号,名称首字母);
2、需要查询1000条记录时能快速返回,并按字母排序
实现:
1、添加权限-AndroidManifest.xml
<!-- 获取联系人 -->
<uses-permission android:name="android.permission.READ_CONTACTS" />
2、动态申请权限
//动态获取权限
String[] permissions = {Manifest.permission.READ_CONTACTS };
if( OpenPermissions(permissions,false)){
return "";
}
protected boolean OpenPermissions(String[] permissions ,boolean isShowTips ){
if (Build.VERSION.SDK_INT >= 23) {
int REQUEST_CODE_CONTACT = 103;
List<String> mPermissionList = new ArrayList<>();
//验证是否许可权限
for (String str : permissions) {
if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
//申请权限
mPermissionList.add(str);
}
}
if(mPermissionList.size() > 0){
if(isShowTips){
//showToastTips("权限不足!");
}
// String[] permissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将List转为数组
this.requestPermissions( mPermissionList.toArray(new String[mPermissionList.size()]), REQUEST_CODE_CONTACT);
return true;
}
}
return false;
}
3、其他需要添加第三方引用(bulid.gradle中添加)
//获取汉子首字母引用
implementation 'com.belerweb:pinyin4j:2.5.0'
//list转json
implementation 'com.google.code.gson:gson:2.8.5'
4、获取用户列表
首先定义用户信息类
public class ContactInfo implements Comparable<ContactInfo> {
public String id;
public String name;
public String mobile;
public String pinyin; // 姓名对应的拼音
public String firstLetter; // 拼音的首字母
//public String userAvatar;
//public String userName;
//public String userNick;
//public int isFriend;
//public String userId;
//public int gradeLevel;
//public String userPosition;
public String userCompany;
//public int userType;
//public boolean isUser = false;
public ContactInfo(){
}
public ContactInfo(String id, String name, String phone) {
this.id = id;
this.name = name;
this.mobile = phone;
pinyin = Cn2Spell.getPinYin(name); // 根据姓名获取拼音
firstLetter = pinyin.substring(0, 1).toUpperCase(); // 获取拼音首字母并转成大写
if (!firstLetter.matches("[A-Z]")) { // 如果不在A-Z中则默认为“#”
firstLetter = "#";
}
}
@Override
public int compareTo(ContactInfo another) {
if(another == null){
return -1;
}
if (firstLetter.equals("#") && !another.firstLetter.equals("#")) {
return 1;
} else if (!firstLetter.equals("#") && another.firstLetter.equals("#")){
return -1;
} else {
return pinyin.compareToIgnoreCase(another.pinyin);
}
}
}
接着,获取联系人集合,新建类ContactUtils,添加方法。(该方法已优化,只需查询一次。)
public static List<ContactInfo> getAllContacts(Context context) {
List<ContactInfo> list = new ArrayList<>();
String[] phoneNumberProjection = new String[]{
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.Contacts.DISPLAY_NAME};
Cursor c = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
phoneNumberProjection, null, null, null);
if (c != null) {
// long startTime = System.currentTimeMillis();
if (c.moveToFirst()) {
do {
String contactId = c.getString(0);
String name = c.getString(2);
String number = c.getString(1);
String _mb = number.replace(" ","");
if(!_mb.isEmpty()){
ContactInfo info = new ContactInfo(contactId,name,_mb);
list.add(info);
}else{
// ContactInfo info = new ContactInfo(contactId,name,"00000000000");
// list.add(info);
}
} while (c.moveToNext());
}
c.close();
// long endTime = System.currentTimeMillis();
// long time = endTime - startTime;
// System.out.println((float) (time / 1000) + "秒");
}
return list;
}
5、调用方法:
protected String GetUserContactList() {
String[] permissions = {Manifest.permission.READ_CONTACTS };
if( OpenPermissions(permissions,false)){
return "";
}
List<ContactInfo> contactInfo = ContactUtils.getAllContacts(mContext);
//模拟1000条数据
// while (contactInfo.size() < 900){
// contactInfo.addAll(contactInfo);
// }
Collections.sort(contactInfo);
String usInfo = "";
Gson gson = new Gson();
usInfo= gson.toJson(contactInfo);
return usInfo;
}