Android获取手机通讯录的信息(姓名和改姓名下的所有手机号
- 1.实现过程
- 2.实现代码
1.实现过程
1 定义一个专门存储信息的对象类,有姓名,首字母和手机号列表
2 通过获取手机的读写权限,然后来获取手机通讯录信息
2.实现代码
1.对象存储类
import java.util.List;public class ContactsBean {private String fullName;private List<String> phoneList;private String namePrefix;public String getNamePrefix() {return namePrefix;}public void setNamePrefix(String namePrefix) {this.namePrefix = namePrefix;}public String getFullName() {return fullName;}public void setFullName(String fullName) {this.fullName = fullName;}public List<String> getPhoneList() {return phoneList;}public void setPhoneList(List<String> phoneList) {this.phoneList = phoneList;}
}
2.获取手机通讯录对象信息实现类
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;import com.hjq.permissions.OnPermission;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;import org.jetbrains.annotations.NotNull;import java.util.ArrayList;
import java.util.List;public class ContactUtils {//(实际上就是“sort_key”字段) 出来是首字母private static final String PHONE_BOOK_LABEL = "phonebook_label";//需要查询的字段private static final String[] CONTACTOR_ION = new String[]{ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER,};private static List<ContactsBean> number = new ArrayList<>();public static List<ContactsBean> getAllContacts(Activity context) {if (XXPermissions.isHasPermission(context, Permission.READ_CONTACTS,Permission.WRITE_CONTACTS)) {number = getNumber(context);}else {XXPermissions.with(context).constantRequest() //可设置被拒绝后继续申请,直到用户授权或者永久拒绝.permission(Permission.READ_CONTACTS,Permission.WRITE_CONTACTS) //不指定权限则自动获取清单中的危险权限.request(new OnPermission() {@Overridepublic void hasPermission(List<String> granted, boolean isAll) {number = getNumber(context);}@Overridepublic void noPermission(List<String> denied, boolean quick) {}});}return number;}@NotNullprivate static List<ContactsBean> getNumber(Context context) {List<ContactsBean> contacts = new ArrayList<>();String sortOrder = ContactsContract.CommonDataKinds.Phone.SORT_KEY_PRIMARY;Cursor cursor = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, sortOrder);if (cursor != null) {while (cursor.moveToNext()) {List<String> allNumber =new ArrayList<>();ContactsBean info = new ContactsBean();String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));String firstChar = cursor.getString(cursor.getColumnIndex(PHONE_BOOK_LABEL));int numberCount = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));if (numberCount > 0) {Cursor phoneNumberCursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[]{id}, null);while (phoneNumberCursor.moveToNext()) {String phoneNumber = phoneNumberCursor.getString(phoneNumberCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));if (!allNumber.contains(phoneNumber)) {allNumber.add(phoneNumber);info.setPhoneList(allNumber);}}if (phoneNumberCursor != null) {phoneNumberCursor.close();}}info.setFullName(name);//添加名字info.setNamePrefix(firstChar.toLowerCase());//首字母info.setPhoneList(allNumber);//所有号码contacts.add(info);}cursor.close();}return contacts;}
}
3.主函数方法调用即可
List<ContactsBean> list=ContactUtils.getAllContacts(getActivity());