Categories: Java | Java ME | PIM | How To | Code Examples
This page was last modified 12:31, 13 December 2007.
How to add contacts using JSR 75
From Forum Nokia Wiki
Following code snippets show how to add contacts using JSR 75.
/** * PIMWrite Class * This sample demo generates the random names and numbers and it will add to the phonebooks. * This code will be useful, if we want to add more contacts to our phonebooks for testing. * if we need to see the performance, of PhoneBooks contacts writing and reading speed * This code will be useful * @author Senthil * */ import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.util.*; public class PIMWrite extends MIDlet implements CommandListener { private Command PIMWriteCmd,exitCommand,generateName; PIMImpl pimImpl; private static Random random = new Random(); static String[] names; public static Display display; public static Form result; private String sp,si,s1; private String[] strJSR = new String[12]; private long l; int noOfContacts; TextField textfield = new TextField("Enter No of Contacts: ","",3,TextField.NUMERIC); /*Constructs an Object */ public PIMWrite() { display = Display.getDisplay(this); PIMWriteCmd = new Command("PIMWrite", Command.SCREEN, 0); exitCommand = new Command("Exit", Command.SCREEN, 0); result=new Form("Write Contacts To PIM"); result.append(textfield); System.out.println(" noOfContacts: "+noOfContacts); result.addCommand(PIMWriteCmd); result.addCommand(exitCommand); result.setCommandListener(this); } public void startApp() throws MIDletStateChangeException { display.setCurrent(result); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); } else if(c == PIMWriteCmd) { noOfContacts = Integer.parseInt(textfield.getString()); createConatactName(); System.out.println(" noOfContacts: "+noOfContacts); //Start the PIM thread and write contacts . pimImpl = new PIMImpl(); pimImpl.start(); } } public void createConatactName() { names = new String[noOfContacts]; for (int i = 0; i < noOfContacts; i++){ names[i] = PIMWrite.generateName()+" "+PIMWrite.generateName(); System.out.println(" Names :"+ names[i]+ " i "+i); } } public static String generateName() { return generateName(4, 7, true); } /** This method generates the random names * * @param minLength Minimum length of name * @param maxLength Max length of name * @param capitalize Captilize * @return name */ public static String generateName(int minLength, int maxLength, boolean capitalize) { int limit = (random.nextInt(maxLength - minLength + 1)) + minLength; StringBuffer s = new StringBuffer(limit); for (int i = 0; i < limit; i++) { char c = (char)((random.nextInt(26)) + 'a'); s.append(c); } if (capitalize && (s.length() > 0)) s.setCharAt(0, Character.toUpperCase(s.charAt(0))); return s.toString(); } }
PIM Implementation class
/** * PIM Implementation class */ import javax.microedition.pim.*; import java.util.*; import javax.microedition.lcdui.*; import javax.microedition.lcdui.Command; public class PIMImpl extends Thread { static Alert check; StringItem si; //----------------------------------------Phone Book--------------------------------- int noOfRecords; public static boolean pimTemp = false; Random randPh = new Random(); public PIMImpl() { } public void run() { writeData(); } public synchronized void writeData() { String ph[] = new String[PIMWrite.names.length]; for (int i=0;i<PIMWrite.names.length ;i++ ) { String r = Long.toString(randPh.nextLong()); if(r.startsWith("-")) { r = r.replace('-','9'); } ph[i] =(r.length()>10)?(r.substring(0,10)):(r); // System.out.println(" PH :"+ph[i] +"i:"+i); } int tc = 0; PIM pim; try{ ContactList contacts = null; ContactList conT = null; pim = PIM.getInstance(); try { contacts = (ContactList) pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE); } catch (PIMException e) { // An error occurred return; } for(int i=0;i<PIMWrite.names.length;i++) { Contact contact = contacts.createContact(); String[] name = new String[contacts.stringArraySize(Contact.NAME)]; // System.out.println("i = "+i); if (contacts.isSupportedField(Contact.TEL)) { contact.addString(Contact.TEL, Contact.ATTR_MOBILE, ph[i]); } if (contacts.isSupportedArrayElement(Contact.NAME, Contact.NAME_GIVEN)) name[Contact.NAME_GIVEN] = PIMWrite.names[i]; contact.addStringArray(Contact.NAME, PIMItem.ATTR_NONE, name); try { contact.commit(); } catch (PIMException e) { // An error occured } PIMWrite.result.deleteAll(); String msg=" Contacts Adding :"+i; si = new StringItem("",msg); PIMWrite.result.append(si); } check = new Alert("Added","Added ",null,AlertType.CONFIRMATION); check.setTimeout(Alert.FOREVER); PIMWrite.display.setCurrent(check); try { contacts.close(); } catch (PIMException e) { } }catch(Exception e){ String er = e+" ; tc="+tc; check = new Alert( "Response",er,null,AlertType.CONFIRMATION ); check.setTimeout(Alert.FOREVER); PIMWrite.display.setCurrent(check); } // return true; } }
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Using images in Contacts | evigilo | Python | 0 | 2006-01-23 16:04 |
| How to back up contacts | JamesChy | General Symbian C++ | 1 | 2006-06-22 07:44 |
| Add mass advanced detail for contacts database | Native-Patriot | General Symbian C++ | 3 | 2008-02-11 14:49 |
| I have matlab j2me version | hpienergy | Mobile Java General | 1 | 2006-03-17 19:59 |
| Python crashes on e61 when openining contacts | jagoncalves | Python | 21 | 2006-11-29 07:08 |
