• Android从SIM卡中获取联系人


    最近做一个功能,需要同步本地联系人信息,主要从SIM卡和android通信录得到

    SIM卡获取联系人,由于SIM卡容量有限,它只存储了联系人的名字和号码

    View Code
      1  private static final int NAME_COLUMN = 0;
    2
    3 private static final int NUMBER_COLUMN = 1;
    4
    5 private static final String SURI = "content://icc/adn";
    6
    7 private Context context = null;
    8
    9 private ContentResolver resolver = null;
    10
    11 private TelephonyManager telMgr = null;
    12
    13 public SimHelper(Context context)
    14 {
    15 this.context = context;
    16 resolver = context.getContentResolver();
    17 }
    18
    19 public TelephonyManager getTelephonyManager()
    20 {
    21 if (telMgr == null)
    22 {
    23 telMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    24 }
    25 return telMgr;
    26 }
    27
    28 public int getSimCardState()
    29 {
    30 return getTelephonyManager().getSimState();
    31 }
    32
    33 public List<ContactInfo> retrieveContactInfoFromSIM()
    34 {
    35 List<ContactInfo> simContactInfoList = new ArrayList<ContactInfo>();
    36 Uri uri = Uri.parse(SURI);
    37 Cursor cursor = resolver.query(uri, null, null, null, null);
    38 for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
    39 {
    40 ContactInfo contacInfo = new ContactInfo();
    41 final NamePhoneTypePair namePhoneTypePair = new NamePhoneTypePair(
    42 cursor.getString(NAME_COLUMN));
    43 final String name = namePhoneTypePair.name;
    44 final int phoneType = namePhoneTypePair.phoneType;
    45 final String phoneNumber = cursor.getString(NUMBER_COLUMN);
    46 setPhoneInfo(contacInfo, phoneType, phoneNumber);
    47 contacInfo.setName(name);
    48 simContactInfoList.add(contacInfo);
    49 }
    50 cursor.close();
    51
    52 return simContactInfoList;
    53 }
    54
    55 private void setPhoneInfo(ContactInfo contactInfo, int phoneType,
    56 String phoneNumber)
    57 {
    58 Phone phone = new Phone();
    59 phone.setType(PhoneType.valueOf(phoneType));
    60 phone.setNumber(phoneNumber);
    61 contactInfo.getPhones().add(phone);
    62 }
    63
    64 private static class NamePhoneTypePair
    65 {
    66 final String name;
    67
    68 final int phoneType;
    69
    70 public NamePhoneTypePair(String nameWithPhoneType)
    71 {
    72 // Look for /W /H /M or /O at the end of the name signifying the type
    73 int nameLen = nameWithPhoneType.length();
    74 if (nameLen - 2 >= 0
    75 && nameWithPhoneType.charAt(nameLen - 2) == '/')
    76 {
    77 char c = Character.toUpperCase(nameWithPhoneType.charAt(nameLen - 1));
    78 if (c == 'W')
    79 {
    80 phoneType = PhoneType.Company.getPhoneType();
    81 }
    82 else if (c == 'M' || c == 'O')
    83 {
    84 phoneType = PhoneType.Mobile.getPhoneType();
    85 }
    86 else if (c == 'H')
    87 {
    88 phoneType = PhoneType.Home.getPhoneType();
    89 }
    90 else
    91 {
    92 phoneType = PhoneType.Other.getPhoneType();
    93 }
    94 name = nameWithPhoneType.substring(0, nameLen - 2);
    95 }
    96 else
    97 {
    98 phoneType = PhoneType.Other.getPhoneType();
    99 name = nameWithPhoneType;
    100 }
    101 }
    102 }

    看Android apps中Phone得源码,似乎可以拿到联系人电话类型,但我试过没有拿到,具体原因还不清楚。

    如何同步Android通信录的联系人,可以参考源码。

  • 相关阅读:
    第十五章:字段与属性
    第二十章:封装与继承
    第二十一章;泛型List
    第十九章:对象初始化器
    第十八章:构造方式
    第十七章:方法的重载
    request和response对象常用方法
    面向对象(1)
    request和response
    tomcat
  • 原文地址:https://www.cnblogs.com/zhuqiang/p/2298080.html
Copyright © 2020-2023  润新知