PIM (Personal Information Management) adalah semacam pengolahan informasi pribadi, yaitu untuk memperoleh, mengatur, memelihara, mengambil, dan menggunakan item informasi untuk penggunaan sehari-hari.
Pada aplikasi mobile konsep seperti ini digunakan untuk pengolahan informasi contact phonebook, event calendar yang terintegrasi langsung pada memori mobile phone untuk keperluan sehari-hari.
Berangkat dari library yang disediakan java yaitu JSR-75, kita coba untuk membuat aplikasi J2ME untuk mengolah contact atau phonebook. Pada contoh yang saya berikan ini hanya mampu menambahkan contact dan menampilkan list kontak.
Source code:
==== ContactPhonebook.java =====
package contact; import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import javax.microedition.pim.*; import java.util.*; public class ContactPhonebook extends MIDlet implements CommandListener { Form myForm; Form newContactForm; TextField enterName; TextField enterNumber; Display display; ContactList myContactList = null; Enumeration contact; Contact c = null; Command contactDB; Command displayPhonebook; Command exit; Command enterContact; Command addContact; Alert alert; final private static int ERROR = 0; final private static int INFO = 1; List mainScr; //PIMS PIM myPIM; public ContactPhonebook() { myForm = new Form("Contact PIM"); display = Display.getDisplay(this); exit = new Command("Exit", Command.EXIT, 0); displayPhonebook = new Command("PhoneBook", Command.OK, 0); enterContact = new Command("Add Contact", Command.OK, 0); myForm.addCommand(displayPhonebook); myForm.addCommand(exit); myForm.addCommand(enterContact); alert = new Alert("", "", null, AlertType.INFO); alert.setTimeout(2000); myForm.setCommandListener(this); display.setCurrent(myForm); myPIM = PIM.getInstance(); initPIM(); } private void initPIM() { myForm.deleteAll(); myPIM = PIM.getInstance(); try { String ContactList[] = myPIM.listPIMLists(PIM.CONTACT_LIST); myContactList = (ContactList) myPIM.openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE); contact = myContactList.items(); } catch (Exception e) { e.printStackTrace(); } DisplayContacts(); } void DisplayNewContactForm() { newContactForm = new Form("Add new contact"); addContact = new Command("Add", Command.OK, 0); enterName = new TextField("Name", "", 20, TextField.ANY); enterNumber = new TextField("Mobile Number", "", 20, TextField.ANY); newContactForm.append(enterName); newContactForm.append(enterNumber); newContactForm.addCommand(addContact); newContactForm.addCommand(displayPhonebook); newContactForm.setCommandListener(this); display.setCurrent(newContactForm); } void createNewContact() { String getName = enterName.getString(); String getNumber = enterNumber.getString(); Contact newContact = myContactList.createContact(); if (myContactList.isSupportedField(Contact.FORMATTED_NAME)) { try { newContact.addString(Contact.FORMATTED_NAME, PIMItem.ATTR_NONE, getName); } catch (Exception e) { e.printStackTrace(); } } else { System.out.println("THIS IS NAME"); } if (myContactList.isSupportedField(Contact.TEL)) { try { newContact.addString(Contact.TEL, PIMItem.ATTR_NONE, getNumber); } catch (Exception e) { e.printStackTrace(); } } try { newContact.commit(); System.out.println("Contact Added \n"); } catch (Exception e) { e.printStackTrace(); } } void DisplayContacts() { while (contact.hasMoreElements()) { c = (Contact) contact.nextElement(); myForm.append(c.getString(Contact.FORMATTED_NAME, 0) + " -"); myForm.append(c.getString(Contact.TEL, 0) + "\n"); } } public void startApp() { } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable d) { if (c==exit){ this.destroyApp(true); this.notifyDestroyed(); } if (c == displayPhonebook) { initPIM(); display.setCurrent(myForm); } else if (c == enterContact) { DisplayNewContactForm(); } else if (c == addContact) { createNewContact(); DisplayNewContactForm(); } } }Menampilkan contact list:
void DisplayContacts() { while (contact.hasMoreElements()) { c = (Contact) contact.nextElement(); myForm.append(c.getString(Contact.FORMATTED_NAME, 0) + " -"); myForm.append(c.getString(Contact.TEL, 0) + "\n"); } }Keterangan singkat dari source code di atas:
- Untuk menampilkan nama kontak menggunakan fungsi Contact.FORMATTED_NAME
- Sedangkan untuk menampilkan nomer telepon menggunakan fungsi Contact.TEL
void createNewContact() { String getName = enterName.getString(); String getNumber = enterNumber.getString(); Contact newContact = myContactList.createContact(); if (myContactList.isSupportedField(Contact.FORMATTED_NAME)) { try { newContact.addString(Contact.FORMATTED_NAME, PIMItem.ATTR_NONE, getName); } catch (Exception e) { e.printStackTrace(); } } else { System.out.println("THIS IS NAME"); } if (myContactList.isSupportedField(Contact.TEL)) { try { newContact.addString(Contact.TEL, PIMItem.ATTR_NONE, getNumber); } catch (Exception e) { e.printStackTrace(); } } try { newContact.commit(); System.out.println("Contact Added \n"); } catch (Exception e) { e.printStackTrace(); } }Keterangan singkat dari source code di atas:
- Pada method createNewContact terdapat: newContact.addString(Contact.FORMATTED_NAME, PIMItem.ATTR_NONE, getName); untuk menambahkan contact nama dari textfield nama.
- Begitu pula pada contact telpon: newContact.addString(Contact.TEL, PIMItem.ATTR_NONE, getNumber); untuk menambahkan contact telpon berdasarkan value yang diinputkan pada textfield number.
Tidak ada komentar:
Posting Komentar