当我尝试使用以下方式获取电话簿联系人时
contactInfo.mobileNo = contactItem.getString(Contact.TEL,0);
我只能在电话号码条目下获得号码,而不会在手机号码下显示条目。
如何在手机号码下获取电话簿条目?
答案 0 :(得分:3)
您只需获取一个元素,需要循环
<强> contactItem.getString(Contact.TEL,0); 强>
import java.util.Enumeration;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.pim.Contact;
import javax.microedition.pim.ContactList;
import javax.microedition.pim.PIM;
String str = "";
try {
PIM pim = PIM.getInstance();
String[] names = pim.listPIMLists(PIM.CONTACT_LIST);
pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY, names[0]);
ContactList addressbook = (ContactList) (pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY));
Contact contact = null;
String phoneNumber = null;
Enumeration items = null;
items = addressbook.items();
while (items.hasMoreElements()) {
contact = (Contact) items.nextElement();
int telCount = contact.countValues(Contact.TEL);
int nameCount = contact.countValues(Contact.FORMATTED_NAME);
if (telCount > 0 && nameCount > 0) {
String contactName = contact.getString(Contact.FORMATTED_NAME, 0);
System.out.println("\n\nName: "+contactName);
str += "\n\nName: "+contactName;
for (int i = 0; i < telCount; i++) {
phoneNumber = contact.getString(Contact.TEL, i);
System.out.println("\nNumber: "+phoneNumber);
str += "\nNumber: "+phoneNumber;
}
}
}
} catch (SecurityException e) {
} catch (Exception e) {
}