1

我的 j2me 应用程序具有与电话和 sim 中的联系人进行消息传递的功能。我测试了应用程序,并且在诺基亚 N70 M 和诺基亚 C2-02 上也运行良好。但不能从诺基亚 E5 和 E72 获取联系人。请让我知道我错了的错误可能性,在这里给你代码......

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();
              }

}

在这里,我检测手机是 s60 还是 s40,如果 s6o 则只读取 s60 中的 pim 联系人和 s40 中的 sim 联系人。提前致谢...:)

4

1 回答 1

2

我终于解决了,我错过了一些存储联系人的命名约定之王。代码如下:- 也运行在诺基亚 E5 和 E72 上。

  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)
   {

   }
}
于 2013-05-17T12:59:08.580 回答