Question

My j2me application have the feature of messaging with contacts in phone and as well as on sim.I tested Application and running well on Nokia N70 M and Nokia C2-02 also. But not fetching contacts from Nokia E5 and and E72. Please let me know possibilities of errors where I am wrong , Giving you code here...

public void loadContacts()
{

    System.out.println("into show contacts1");
    try
    { 
    javax.microedition.pim.ContactList addressbook=null;
    javax.microedition.pim.ContactList simaddressbook=null;
    PIM pim = PIM.getInstance();
    System.out.println("into show contacts2");
    String[] allContactLists = PIM.getInstance().listPIMLists(PIM.CONTACT_LIST);
    System.out.println("into show contacts3");
    for(int ctr=0;ctr<allContactLists.length;ctr++)
    {
    pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY,allContactLists[ctr]);
    addressbook=(javax.microedition.pim.ContactList)(pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY));
    Contact contact = null;
        Enumeration items = null;
        items = addressbook.items();
        while (items.hasMoreElements()) {
            contact = (Contact) items.nextElement();
            int telCount = contact.countValues(Contact.TEL);
            int nameCount = contact.countValues(Contact.FORMATTED_NAME);
            if (telCount > 0 && nameCount > 0) {
                String contactName = contact.getString(Contact.FORMATTED_NAME, 0);
                    for (int i = 0; i < telCount; i++) {
                          String phoneNumber = contact.getString(Contact.TEL, i);
                         ContactFetcher contactfetcher=new ContactFetcher();
                          contactfetcher.setFotmattedName(contactName);
                          contactfetcher.settelName(phoneNumber);
                          PersistableManager pm=PersistableManager.getInstance();
                          pm.save(contactfetcher);
            }
            }
        }
    }
    if(!nokiaos)
    {
      simaddressbook=(javax.microedition.pim.ContactList)(PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE,"SIM"));
     Contact contact = null;
     Enumeration simitems=null;
     simitems=simaddressbook.items();
     while (simitems.hasMoreElements()) {
             System.out.println("into show contacts9");
            contact = (Contact) simitems.nextElement();
            int telCount = contact.countValues(Contact.TEL);
            int nameCount = contact.countValues(Contact.FORMATTED_NAME);
            if (telCount > 0 && nameCount > 0) {
                String contactName = contact.getString(Contact.FORMATTED_NAME, 0);
                    for (int i = 0; i < telCount; i++) {
                          String phoneNumber = contact.getString(Contact.TEL, i);
                          ContactFetcher contactfetcher=new ContactFetcher();
                          contactfetcher.setFotmattedName(contactName);

                          contactfetcher.settelName(phoneNumber);
                          PersistableManager pm=PersistableManager.getInstance();
                          pm.save(contactfetcher);
          }
        }
     }

    }


  }

              catch(Exception e)
              {
                  e.printStackTrace();
              }

}

Here I am detecting whether phone is s60 or s40 if s6o then only reading pim contacts in s60 and also sim contacts in s40. Thanks in advance...:)

Was it helpful?

Solution

I have resolved at last, what i missing some king of naming convention on which contacts are stored. Code is below like that :- Which running on Nokia E5 and E72 as well.

  public void loadContacts()
  {

    System.out.println("into show contacts1");
   try
    { 
    javax.microedition.pim.ContactList addressbook=null;
    javax.microedition.pim.ContactList simaddressbook=null;
    PIM pim = PIM.getInstance();
    System.out.println("into show contacts2");
    String[] allContactLists = PIM.getInstance().listPIMLists(PIM.CONTACT_LIST);
    System.out.println("into show contacts3");

    pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY,allContactLists[0]);
    addressbook=(javax.microedition.pim.ContactList)(pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY));
    Contact contact = null;
        Enumeration items = null;
        items = addressbook.items();
        if(addressbook.isSupportedField(Contact.FORMATTED_NAME) && addressbook.isSupportedField(Contact.TEL))
        {
             while (items.hasMoreElements()) {
             System.out.println("into show contacts9");
            contact= (Contact) items.nextElement();
            int telCount = contact.countValues(Contact.TEL);
            int nameCount = contact.countValues(Contact.FORMATTED_NAME);
            if (telCount > 0 && nameCount > 0) {
                String contactName = contact.getString(Contact.FORMATTED_NAME, 0);
                    for (int i = 0; i < telCount; i++) {
                          String phoneNumber = contact.getString(Contact.TEL, i);

                         ContactPerson.addElement(contactName);    
                         Telephone.addElement(phoneNumber);
                         Imagedata.addElement("");

                     }
                }
             }
        }
        else
        {
            while (items.hasMoreElements()) {
            contact = (Contact) items.nextElement();
            String contactName="";
            contactName = contactName+contact.getStringArray(Contact.NAME,
               Contact.ATTR_NONE)[Contact.NAME_FAMILY];

            if(contactName.equals("null"))
            {
                contactName = contact.getStringArray(Contact.NAME,
                        Contact.ATTR_NONE)[Contact.NAME_GIVEN];
            }
            String phoneNumber=contact.getString(Contact.TEL, Contact.ATTR_NONE);
             if (contactName!=null && phoneNumber!=null) 
             {           
                         ContactPerson.addElement(contactName);    
                         Telephone.addElement(phoneNumber);
                         Imagedata.addElement("");

             }
            }
        }


  if(!nokiaos)
    {
      simaddressbook=(javax.microedition.pim.ContactList)(PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE,"SIM"));
     Contact contactsim = null;
     Enumeration simitems=null;
     simitems=simaddressbook.items();
     if(simaddressbook.isSupportedField(Contact.FORMATTED_NAME) && simaddressbook.isSupportedField(Contact.TEL))
     {
         while (simitems.hasMoreElements()) {
             System.out.println("into show contacts9");
            contactsim= (Contact) simitems.nextElement();
            int telCount = contactsim.countValues(Contact.TEL);
            int nameCount = contactsim.countValues(Contact.FORMATTED_NAME);
            if (telCount > 0 && nameCount > 0) {
                String contactName = contactsim.getString(Contact.FORMATTED_NAME, 0);
                    for (int i = 0; i < telCount; i++) {
                          String phoneNumber = contactsim.getString(Contact.TEL, i);
                         ContactPerson.addElement(contactName);    
                         Telephone.addElement(phoneNumber);
                         Imagedata.addElement("simico.png");


                    }
             }
        }
     }
     else
     {
         while (simitems.hasMoreElements()) {
             System.out.println("into show contacts9");
            contactsim= (Contact) simitems.nextElement();
            String contactName = contactsim.getStringArray(Contact.NAME,
               Contact.ATTR_NONE)[Contact.NAME_GIVEN];

            String phoneNumber=contactsim.getString(Contact.TEL, Contact.ATTR_NONE);
             if (contactName != null && phoneNumber!=null) 
             {
                         ContactPerson.addElement(contactName);    
                         Telephone.addElement(phoneNumber);
                         Imagedata.addElement("simico.png");

             }
          }
        }
     }

 }

              catch(Exception e)
              {
                  e.printStackTrace();
              }
   try
   {
                        ContactFetcher contactfetcher=new ContactFetcher(); 
                         contactfetcher.setFotmattedName(ContactPerson);
                         contactfetcher.settelName(Telephone);
                         contactfetcher.setImage(Imagedata);
                         PersistableManager pm=PersistableManager.getInstance();
                         pm.save(contactfetcher); 
   }
   catch(Exception ex)
   {

   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top