近日在项目开发过程中发现,华为手机HarmonyOS 3.0系统,设置>隐私 里面可以查看各个应用访问隐私权限的次数,发现应用程序访问手机通讯录的次数异常的高,针对访问通讯录频次高的问题做了研究和优化

问题分析:
分析代码发现只要通过ContentProvider 访问通讯录一次,统计次数就响应增加一次
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(ContactsContract.Data.CONTENT_URI, null, null, null, null);
代码中获取联系人头像、邮箱、号码、公司信息等都调用了query 方法,因此查询一个联系人的信息访问了多次通讯录,因此对获取联系人的方法做了改进,改进如下:
一、权限声明
AndroidManifest.xml文件中声明权限如下:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 获取联系人权限
<uses-permission android:name="android.permission.READ_CONTACTS" /> 文件读写权限
二、新建联系人信息类
import java.util.ArrayList;
import java.util.List;
/**
* Create time 2023/3/8 16:56
*/
public class ContactsInfo {
long contactId;
String displayName;
String photoUri;
String photoPath;
//
String firstName;
String lastName;
//
String company;
String department;
String job;
String jobDescription;
//
String emailAddress;
String emailAddressDisplayName;
String note;
String nickName;
String webUrl;
String relationName;
String protocol;
String customProtocol;
String identity;
String namespace;
String groupId;
List<ContactsNumber> contactsNumbers;
public long getContactId() {
return contactId;
}
public void setContactId(long contactId) {
this.contactId = contactId;
}
public String getDisplayName() {
return displayName == null ? "" : displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getPhotoUri() {
return photoUri == null ? "" : photoUri;
}
public void setPhotoUri(String photoUri) {
this.photoUri = photoUri;
}
public String getPhotoPath() {
return photoPath == null ? "" : photoPath;
}
public void setPhotoPath(String photoPath) {
this.photoPath = photoPath;
}
public String getFirstName() {
return firstName == null ? "" : firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName == null ? "" : lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCompany() {
return company == null ? "" : company;
}
public void setCompany(String company) {
this.company = company;
}
public String getDepartment() {
return department == null ? "" : department;
}
public void setDepartment(String department) {
th