首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2ME开发 >

读取手机的通讯录,该如何解决

2012-02-02 
读取手机的通讯录为什么下面的代码读去通讯录(已经存在),sun的wtk会提示问是否要读取通讯录?当我按下是的,

读取手机的通讯录
为什么下面的代码读去通讯录(已经存在),sun的wtk会提示问是否要读取通讯录?当我按下是的,而模拟器没有放映,
我用Nokia_S40_DP20_SDK_1_1把jad导入运行时,提示的却是cannotdefounderror为什么啊???
import   gov.nist.siplite.header.ContactList;

import   java.io.*;
import   java.util.Enumeration;

import   javax.microedition.lcdui.*;
import   javax.microedition.midlet.*;
import   javax.microedition.pim.Contact;
import   javax.microedition.pim.PIM;
import   javax.microedition.pim.PIMException;
import   javax.microedition.pim.PIMList;

import   org.kxml.io.AbstractXmlWriter;
import   org.kxml.io.XmlWriter;

public   class   f   extends   MIDlet   implements   CommandListener  
  {

          private   Display       display;      
  private       Command   exitCommand,okCommand;
//   private   TextBox   textbox;
  private   Alert   alert;
  public   String   tagTable[]   =   {
                  "id ",
              "firstname ",
              "mun "
      };
        public   f(){
        display   =   Display.getDisplay(   this   );
       
       
        alert=new   Alert( "alert ", "Are   you   start   xml   form   contact? ",null,AlertType.INFO);
                exitCommand   =   new   Command(   "Exit ",     Command.EXIT,   1   );
                okCommand=new   Command( "statxml ",Command.OK,1);
         
              alert.addCommand(exitCommand);
              alert.addCommand(okCommand);
              alert.setCommandListener(this);
        }
        public   void   commandAction(   Command   c,   Displayable   d   ){
                if(   c   ==   exitCommand   )
                {    
                  destroyApp(   false);
                  notifyDestroyed();
                }else   if(c==okCommand)
                  {
                    try  
                    {
                            testXML();
                            }catch(   Exception   e   )
                              {
                                  System.err.println(   "Exception   "   +   e   );


                                }
                  }
               
        }

        protected   void   destroyApp(   boolean   unconditional   )  
        {
       
        }
        protected   void   pauseApp()
        {
        }

        protected   void   startApp()
        {
                display.setCurrent(alert);
                     
                     
               
        }

        private   void   log(   String   str   )
        {
                System.out.println(   str   );
        }

     

          private   void   testXML()   throws   IOException
          {      
          log( "starting   testxml ");
          StringBuffer       sb=new       StringBuffer();      
         

          try{
            PIM   pim   =   PIM.getInstance();
            ContactList   clist=null;
          try   {
        log( "stary   open ");
       
            clist   =   (ContactList)   pim.openPIMList(PIM.CONTACT_LIST,
            PIM.READ_ONLY   );
            log( "end   open ");
            }   catch(Exception   e)   {
            sb.append( "{ "+e+ "} ");
            }
       
          log( "start   congt ");
            Contact   contact   =   null;
                Enumeration   items   =   ((PIMList)   clist).items();
            while(items.hasMoreElements())   {
         
            contact   =   (Contact)(items.nextElement());
            int[]   fields   =   contact.getFields();
          for(int   i   =   0;   i   <   fields.length;   i++)  
          {


                int   fieldIndex   =   fields[i];
                int   dataType   =   ((PIMList)   clist).getFieldDataType(fieldIndex);
         
                sb.append( "   Field   "   +   fieldIndex   +   ":   "+((PIMList)   clist).getFieldLabel(fieldIndex)+ "   -   data   type:   "   +   dataType);
                sb.append( "\n ");
         
                for(int   j   =   0;   j   <   contact.countValues(fieldIndex);   j++)  
                      {
                int   attr   =   contact.getAttributes(fieldIndex,   j);
                        try
                {
                          sb.append( "( "+((PIMList)   clist).getAttributeLabel(attr)   +   "):   ");
                          sb.append(contact.getString(fieldIndex,   j));
                          sb.append( "\n ");
                          }catch(Exception   e)
                            {
                                sb.append( ": "+e+ ": ");
                                sb.append( "\n ");
                          }
                }
            log(sb.toString());
               
         
          }
       
          }
         
        }catch(Exception   pe)   {  
              pe.printStackTrace();
    }

}
       
}
   
            /*   log(     "Writing   uncompressed   XML   to   a   string... "   );
                            ByteArrayOutputStream   out   =         new   ByteArrayOutputStream();
                            XmlWriter   xw   =   new   XmlWriter(     new   OutputStreamWriter(   out   )   );


                            AbstractXmlWriter   writer=(   AbstractXmlWriter   )xw;
                            PIM   pim   =   PIM.getInstance();  
                    ContactList   list   =   null;    
                    try   {  
                            log( "starting   ddc ");
                            list   =   (ContactList)pim.openPIMList(PIM.CONTACT_LIST,PIM.READ_ONLY);  
                            log( "ending   ddc ");
                    }catch(PIMException   pe){  
                    log( "ddc   catch ");
                          pe.printStackTrace();
                    }catch(SecurityException   se){  
                    log( "ddc   catch2 ");
                            se.printStackTrace();
                    }  
                    try   {  
                    log( "starting   enumeratong ");
                            Enumeration   items   =   ((PIMList)   list).items();
                            while(items.hasMoreElements())   {  
                                    Contact   contact   =   (Contact)items.nextElement();  
                                    writer.startTag( "id ");
                                    writer.startTag(   "firstname "   );
                                    int     field=contact.NAME   ;
                                    int   index=contact.getPreferredIndex(   field)   ;
                                    String   temp=contact.getString(field,   index);
                                    writer.write(   temp   );


                                    writer.endTag();
                                    writer.startTag(   "mun "   );
                                    int   field2=contact.TEL;
                                    int   index2=contact.getPreferredIndex(   field)   ;
                                    String   temp2=contact.getString(field2,   index2);
                                    writer.write(   temp2);
                                    writer.endTag();
                                    writer.endTag();
                                    writer.close();
                            }  
                    }catch(PIMException   pe)   {  
                                  pe.printStackTrace();
                    }catch(SecurityException   se)   {  
                    se.printStackTrace();
                    }  
                            String   str   =   new   String(   out.toByteArray()   );
                            log(   "String   length   is   "   +   str.length()   );
                            log(   "String   value   is "   );
                            log(   str   );
                            log(   "Writing   compressed   XML   to   a   string... "   );
                            out   =   new   ByteArrayOutputStream();
                       
                  */


[解决办法]
重新起一个线程去做查询工作,而不要把他放到commandAction中
[解决办法]
拉把椅子上帮 "CHINADENG() "顶,


呵呵
[解决办法]
模拟器中默认是没有任何数据的,你先添加一些联系人的数据到里面再读取
或者你到%WTK_HOME%\appdb\Nokia_S40_DP20_SDK_1_1\pim\contacts\Contacts目录下看有没有数据,如果没有,查询时候就的null回来,如果有还是查询不出来,可能就是程序的问题了,或者是由于j2me的安全问题,限制了你去读取敏感数据

热点排行