J2ME获取sim联系人

时间:2013-02-16 04:15:50

标签: java-me

如何使用J2ME中的PIM检索SIM卡和电话簿联系人。 我试过了 。PIM.getInstance()listPIMLists(PIM.CONTACT_LIST); 但它只显示电话簿联系。

1 个答案:

答案 0 :(得分:0)

可能这种方法可以帮到你

public static HashLongObject loadContactFromPhone() {

    PIM iPim = PIM.getInstance();
    String[] allContactLists = iPim.listPIMLists(PIM.CONTACT_LIST);
    // Phone or SIM
    HashLongObject iPhoneBookList = new HashLongObject();
    int i;
    for (i = 0; i < allContactLists.length; i++) {
        try {
            PIMList iPIMList = iPim.openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY, allContactLists[i]);
            Enumeration iPimListEnum = iPIMList.items();
            String iContactName, iTelNumber;
            String []arrName;
            boolean isSupportFormettedName = iPIMList.isSupportedField(Contact.FORMATTED_NAME);
            if(isSupportFormettedName) {
                while (iPimListEnum.hasMoreElements()) {
                    try {
                        Contact iContact = (Contact) iPimListEnum.nextElement();
                        iContactName = iContact.getString(Contact.FORMATTED_NAME, 0);
                        iTelNumber = iContact.getString(Contact.TEL, 0);
                    } catch (Exception e) {
                        Logger.logStackTrace(e);
                        continue;
                    }

                    long corePhoneNumber = StringUtils.toCCPhoneNumber(iTelNumber);

                    // Check Duplicate
                    if (iPhoneBookList.containsKey(corePhoneNumber)) {
                        continue;
                    }

                    iPhoneBookList.put(corePhoneNumber, iContactName);
                }
            } else {
                while (iPimListEnum.hasMoreElements()) {
                    try {
                        Contact iContact = (Contact) iPimListEnum.nextElement();
                        arrName = iContact.getStringArray(Contact.NAME, Contact.ATTR_NONE);
                        iContactName = "";
                        if(arrName[Contact.NAME_FAMILY] != null) {
                            iContactName += arrName[Contact.NAME_FAMILY];
                        }
                        if(arrName[Contact.NAME_GIVEN] != null) {
                            iContactName += arrName[Contact.NAME_GIVEN];
                        }
                        iTelNumber = iContact.getString(Contact.TEL, 0);
                    } catch (Exception e) {
                        Logger.logStackTrace(e);
                        continue;
                    }

                    long corePhoneNumber = StringUtils.toCCPhoneNumber(iTelNumber);

                    // Check Duplicate
                    if (iPhoneBookList.containsKey(corePhoneNumber)) {
                        continue;
                    }

                    iPhoneBookList.put(corePhoneNumber, iContactName);
                }
            }
        } catch (PIMException ex) {
            Logger.logStackTrace(ex);
        } catch (Exception otherEx) {
            Logger.logStackTrace(otherEx);
        }
    }
    return iPhoneBookList;
}