Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

Revision as of 06:59, 4 June 2009 by tanjaluodes (Talk | contribs)

How to add contacts using JSR 75

From Wiki
Jump to: navigation, search

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;
 
}
 
}
45 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×