在J2ME中从电话簿加载联系人

时间:2011-11-17 08:15:56

标签: java java-me contact jsr75

我在下面实施了此代码,以便从手机的地址簿中读取联系人

问题是,如果电话簿中的所有号码都保存在SIM卡中,它会读取并显示联系人以供选择。
但是如果手机内存中包含任何数字,则会出现应用程序错误。(OutOfMemoryException) 我该怎么办(PS不介意那里的一些System.out.println语句。我用它们进行调试)

public void execute() {
        try {
// go through all the lists
            String[] allContactLists = PIM.getInstance().listPIMLists(PIM.CONTACT_LIST);

            if (allContactLists.length != 0) {
                for (int i = 0; i < allContactLists.length; i++) {
                    System.out.println(allContactLists[i] + " " + allContactLists[1]);
                    System.out.println(allContactLists.length);
                    loadNames(allContactLists[i]);
                    System.out.println("Execute() error");
                }
            } else {
                available = false;
            }
        } catch (PIMException e) {
            available = false;

        } catch (SecurityException e) {
            available = false;

        }
    }

private void loadNames(String name) throws PIMException, SecurityException {
        ContactList contactList = null;
        try {
//       ----    
//            System.out.println("loadErr1");
            contactList = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY, name);
//            System.out.println(contactList.getName());//--Phone Contacts or Sim Contacts

            // First check that the fields we are interested in are supported(MODULARIZE)
            if (contactList.isSupportedField(Contact.FORMATTED_NAME)
                    && contactList.isSupportedField(Contact.TEL)) {
//               ContactLst.append("Reading contacts...", null);
//                System.out.println("sup1");
                Enumeration items = contactList.items();
//                System.out.println("sup2");
                Vector telNumbers = new Vector();
                telNames = new Vector();
                while (items.hasMoreElements()) {
                    Contact contact = (Contact) items.nextElement();
                    int telCount = contact.countValues(Contact.TEL);
                    int nameCount = contact.countValues(Contact.FORMATTED_NAME);
//                    System.out.println(telCount);
//                    System.out.println(nameCount);
// we're only interested in contacts with a phone number
// nameCount should always be > 0 since FORMATTED_NAME is
// mandatory
                    if (telCount > 0 && nameCount > 0) {

                        String contactName = contact.getString(Contact.FORMATTED_NAME, 0);
// go through all the phone numbers
                        for (int i = 0; i < telCount; i++) {
                            System.out.println("Read Telno");
                            int telAttributes = contact.getAttributes(Contact.TEL, i);
                            String telNumber = contact.getString(Contact.TEL, i);
                            System.out.println(telNumber + " " + "tel");
// check if ATTR_MOBILE is supported
                            if (contactList.isSupportedAttribute(Contact.TEL, Contact.ATTR_MOBILE)) {
                                if ((telAttributes & Contact.ATTR_MOBILE) != 0) {
                                    telNames.insertElementAt(telNames, i);
                                    telNumbers.insertElementAt(telNumber, i);
                                } else {
                                    telNumbers.addElement(telNumber);
                                    telNames.addElement(telNames);
                                }
                            }
//                            else {
////                                telNames.addElement(contactName);
//                                telNumbers.addElement(telNumber);       
//                            }
                            System.out.println("telephone nos");
                        }
// Shorten names which are too long
                        shortenName(contactName, 20);

                        for (int i = 0; i <= telNumbers.size(); i++) {
                            System.out.println(contactName + " here " + telNames.size());
                            telNames.addElement(contactName);
                            System.out.println(telNames.elementAt(i) + " na " + i);
                        }
                        names = new String[telNames.size()];
                        for (int j = 0; j < names.length; j++) {
                            names[j] = (String) telNames.elementAt(j);
                            System.out.println(names[j] + "....");
                        }

//                        allTelNames.addElement(telNames);
                        System.out.println("cap :" + telNames.size() + " " + names.length);
//                        telNames.removeAllElements();
//                        telNumbers.removeAllElements();
                    }
                }
                available = true;
            } else {
//                ContactLst.append("Contact list required items not supported", null);
                available = false;
            }
        } finally {
// always close it
            if (contactList != null) {
                contactList.close();
            }
        }
    }

0 个答案:

没有答案