android获取手机的所有通讯录的号码和sim卡号码

时间:2023-03-09 09:02:51
android获取手机的所有通讯录的号码和sim卡号码

============personer================================================

package com.qgc.cantent.entity;

public class Personer {
    private int personId;
    private String personName;
    private String phone;
 public int getPersonId() {
  return personId;
 }
 public Personer(){}
 public Personer( String personName, String phone) {

this.personName = personName;
  this.phone = phone;
 }

public Personer(int personId, String personName,String phone) {
  super();
  this.personId = personId;
  this.personName = personName;
  this.phone = phone;
 }
 public void setPersonId(int personId) {
  this.personId = personId;
 }
 public String getPersonName() {
  return personName;
 }
 public void setPersonName(String personName) {
  this.personName = personName;
 }
 public String getPhone() {
  return phone;
 }
 public void setPhone(String phone) {
  this.phone = phone;
 }

}

================activity=======================

// 获所有手机号码
   public List<Personer> getPhoneContant() {

// 取得ContentResolver
    List<Personer> list = new ArrayList<Personer>();
    ContentResolver content = getContentResolver();

// 联系人的URI
    Cursor cursor = content
      .query(Phone.CONTENT_URI, null, null, null, null);

// int contactCount = cursor.getCount(); // 获得联系人数目
    if (cursor != null) {

while (cursor.moveToNext()) {
      int columId = cursor
        .getColumnIndex(ContactsContract.Contacts._ID);// id下标
      int displayNameColum = cursor
        .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);// 名称下标
      // 个数
      int phoneNo = cursor
        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);// 电话号码列

// 获得联系人的ID号
      String contactId = cursor.getString(columId);
      // 获得联系人姓名
      String disPlayName = cursor.getString(displayNameColum);

String phonenum = cursor.getString(phoneNo);// 号码
      Personer p = new Personer(Integer.parseInt(contactId),
        disPlayName, phonenum);
      list.add(p);
      // 电话号码的个数

}
    }
    return list;

}

// 获所有SIM卡号码
   @SuppressWarnings("deprecation")
   public List<Personer> getSimContant() {

// 取得ContentResolver
    List<Personer> list = new ArrayList<Personer>();
    // 联系人的URI
    Uri uri = Uri.parse("content://icc/adn");

Cursor cursor = this.getContentResolver().query(uri, null, null,

null, null);
    if (cursor != null) {
     while (cursor.moveToNext()) {
      int columId = cursor.getColumnIndex(People._ID);// id下标
      int displayNameColum = cursor.getColumnIndex(People.NAME);// 名称下标
      // 个数
      int phoneNo = cursor.getColumnIndex(People.NUMBER);// 电话号码列
      // 获得联系人的ID号
      String contactId = cursor.getString(columId);
      // 获得联系人姓名
      String disPlayName = cursor.getString(displayNameColum);

String phonenum = cursor.getString(phoneNo);// 号码
      Personer p = new Personer(Integer.parseInt(contactId),
        disPlayName, phonenum);
      list.add(p);
     }
    }
    return list;

}