This time, I give you source code with java language to read and search contact number on handphone.I learn it in Mobile Computing class. Use midlet to make it, and of course type of the project is Mobile Application. I make this mobile application using Net Bean 6.8.
Here is the code, and enjoy:
import 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.lcdui.List;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.*;
import javax.microedition.pim.Contact;
import javax.microedition.pim.ContactList;
import javax.microedition.pim.PIM;
import javax.microedition.pim.PIMException;
/**
* @author Viktor Mononimbar
*/
public class ContactMidlet extends MIDlet implements CommandListener {
private ContactList contList = null;
private Enumeration contacts = null;
private List mNameList;
private Command mExitCommand = new Command("Keluar", Command.EXIT, 0);
private Command mCancelButton = new Command("Batal", Command.CANCEL, 0);
private Command mCariKontak = new Command("Cari", Command.OK, 1);
private Command mCariKontakCommand = new Command("Cari Kontak", Command.OK, 1);
private Form form = null;
private TextField searchText = new TextField("Cari Kontak", "", 20, TextField.ANY);
private StringItem searchMessageString = new StringItem("Hasil:", "");
private Display display;
public ContactMidlet() {
form = new Form("Kontak");
form.append(searchText);
form.append(searchMessageString);
form.addCommand(mCancelButton);
form.addCommand(mCariKontakCommand);
form.setCommandListener(this);
mNameList = new List("List of contacts", List.EXCLUSIVE);
mNameList.addCommand(mCariKontak);
mNameList.addCommand(mExitCommand);
mNameList.setCommandListener(this);
// form.append(mNameList);
loadContact();
}
private void loadContact() {
this.cariKontak("");
}
private void showList() {
}
public void startApp() {
display = Display.getDisplay(this);
display.setCurrent(mNameList);
}
public void pauseApp() {
}
public void destroyApp(boolean flg) {
}
public void commandAction(Command c, Displayable s) {
if (c == mExitCommand) {
destroyApp(true);
notifyDestroyed();
} else if (c == mCariKontak) {
display = Display.getDisplay(this);
display.setCurrent(form);
} else if (c == mCariKontakCommand) {
cariKontak(this.searchText.getString());
} else if (c == mCancelButton) {
display = Display.getDisplay(this);
display.setCurrent(mNameList);
}
}
private void cariKontak(String kriteria) {
PIM pimInst = PIM.getInstance();
this.mNameList.deleteAll();
this.searchMessageString.setText("Sedang Memuat..");
boolean ketemu = false;
try {
contList = (ContactList) pimInst.openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY);
contacts = contList.items();
} catch (PIMException ex) {
ex.printStackTrace();
}
if (contacts == null) {
return;
}
while (contacts.hasMoreElements()) {
try {
Contact tCont = (Contact) contacts.nextElement();
String[] nameValues = tCont.getStringArray(Contact.NAME, 0);
if (nameValues != null) {
String firstName = "", lastName = "";
String gabung = "";
if (nameValues.length > 0) {
lastName = nameValues[Contact.NAME_FAMILY];
lastName = lastName == null ? "" : lastName;
gabung += lastName;
}
if (nameValues.length > 1) {
firstName = nameValues[Contact.NAME_GIVEN];
firstName = firstName == null ? "" : firstName;
gabung += " " + firstName;
}
int phoneNumbers = tCont.countValues(Contact.TEL);
System.out.println("jumlah : " + phoneNumbers);
String telepon = "";
for (int i = 0; i < phoneNumbers; i++) {
if ((tCont.getAttributes(Contact.TEL, i) != 0) & Contact.ATTR_HOME != 0) {
// Home number
telepon += tCont.getString(Contact.TEL, i);
}
}
if (kriteria.length() > 0) {
if (gabung.toLowerCase().indexOf(kriteria.toLowerCase()) >= 0) {
mNameList.append(lastName + ", " + firstName + " " + telepon, null);
ketemu = true;
}
} else {
mNameList.append(lastName + ", " + firstName + " " + telepon, null);
ketemu = true;
}
}
} catch (Exception ex) {
//mNameList.append(ex.getMessage(), null);
ex.printStackTrace();
}
}
if (ketemu) {
display = Display.getDisplay(this);
display.setCurrent(mNameList);
} else {
this.searchMessageString.setText("Tidak ada yang mirip!.");
}
}
}
I hope you enjoy it. Viktor Mononimbar Source Code Maniac.
Tidak ada komentar:
Posting Komentar