获取手机通讯录跟sim卡通讯录
??? /** 獲取庫Phone表字段 **/
?? ?private static final String[] PHONES_PROJECTION = new String[] { Phone.DISPLAY_NAME, Phone.NUMBER };
?? ?private List<ContactsModel> contactsModels = new ArrayList<ContactsModel>();
/** 得到手機通訊錄聯系人信息 **/
?? ?private void getPhoneContacts() {?? ??? ?ContentResolver resolver = mContext.getContentResolver();
?? ??? ?// 獲取手機聯系人
?? ??? ?Cursor phoneCursor = resolver.query(Phone.CONTENT_URI, PHONES_PROJECTION, null, null, null);
?? ??? ?if (phoneCursor != null) {
?? ??? ??? ?while (phoneCursor.moveToNext()) {
?? ??? ??? ??? ?ContactsModel contactsModel = new ContactsModel();
?? ??? ??? ??? ?String contactName = phoneCursor.getString(0);
?? ??? ??? ??? ?// 得到手機號碼
?? ??? ??? ??? ?String phoneNumber = phoneCursor.getString(1);
?? ??? ??? ??? ?// 當手機號碼為空的或者為空字段 跳過當前循環
?? ??? ??? ??? ?if (TextUtils.isEmpty(phoneNumber))
?? ??? ??? ??? ??? ?continue;
?? ??? ??? ??? ?// 得到聯系人名稱
?? ??? ??? ??? ?contactsModel.setContactName(contactName);
?? ??? ??? ??? ?contactsModel.setPhoneNumber(phoneNumber);
?? ??? ??? ??? ?contactsModels.add(contactsModel);
?? ??? ??? ?}
?? ??? ??? ?phoneCursor.close();
?? ??? ?}
?? ?}
?? ?/** 得到手機SIM卡聯系人人信息 **/
?? ?private void getSIMContacts() {
?? ??? ?ContentResolver resolver = mContext.getContentResolver();
?? ??? ?// 獲取Sims卡聯系人
?? ??? ?Uri uri = Uri.parse("content://icc/adn");
?? ??? ?Cursor phoneCursor = resolver.query(uri, PHONES_PROJECTION, null, null, null);
?? ??? ?if (phoneCursor != null) {
?? ??? ??? ?while (phoneCursor.moveToNext()) {
?? ??? ??? ??? ?ContactsModel contactsModel = new ContactsModel();
?? ??? ??? ??? ?// 得到手機號碼
?? ??? ??? ??? ?String contactName = phoneCursor.getString(0);
?? ??? ??? ??? ?String phoneNumber = phoneCursor.getString(1);
?? ??? ??? ??? ?// 當手機號碼為空的或者為空字段 跳過當前循環
?? ??? ??? ??? ?if (TextUtils.isEmpty(phoneNumber))
?? ??? ??? ??? ??? ?continue;
?? ??? ??? ??? ?contactsModel.setContactName(contactName);
?? ??? ??? ??? ?contactsModel.setPhoneNumber(phoneNumber);
?? ??? ??? ??? ?contactsModels.add(contactsModel);
?? ??? ??? ?}
?? ??? ??? ?phoneCursor.close();
?? ??? ?}
?? ?}
聯系人model類
public class ContactsModel {
?? ?private String contactName;
?? ?private String phoneNumber;
?? ?public String getContactName() {
?? ??? ?return contactName;
?? ?}
?? ?public void setContactName(String contactName) {
?? ??? ?this.contactName = contactName;
?? ?}
?? ?public String getPhoneNumber() {
?? ??? ?return phoneNumber;
?? ?}
?? ?public void setPhoneNumber(String phoneNumber) {
?? ??? ?this.phoneNumber = phoneNumber;
?? ?}
}
總結
以上是生活随笔為你收集整理的获取手机通讯录跟sim卡通讯录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Cknife与一句话木马提权
- 下一篇: intellij idea 导出可执行j