J2ME 读取手机本地通讯录

mport java.util.Enumeration;   
import javax.microedition.lcdui.Command;   
import javax.microedition.lcdui.CommandListener;   
import javax.microedition.lcdui.Display;   
import javax.microedition.lcdui.Displayable;   
import javax.microedition.lcdui.Form;   
import javax.microedition.midlet.MIDlet;   
import javax.microedition.midlet.MIDletStateChangeException;   
import javax.microedition.pim.Contact;   
import javax.microedition.pim.ContactList;   
import javax.microedition.pim.PIM;   
import javax.microedition.pim.PIMException;   

/** 
*   
* PIM DEMO 
*   
* @author kf156(亚日) 
*   
*/ 
public class PIMTest extends MIDlet implements CommandListener {   

    private Display display;   

    private Form form;   

    private Command exit;   

    Command ok;   

    public PIMTest() {   
        display = Display.getDisplay(this);   
        ok = new Command("读取", Command.OK, 1);   
        exit = new Command("退出", Command.EXIT, 1);   
        form = new Form("读取通讯录");   
        form.addCommand(ok);   
        form.addCommand(exit);   
        form.setCommandListener(this);   

    }   

    protected void destroyApp(boolean arg0) {   
    }   

    protected void pauseApp() {   
    }   

    protected void startApp() throws MIDletStateChangeException {   
        display.setCurrent(form);   
    }   

    /** 
     * 读通讯录 
     *   
     */ 
    private void readAddressList() {   

        String[] names = PIM.getInstance().listPIMLists(PIM.CONTACT_LIST);// 得到联系列表名称   

        for (int i = 0; i < names.length; i++) {   
            try {   
                form.append("name=" + names[i] + "\n");   
                ContactList list = (ContactList) PIM.getInstance().openPIMList(   
                        PIM.CONTACT_LIST, PIM.READ_ONLY, names[i]);   
                Contact contact;   
                Enumeration e = list.items();   
                int n = 0;// 记录数   
                while (e.hasMoreElements()) {   
                    contact = (Contact) e.nextElement();   
                    String name = null;   
                    try {   

                        // 取NAME   
                        String[] arrayStr = contact.getStringArray(   
                                Contact.NAME, 0);   

                        if (arrayStr != null && arrayStr.length != 0) {   
                            name = "";   
                            // 在N78上测试前五位下标有效,并分别对应:0姓、1名、2中间名、3头衔、4称谓   
                            for (int j = 0; j < arrayStr.length; j++) {   
                                if (arrayStr[j] != null 
                                        && arrayStr[j].length() != 0) {   
                                    name += j + "." + arrayStr[j];   
                                }   
                            }   
                        }   

                    } catch (Exception exc) {   
                        form.append("error: " + exc.toString() + "\n");   
                    }   

                    try {   
                        if (name == null) {// 若NAME没取到,则改取FORMATTED_NAME   
                            if (list.isSupportedField(Contact.FORMATTED_NAME)) {   
                                name = contact.getString(   
                                        Contact.FORMATTED_NAME, 0);   
                            }   
                        }   

                    } catch (Exception exc) {   
                        form.append("error: " + exc.toString() + "\n");   
                    }   

                    form.append("name=" + name + "\n");   

                    if (list.isSupportedField(Contact.TEL)) {// 取TEL   
                        String tel = contact.getString(Contact.TEL, 0);   
                        form.append("tel=" + tel + "\n");   
                    }   

                    n++;// 记录数增加   
                }   

                form.append("共" + n + "条记录 \n");   
                list.close();   
            } catch (PIMException e) {   
                form.append("PIMException: " + e.toString());   
                e.printStackTrace();   
            } catch (Exception e) {   
                form.append("Exception: " + e.toString());   
                e.printStackTrace();   
            }   
        }   

    }   

    public void commandAction(Command c, Displayable d) {   
        if (c == exit) {   
            destroyApp(true);   
            notifyDestroyed();   
        } else if (c == ok) {   
            readAddressList();   
        }   
    }   

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值