利用J2ME怎样调用手机中的电话薄?

来源:互联网 发布:何时统一台湾 知乎 编辑:程序博客网 时间:2024/05/08 22:33

Java code
/** * 导入 */ public static void importContacts() { //得到能支持的功能名字。一般只有电话本 String[] names = PIM.getInstance().listPIMLists(PIM.CONTACT_LIST); m_vNames = new Vector(); m_vTels = new Vector(); for (int i = 0; i < names.length; i++) { try { //联系人 ContactList list = (ContactList) PIM.getInstance().openPIMList( PIM.CONTACT_LIST, PIM.READ_ONLY, names[i]); Contact contact = null; for (Enumeration e = list.items(); e.hasMoreElements();) { contact = (Contact) e.nextElement(); try {//姓名 m_sNameImporting = contact.getString(Contact.FORMATTED_NAME, 0);//电话号码,如果一个联系人对应多个号码,只能取得第一个。 m_sTelsImporting = contact.getString(Contact.TEL, 0); m_vNames.addElement(m_sNameImporting); m_vTels.addElement(m_sTelsImporting); } catch (Exception ex) { ex.printStackTrace(); } } list.close(); } catch (Exception ex) { ex.printStackTrace(); } } }

RIM有自己的API(BlackBerryContact.class)提供联系人信息。

但是需要签名才能运行。

我们可以用javax.microedition.pim .Contact 来得到电话里的联系人信息。

在bb运行时会弹出确认框,但不影响使用。

下面的table列举了不同信息分类,我们可以从中得到需要的信息。

Fields Type of Data Associated with Field NAME, ADDR PIMItem.STRING_ARRAY EMAIL, FORMATTED_NAME, NICKNAME, PHOTO_URL, PUBLIC_KEY_STRING, FORMATTED_ADDR, NOTE, ORG, TEL, TITLE, UID, URL PIMItem.STRING BIRTHDAY, REVISION PIMItem.DATE PHOTO, PUBLIC_KEY PIMItem.BINARY CLASS PIMItem.INT

 

下面有一个我取联系人信息的例子:

 

首先写了一个map来存储联系人信息:

 

Java代码 复制代码
  1. package com.kennan.conversion;   
  2.   
  3. import java.util.Enumeration;   
  4.   
  5. import net.rim.device.api.collection.ReadableIntMap;   
  6. import net.rim.device.api.collection.WritableIntMap;   
  7. import net.rim.device.api.util.IntEnumeration;   
  8. import net.rim.device.api.util.IntMultiMap;   
  9.   
  10. /**  
  11.  *   
  12.  * @author kennan.zhang  
  13.  * map  
  14.  *   
  15.  */  
  16. public class ContackListMap extends IntMultiMap implements ReadableIntMap,   
  17.         WritableIntMap {   
  18.   
  19.     public boolean contains(int key) {   
  20.   
  21.         return super.containsKey(key);   
  22.     }   
  23.   
  24.     public Object get(int key) {   
  25.         Enumeration e = super.elements(key);   
  26.         if (e.hasMoreElements()) {   
  27.   
  28.             return e.nextElement();   
  29.         }   
  30.         return null;   
  31.     }   
  32.   
  33.     public int getKey(Object element) {   
  34.         IntEnumeration ie = super.keys();   
  35.         if (ie.hasMoreElements()) {   
  36.             return ie.nextElement();   
  37.         }   
  38.         return 0;   
  39.     }   
  40.   
  41.     public int size() {   
  42.   
  43.         return super.size();   
  44.     }   
  45.   
  46.     public void put(int key, Object element) {   
  47.         super.add(key, element);   
  48.     }   
  49.   
  50.     public void remove(int key) {   
  51.         super.removeKey(key);   
  52.     }   
  53.   
  54.     public void removeAll() {   
  55.         super.clear();   
  56.     }   
  57.   
  58. }  

 

 下面是获得联系人信息的方法。

 

Java代码 复制代码
  1. package com.kennan.contactList;   
  2.   
  3. import java.util.Enumeration;   
  4.   
  5. import javax.microedition.pim.Contact;   
  6. import javax.microedition.pim.ContactList;   
  7. import javax.microedition.pim.PIM;   
  8. import javax.microedition.pim.PIMException;   
  9.   
  10. import net.rim.device.api.ui.MenuItem;   
  11. import net.rim.device.api.ui.component.Dialog;   
  12. import net.rim.device.api.ui.container.MainScreen;   
  13. import net.rim.device.api.util.IntMultiMap;   
  14.   
  15. import com.kennan.conversion.ContackListMap;   
  16. import com.kennan.conversion.HGB2PINYIN;   
  17.   
  18. /**  
  19.  *   
  20.  * @author kennan.zhang  
  21.  *   
  22.  */  
  23. public final class ContactListScreen extends MainScreen {   
  24.   
  25.     // 联系人列表   
  26.     private ContackListMap _ownContactList = new ContackListMap();   
  27.   
  28.     private KennanContact _kapp;   
  29.   
  30.     private String[] _tels;   
  31.   
  32.     public ContactListScreen(KennanContact kapp) {   
  33.         super();   
  34.         _kapp = kapp;   
  35.         Enumeration e;   
  36.         String uid;   
  37.         String[] names;   
  38.         ContackListMap perInfo;   
  39.         Contact c;   
  40.         int ncount;   
  41.         StringBuffer name;   
  42.         int tcount;   
  43.   
  44.         try {   
  45.             e = ((ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST,   
  46.                     PIM.READ_WRITE)).items();   
  47.   
  48.             while (e.hasMoreElements()) {   
  49.                 perInfo = new ContackListMap();   
  50.                 c = (Contact) e.nextElement();   
  51.   
  52.                 // UID   
  53.                 uid = c.getString(Contact.UID, 0);   
  54.   
  55.                 // 合并 添加姓名   
  56.                 names = c.getStringArray(Contact.NAME, 0);   
  57.                 ncount = names.length;   
  58.                 name = new StringBuffer();   
  59.                 for (int i = 0; i < ncount; i++) {   
  60.                     if (names[i] != null) {   
  61.   
  62.                         name.append(names[i]);   
  63.                         name.append(' ');   
  64.                     }   
  65.                 }   
  66.                 perInfo.put(Contact.NAME, name);   
  67.   
  68.                 // 电话   
  69.                 tcount = c.countValues(Contact.TEL);   
  70.                 _tels = new String[tcount];   
  71.                 for (int i = 0; i < tcount; i++) {   
  72.                     _tels[i] = c.getString(Contact.TEL, i);   
  73.                 }   
  74.                 perInfo.put(Contact.TEL, _tels);   
  75.   
  76.                 // E-mail   
  77.                 if (c.countValues(Contact.EMAIL) > 0) {   
  78.                     perInfo.put(Contact.EMAIL, c.getString(Contact.EMAIL, 0));   
  79.                 }   
  80.   
  81.                 // 添加联系人信息   
  82.                 _ownContactList.put(Integer.parseInt(uid), perInfo);   
  83.   
  84.                 // 添加联系人条目   
  85.                 add(new ContactListNode(name.toString(), uid, false));   
  86.             }   
  87.   
  88.         } catch (PIMException a) {   
  89.   
  90.             a.printStackTrace();   
  91.         } catch (IndexOutOfBoundsException a) {   
  92.   
  93.             a.printStackTrace();   
  94.         }   
  95.                 // 显示联系人   
  96.         this.addMenuItem(viewItem);   
  97.     }   
  98.   
  99.     // 查看联系人信息   
  100.     private MenuItem viewItem = new MenuItem("查看联系人信息"10010) {   
  101.         public void run() {   
  102.             int uid = getUID(this);   
  103.   
  104.             // 联系人详细信息   
  105.             StringBuffer inform = new StringBuffer();   
  106.   
  107.             inform.append("姓名:");   
  108.             inform.append(((ContackListMap) _ownContactList.get(uid))   
  109.                     .get(Contact.NAME));   
  110.             inform.append('/n');   
  111.   
  112.             // 联系人电话   
  113.             inform.append("电话:/n");   
  114.             _tels = (String[]) ((ContackListMap) (_ownContactList.get(uid)))   
  115.                     .get(Contact.TEL);   
  116.             int _tlength = _tels.length;   
  117.             for (int i = 0; i < _tlength; i++) {   
  118.                 inform.append(_tels[i]);   
  119.                 inform.append('/n');   
  120.             }   
  121.   
  122.             // E-mail   
  123.             inform.append("E-mail:/n");   
  124.             inform.append(((ContackListMap) _ownContactList.get(uid))   
  125.                     .get(Contact.EMAIL));   
  126.   
  127.             Dialog.inform(inform.toString());   
  128.             inform = null;   
  129.         }   
  130.     };   
  131.   
  132.     private int getUID(MenuItem mi) {   
  133.   
  134.         ContactListNode pnb = (ContactListNode) mi.getTarget();   
  135.         return Integer.parseInt(pnb.getUid());   
  136.     }   
  137. }  

 

原创粉丝点击