Created
July 5, 2010 16:25
-
-
Save dongyuwei/464508 to your computer and use it in GitHub Desktop.
ContactListThread,show all contact name and mobile number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dongyuwei.contact; | |
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.UiApplication; | |
public class ContactListThread extends Thread { | |
Screen mainScreen; | |
public ContactListThread(Screen mainScreen) { | |
this.mainScreen = mainScreen; | |
} | |
public void run() { | |
synchronized (UiApplication.getEventLock()) { | |
mainScreen.appendLabelText(getContacts()); | |
} | |
} | |
private String getContacts() { | |
StringBuffer sb = new StringBuffer(); | |
try { | |
ContactList contactList = (ContactList) (PIM.getInstance() | |
.openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE)); | |
Enumeration enumx = contactList.items(); | |
while (enumx.hasMoreElements()) { | |
Contact contact = (Contact) enumx.nextElement(); | |
String fullName = ""; | |
StringBuffer telephone = new StringBuffer(); | |
if (contactList.isSupportedField(Contact.NAME)) { | |
String[] name = contact.getStringArray(Contact.NAME, 0); | |
String firstName = name[Contact.NAME_GIVEN]; | |
String lastName = name[Contact.NAME_FAMILY]; | |
if (firstName == null) { | |
firstName = ""; | |
} | |
if (lastName == null) { | |
lastName = ""; | |
} | |
fullName = firstName + " " + lastName; | |
} | |
if (contactList.isSupportedField(Contact.TEL)) { | |
int emailCount = contact.countValues(Contact.TEL); | |
for (int i = 0; i < emailCount; i++) { | |
String tel = contact.getString(Contact.TEL, i); | |
if (tel != null) { | |
telephone.append(tel.trim()); | |
telephone.append(";"); | |
} | |
} | |
} | |
sb.append(fullName.trim()).append(": ").append(telephone).append("\n"); | |
} | |
} catch (PIMException ex) { | |
ex.printStackTrace(); | |
} | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment