2

How to retrieve both SIM and phone book contact using PIM in J2ME. I Tried PIM.getInstance().listPIMLists(PIM.CONTACT_LIST); but it's only displaying Phone book contact.

jaya
  • 51
  • 1
  • 3
  • possible duplicate of [Can you access sim contacts from J2ME?](http://stackoverflow.com/questions/4955323/can-you-access-sim-contacts-from-j2me) – Nate Feb 16 '13 at 05:19
  • The call to listPIMLists is only returning one list? – Telmo Pimentel Mota Feb 16 '13 at 21:04
  • yes it is only returning one list that is phone book contacts. When I am trying to read sim contacts using simaddressbook=(javax.microedition.pim.ContactList)(PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE,"SIM")); but its giving null pointer exception – jaya Feb 18 '13 at 10:56
  • I have tried to run the app on a few J2ME supportive phones. As Nokia N17 and Samsung C2-02, both are reading phone book contacts. But when I tried to run it on Nokia E5, its displaying nothing. – jaya Feb 18 '13 at 11:04

1 Answers1

0

May be this method can help you

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;
}
Thanh Le
  • 763
  • 4
  • 13