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来存储联系人信息:
package com.kennan.conversion;
import java.util.Enumeration;
import net.rim.device.api.collection.ReadableIntMap;
import net.rim.device.api.collection.WritableIntMap;
import net.rim.device.api.util.IntEnumeration;
import net.rim.device.api.util.IntMultiMap;
/**
*
* @author kennan.zhang
* map
*
*/
public class ContackListMap extends IntMultiMap implements ReadableIntMap,
WritableIntMap {
public boolean contains(int key) {
return super.containsKey(key);
}
public Object get(int key) {
Enumeration e = super.elements(key);
if (e.hasMoreElements()) {
return e.nextElement();
}
return null;
}
public int getKey(Object element) {
IntEnumeration ie = super.keys();
if (ie.hasMoreElements()) {
return ie.nextElement();
}
return 0;
}
public int size() {
return super.size();
}
public void put(int key, Object element) {
super.add(key, element);
}
public void remove(int key) {
super.removeKey(key);
}
public void removeAll() {
super.clear();
}
}
下面是获得联系人信息的方法。
package com.kennan.contactList;
import java.util.Enumeration;
import javax.microedition.pim.Contact;
import javax.microedition.pim.ContactList;
import javax.microedition.pim.PIM;
import javax.microedition.pim.PIMException;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.util.IntMultiMap;
import com.kennan.conversion.ContackListMap;
import com.kennan.conversion.HGB2PINYIN;
/**
*
* @author kennan.zhang
*
*/
public final class ContactListScreen extends MainScreen {
// 联系人列表
private ContackListMap _ownContactList = new ContackListMap();
private KennanContact _kapp;
private String[] _tels;
public ContactListScreen(KennanContact kapp) {
super();
_kapp = kapp;
Enumeration e;
String uid;
String[] names;
ContackListMap perInfo;
Contact c;
int ncount;
StringBuffer name;
int tcount;
try {
e = ((ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST,
PIM.READ_WRITE)).items();
while (e.hasMoreElements()) {
perInfo = new ContackListMap();
c = (Contact) e.nextElement();
// UID
uid = c.getString(Contact.UID, 0);
// 合并 添加姓名
names = c.getStringArray(Contact.NAME, 0);
ncount = names.length;
name = new StringBuffer();
for (int i = 0; i < ncount; i++) {
if (names[i] != null) {
name.append(names[i]);
name.append(' ');
}
}
perInfo.put(Contact.NAME, name);
// 电话
tcount = c.countValues(Contact.TEL);
_tels = new String[tcount];
for (int i = 0; i < tcount; i++) {
_tels[i] = c.getString(Contact.TEL, i);
}
perInfo.put(Contact.TEL, _tels);
// E-mail
if (c.countValues(Contact.EMAIL) > 0) {
perInfo.put(Contact.EMAIL, c.getString(Contact.EMAIL, 0));
}
// 添加联系人信息
_ownContactList.put(Integer.parseInt(uid), perInfo);
// 添加联系人条目
add(new ContactListNode(name.toString(), uid, false));
}
} catch (PIMException a) {
a.printStackTrace();
} catch (IndexOutOfBoundsException a) {
a.printStackTrace();
}
// 显示联系人
this.addMenuItem(viewItem);
}
// 查看联系人信息
private MenuItem viewItem = new MenuItem("查看联系人信息", 100, 10) {
public void run() {
int uid = getUID(this);
// 联系人详细信息
StringBuffer inform = new StringBuffer();
inform.append("姓名:");
inform.append(((ContackListMap) _ownContactList.get(uid))
.get(Contact.NAME));
inform.append('\n');
// 联系人电话
inform.append("电话:\n");
_tels = (String[]) ((ContackListMap) (_ownContactList.get(uid)))
.get(Contact.TEL);
int _tlength = _tels.length;
for (int i = 0; i < _tlength; i++) {
inform.append(_tels[i]);
inform.append('\n');
}
// E-mail
inform.append("E-mail:\n");
inform.append(((ContackListMap) _ownContactList.get(uid))
.get(Contact.EMAIL));
Dialog.inform(inform.toString());
inform = null;
}
};
private int getUID(MenuItem mi) {
ContactListNode pnb = (ContactListNode) mi.getTarget();
return Integer.parseInt(pnb.getUid());
}
}