java left logo
java middle logo
java right logo
 

Home arrow Java ME Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 3918
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to import Contact into phonebook E-mail
User Rating: / 21
PoorBest 

The example below create a contact in phonebook of mobile and user can view it in a VCard format.

import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.pim.*;

public class ImportContactMIDlet  extends MIDlet implements CommandListener {
    
}
    private final Form form;
    private final Display display;
    private final Command exitCommand, saveCommand;
    TextField name,address,nickname,number,email,url;
    DateField birthday;
    
    public ImportContactMIDlet() {
        
        saveCommand = new Command("New Contacts", Command.OK, 0);
        exitCommand = new Command("Exit", Command.EXIT, 1);
        form = new Form("saveCommand");
        number = new TextField("Tel","",20,TextField.PHONENUMBER);
        name = new TextField("Name","",20,TextField.ANY);
        address = new TextField("Address","",100,TextField.ANY);
        nickname = new TextField("NickName","",20,TextField.ANY);
        email = new TextField("Email","",50,TextField.EMAILADDR);
        url = new TextField("URL","",100,TextField.URL);
        birthday = new DateField("Birthdate", DateField.DATE, 
                TimeZone.getTimeZone("GMT"));
        
        birthday.setDate(new Date());
        form.append(name);
        form.append(number);
        form.append(address);
        form.append(email);
        form.append(nickname);
        form.append(birthday);
        form.append(url);
        form.addCommand(saveCommand);
        form.addCommand(exitCommand);
        form.setCommandListener(this);
        display = Display.getDisplay(this);
        
    }
    
    public void startApp() {
        
        Displayable current = Display.getDisplay(this).getCurrent();
        if (current == null) {
            // check that the API is available
            boolean isAPIAvailable =(
                    System.getProperty("microedition.pim.version"!= null);
            
            StringBuffer sbuf = new StringBuffer(
                    getAppProperty("MIDlet-Name"));
            
            sbuf.append("\n").append(getAppProperty("MIDlet-Vendor")).
                    append(isAPIAvailable?"":"\nPIM API not available");
            
            Alert alertScreen = new Alert(null, sbuf.toString(), null, 
                    AlertType.INFO);
            
            alertScreen.setTimeout(3000);
            
            if (!isAPIAvailable) {
                display.setCurrent(splashScreen);
            else {
                display.setCurrent(form);
            }
            
        else {
            Display.getDisplay(this).setCurrent(current);
        }
    }
    
    public void pauseApp(){}
    
    public void destroyApp(boolean unconditional){}
    
    void showMessage(String message, Displayable displayable) {
        
        Alert alert = new Alert("");
        alert.setTitle("Error");
        alert.setString(message);
        alert.setType(AlertType.ERROR);
        alert.setTimeout(5000);
        display.setCurrent(alert, displayable);
        
    }
    
    void showMain() {
        display.setCurrent(form);
    }
    
    void saveContact() {
        display.setCurrent(new AddContactForm(ImportContactMIDlet.this));
    }
    
    public void commandAction(Command cmd, Displayable displayable) {
        
        if (cmd == exitCommand) {
            
            notifyDestroyed();
            
        else if (cmd == saveCommand) {
            
            if(name.getString() == null || name.getString().length() == 0) {
                
                showMessage("Name required",form);
                
            else {
                
                saveContact();
                
            }
        }
    }
}

import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.pim.*;
import java.io.ByteArrayOutputStream;

class AddContactForm  extends Form  implements CommandListener {
    
    private final Command exitCommand, showCommand, backCommand;
    private final ImportContactMIDlet parent;
    private PIMItem item;
    
    public AddContactForm(ImportContactMIDlet parent) {
        
        super("Contacts");
        this.parent = parent;
        
        showCommand = new Command("Show VCard", Command.OK , 0);
        backCommand = new Command("Back", Command.BACK, 1);
        exitCommand = new Command("Exit", Command.EXIT, 1);
        
        addCommand(showCommand);
        addCommand(backCommand);
        addCommand(exitCommand);
        setCommandListener(this);
        SaveContacts l = new SaveContacts();
        
    }
    
    public void commandAction(Command cmd, Displayable displayable) {
        
        if (cmd == showCommand) {
            
            showVCard();
            
        else if (cmd == backCommand) {
            
            parent.name.setString("");
            parent.address.setString("");
            parent.email.setString("");
            parent.url.setString("");
            parent.nickname.setString("");
            parent.number.setString("");
            parent.birthday.setDate(new Date());
            parent.showMain();
            
        else if (cmd == exitCommand) {
            
            parent.notifyDestroyed();
            
        }
    }
    
    public void setItem(PIMItem item) {
        this.item = item;
    }
    
    public PIMItem getItem() {
        return item;
    }
    
    public void showVCard() {
        
        PIMItem it = getItem();
        
        try {
            
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            PIM.getInstance().toSerialFormat(it, baos, "UTF-8""VCARD/2.1");
            String s = new String(baos.toByteArray()"UTF-8");
            parent.showMessage(s, AddContactForm.this);
            
        catch (Exception e) {
            
            e.printStackTrace();
            parent.showMessage(e.getMessage(), AddContactForm.this);
            
        }
    }
    
    private class SaveContacts implements Runnable {
        
        public SaveContacts() {
            new Thread(this).start();
        }
        
        public void run() {
            
            try {
                ContactList contacts = null;
                try {
                    contacts = (ContactListPIM.getInstance().openPIMList(
                            PIM.CONTACT_LIST, PIM.READ_WRITE);
                    
                catch (PIMException e) {
                    // An error occurred
                    return;
                }
                Contact contact = contacts.createContact();
                
                try {
                    contact.addString(Contact.FORMATTED_NAME, 
                            PIMItem.ATTR_NONE, parent.name.getString());
                    
                    contact.addString(Contact.FORMATTED_ADDR, 
                            PIMItem.ATTR_NONE, parent.address.getString());
                    
                    contact.addString(Contact.TEL, Contact.ATTR_HOME, 
                            parent.number.getString());
                    
                    contact.addToCategory("Personal");
                    
                    contact.addDate(Contact.BIRTHDAY, PIMItem.ATTR_NONE, 
                            parent.birthday.getDate().getTime());
                    
                    contact.addString(Contact.EMAIL, 
                            Contact.ATTR_HOME | Contact.ATTR_PREFERRED, 
                            parent.email.getString());
                    
                    contact.addString(Contact.URL, 
                            Contact.ATTR_HOME | Contact.ATTR_PREFERRED, 
                            parent.url.getString());
                    
                catch (UnsupportedFieldException e) {
                    return;
                }
                
                try {
                    contact.commit();
                    setItem(contact);
                catch (PIMException e) {// An error occured}
                    try {
                        contacts.close();
                    catch (PIMException e) {}
                catch (PIMException e) {
                    
                    parent.showMessage(e.getMessage()
                            AddContactForm.this);
                    
                catch (SecurityException e) {
                    
                    parent.showMessage(e.getMessage()
                            AddContactForm.this);
                    
                }
            }
        }
    }


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.