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: 4096
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 find contacts from PhoneBook of mobile phone E-mail
User Rating: / 29
PoorBest 

JSR75 provides facility of accessing native database like contacts, events and to-dos of phone.

The example below lists the contacts from phone book and on selection prints the phone number of selected contact.

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

public class ContactMIDlet  extends MIDlet implements CommandListener {
    
    private final Form form;
    private final Display display;
    private final Command exitCommand, findCommand;
    private TextField number;
    
    public ContactMIDlet() {
        
        findCommand = new Command("Find Contacts", Command.OK, 0);
        exitCommand = new Command("Exit", Command.EXIT, 1);
        form = new Form("Contact");
        form.addCommand(findCommand);
        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 showContactsList() {
        display.setCurrent(new ContactListForm(ContactMIDlet.this));
    }
    
    
    void contactSelected(String telephoneNumber) {
        this.setPhoneNumber(telephoneNumber);
        showMain();
    }
    
    void setPhoneNumber(String phoneNumber) {
        number = new TextField("Contact","",20,TextField.PHONENUMBER);
        number.setString(phoneNumber);
        form.append(number);
    }
    
    public void commandAction(Command cmd, Displayable displayable) {
        
        if (cmd == exitCommand) {
            notifyDestroyed();
        else if (cmd == findCommand) {
            showContactsList();
        }
    }
}

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

class ContactListForm  extends List  implements CommandListener {
    
    private final Command exitCommand, selectCommand, backCommand;
    private final ContactMIDlet parent;
    private boolean available;
    private Vector allTelNumbers = new Vector();
    
    public ContactListForm(ContactMIDlet parent) {
        
        super("Contacts", Choice.IMPLICIT);
        this.parent = parent;
        selectCommand = new Command("Select", Command.OK, 0);
        backCommand = new Command("Back", Command.BACK, 1);
        exitCommand = new Command("Exit", Command.EXIT, 1);
        addCommand(backCommand);
        addCommand(exitCommand);
        setCommandListener(this);
        setFitPolicy(Choice.TEXT_WRAP_ON);
        
        LoadContacts l = new LoadContacts();
        
    }
    
    public void commandAction(Command cmd, Displayable displayable) {
        
        // if no names are available return
        if (!available) {
            parent.showMain();
            return;
        else if (cmd == selectCommand) {
            int selected = getSelectedIndex();
            if (selected >= 0) {
                parent.contactSelected(
                        (StringallTelNumbers.elementAt(selected));
            else {
                parent.showMain();
            }
        else if (cmd == backCommand) {
            parent.showMain();
        else if (cmd == exitCommand) {
            parent.notifyDestroyed();
        }
        
    }
    
    
    // loads the names of a named contact list
    private void loadNames(String name
        throws PIMException, SecurityException {
        
        ContactList contactList = null;
        try {
            contactList = (ContactListPIM.getInstance()
            .openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY, name);
            
            // First check that the fields we are interested in 
            // are supported by the PIM List
            if (contactList.isSupportedField(Contact.FORMATTED_NAME)
            && contactList.isSupportedField(Contact.TEL)) {
                
                setTitle("Contacts");
                Enumeration items = contactList.items();
                delete(0);
                Vector telNumbers = new Vector();
                
                while (items.hasMoreElements()) {
                    
                    Contact contact = (Contactitems.nextElement();
                    int telCount = contact.countValues(Contact.TEL);
                    int nameCount = contact.countValues(
                            Contact.FORMATTED_NAME);
                    
                    if (telCount > && nameCount > 0) {
                        String contactName =contact.getString(
                                Contact.FORMATTED_NAME, 0);
                        
                        for (int i = 0; i < telCount; i++) {
                            
                            int telAttributes = 
                                    contact.getAttributes(Contact.TEL, i);
                            
                            String telNumber = 
                                    contact.getString(Contact.TEL, i);
                            
                            telNumbers.addElement(telNumber);
                            allTelNumbers.addElement(telNumber);
                            
                        }
                        
                        // insert elements in the list in order
                        for (int i = 0; i < telNumbers.size(); i++) {
                            
                            append(contactName+ ": "
                                    telNumbers.elementAt(i)null);
                        }
                        
                        telNumbers.removeAllElements();
                    }
                }
                
                available = true;
                
            else {
                
                append("Contact list required items not supported"null);
                available = false;
            }
        finally {
            
            // always close it
            if (contactList != null) {
                contactList.close();
            }
        }
    }
    
    private class LoadContacts implements Runnable {
        
        public LoadContacts() {
            new Thread(this).start();
        }
        
        public void run() {
            
            try {
                
                String[] allContactLists = 
                        PIM.getInstance().listPIMLists(PIM.CONTACT_LIST);
                
                if (allContactLists.length != 0) {
                    for (int i = 0; i < allContactLists.length; i++) {
                        loadNames(allContactLists[i]);
                    }
                    addCommand(selectCommand);
                else {
                    append("No Contact lists available"null);
                    available = false;
                }
                
            catch (PIMException e) {
                
                parent.showMessage(e.getMessage(), ContactListForm.this);
                available = false;
                
            catch (SecurityException e) {
                
                parent.showMessage(e.getMessage(), ContactListForm.this);
                available = false;
                
            }
        }
    }
}


 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.