Code:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.pim.*;
import java.util.Enumeration;
public class AddContact extends MIDlet implements CommandListener,Runnable
{
Form form;
Display display;
Command exit;
Command add;
Alert alert;
public Thread th;
private boolean quit = false;
public AddContact()
{
display = Display.getDisplay(this);
form = new Form("Check PIM support");
exit = new Command("Exit",Command.EXIT,1);
add = new Command("Add Contact",Command.OK,1);
alert = new Alert("Add Contact","Contact Added Succesfully",null,null);
alert.setTimeout(Alert.FOREVER);
form.addCommand(add);
form.addCommand(exit);
form.setCommandListener(this);
}
public void startApp()
{
display.setCurrent(form);
th = new Thread(this);
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
public void commandAction(Command c, Displayable d) {
if(c==exit)
{
destroyApp(false);
notifyDestroyed();
}
else if (c == add)
{
if(!quit)
{
th.start();
display.setCurrent(alert);
}
}
}
public void quit()
{
quit = true;
}
public void run()
{
while (!quit)
{
try
{
PIM pim = PIM.getInstance();
ContactList cl = null;
Contact new_contact = null;
Enumeration read_contact = null;
String final_no="";
try
{
cl = (ContactList)pim.openPIMList(PIM.CONTACT_LIST,PIM.READ_WRITE);
}
catch(PIMException pime)
{
Alert alert_1 = new Alert("Error","Error Accessing Database"+pime,null,null);
alert_1.setTimeout(Alert.FOREVER);
alert_1.setType(AlertType.ERROR);
display.setCurrent(alert_1);
}
catch(SecurityException se)
{
Alert alert_2 = new Alert("Error","Security Error"+se,null,null);
alert_2.setTimeout(Alert.FOREVER);
alert_2.setType(AlertType.ERROR);
display.setCurrent(alert_2);
}
new_contact = cl.createContact();
new_contact.addString(Contact.TEL,Contact.ATTR_HOME,"123456");
new_contact.addString(Contact.FORMATTED_NAME,Contact.ATTR_NONE,"AasdD");
new_contact.addString(Contact.TEL,Contact.ATTR_MOBILE,"2634465");
new_contact.addString(Contact.TEL,Contact.ATTR_FAX,"+91 7834465");
new_contact.addString(Contact.TEL,Contact.ATTR_PAGER,"7034465");
new_contact.addString(Contact.TEL,Contact.ATTR_MOBILE,"9234465");
try
{
new_contact.commit();
}
catch(PIMException piem)
{
Alert alert_4 = new Alert("Error","Error while saving Data"+piem,null,null);
alert_4.setTimeout(Alert.FOREVER);
alert_4.setType(AlertType.ERROR);
display.setCurrent(alert_4);
}
try
{
cl.close();
}
catch(PIMException piem)
{
Alert alert_5 = new Alert("Error","Error Closing Application"+piem,null,null);
alert_5.setTimeout(Alert.FOREVER);
alert_5.setType(AlertType.ERROR);
display.setCurrent(alert_5);
}
}
catch (Exception e)
{
Alert alert_4 = new Alert("Error","Error on first try"+e,null,null);
alert_4.setTimeout(Alert.FOREVER);
alert_4.setType(AlertType.ERROR);
display.setCurrent(alert_4);
}
quit();
}
}
}
This application is simply add new contact in AddressBook.....
Is there any problem with code....
I am waititng for reply
wth regards
Pravin