İçindekilerGirişİndex
YukarıİlkÖncekiSonrakiSon
Geriİleri
Yazdır
Zafer Teker
tekzaf@yahoo.com

Cep Telefonları İçin Basit Bir Telefon Rehberi Uygulaması

Bu program java destekli cep telefonlarında çalışan basit bir telefon rehber uygulamasıdır. Her kişi için bir isim ve bir tel bilgisi girilir. Tüm kişilerin listesi görünmektedir. Listeden bir kişi seçilip silinebilir.

Program çalıştığında eklenmiş olan kişilerin listesi bulunur. "Kayıt" seçeneği ile yeni bir kişi eklenebilir. Listeden bir eleman seçilerek silinebilir.

Programda 5 class bulunmaktadır. Sırayla bu class'lardan bahsedeceğiz.

Contact Class'i

Göster Gizle Kopar Satır Gizle Satır Göster
  1 public class Contact{
  2   private int recordId;
  3   private String name;
  4   private String phone;
  5   public Contact(int recordId,String name,String phone){
  6     this.recordId=recordId;
  7     this.name=name;
  8     this.phone=phone;
  9   }
 10   public int getRecordId(){
 11     return recordId;
 12   }
 13   public String getName(){
 14     return name;
 15   }
 16   public String getPhone(){
 17     return phone;
 18   }
 19   public String toString(){
 20     return recordId+" "+name+" "+phone;
 21   }
 22 }

Bir kayıt id'si, bir isim ve bir telefon numarası bilgisini tutan bir class'tır. kayıt id'si kayıt edildiğinde bu kişiye ait oluşan bir id'dir

RecordManager class'ı

Göster Gizle Kopar Satır Gizle Satır Göster
  1 import javax.microedition.rms.*;
  2 import javax.microedition.io.*;
  3 import java.io.*;
  4 import java.util.*;
  5 public class RecordManager {
  6   public final static String RECORD_STORE_ID="PhoneBook";
  7   public static int recordContact(String name,String phone) throws Exception{
  8     RecordStore profile = RecordStore.openRecordStore(RECORD_STORE_ID, true);
  9     ByteArrayOutputStream baos=new ByteArrayOutputStream();
 10     DataOutputStream dos=new DataOutputStream(baos);
 11     dos.writeUTF(name);
 12     dos.writeUTF(phone);
 13     byte[] bytes=baos.toByteArray();
 14     int recordId=profile.addRecord(bytes,0,bytes.length);
 15     dos.close();
 16     baos.close();
 17     return recordId;
 18   }
 19   public static Vector readRecords() throws Exception{
 20     Vector vector=new Vector();
 21     RecordStore profile = RecordStore.openRecordStore(RECORD_STORE_ID,true);
 22     RecordEnumeration enum=profile.enumerateRecords(null,null,false);
 23     while(enum.hasNextElement()){
 24       int recordId=enum.nextRecordId();
 25       byte[] contact=profile.getRecord(recordId);
 26       ByteArrayInputStream bais = new ByteArrayInputStream(contact);
 27       DataInputStream dis = new DataInputStream(bais);
 28       String name = dis.readUTF();
 29       String phone = dis.readUTF();
 30       Contact c=new Contact(recordId,name,phone);
 31       vector.addElement(c);
 32     }
 33     return vector;
 34   }
 35   public static void deleteRecord(int index) throws Exception{
 36     RecordStore profile = RecordStore.openRecordStore(RECORD_STORE_ID,true);
 37     profile.deleteRecord(index);
 38   }
 39 }

RecordManager class'ı bilgileri kaydetip okuma işlemlerini gerçekleştirir. üç method'u bulunur.

recordContact() verilen isim ve telefon numarasını kayıt eder. Geriye kayıt id'sini döndürür. 
readRecords() kayıt edilmiş tüm kayıtları bir Contact vector'üne koyar ve vector'ü geri döndürür. 
deleteRecord() verilen id'li kayıt siler

RecordForm class'ı

Göster Gizle Kopar Satır Gizle Satır Göster
  1 import javax.microedition.lcdui.*;
  2 import javax.microedition.lcdui.Displayable;
  3 public class RecordForm extends Form implements CommandListener{
  4   private Command record=new Command("Kaydet",Command.SCREEN,1);
  5   private Command list=new Command("Liste",Command.SCREEN,2);
  6   private TextField name;
  7   private TextField phone;
  8   private PhoneBook phoneBook;
  9   public RecordForm(PhoneBook phoneBook){
 10     super("Kayıt Formu");
 11     this.phoneBook=phoneBook;
 12     name=new TextField("isim","",100,TextField.ANY);
 13     phone=new TextField("tel","",100,TextField.PHONENUMBER);
 14     append(name);
 15     append(phone);
 16     addCommand(record);
 17     addCommand(list);
 18     setCommandListener(this);
 19   }
 20   public void commandAction(Command command, Displayable displayable){
 21     if(command==record){
 22       String contactName = name.getString();
 23       String contactPhone = phone.getString();
 24       if(!contactName.equals("")){
 25         try {
 26           RecordManager.recordContact(contactName, contactPhone);
 27           name.setString("");
 28           phone.setString("");
 29         }
 30         catch (Exception e) {
 31           phoneBook.showExceptionAlert(e, "Kayit Sirasinda Bir Hata Olustu");
 32           e.printStackTrace();
 33         }
 34       }
 35     }else if(command==list){
 36       phoneBook.showContactList();
 37     }
 38   }
 39 }

Bu class kullanıcının bir kişi eklemesi için kullanılır. Bir isim alanı ve bir telefon giriş alanı bulunmaktadır. Kaydet seçildiğinde girilen bilgiler kaydedilir.

ContactList class'ı

Göster Gizle Kopar Satır Gizle Satır Göster
  1 import javax.microedition.lcdui.*;
  2 import javax.microedition.lcdui.Displayable;
  3 import java.util.*;
  4 public class ContactList extends List implements CommandListener{
  5   private Command refresh=new Command("Yenile",Command.SCREEN,1);
  6   private Command record=new Command("Kayıt",Command.SCREEN,2);
  7   private Command delete=new Command("Sil",Command.SCREEN,3);
  8   private PhoneBook phoneBook;
  9   private Vector contacts=new Vector();
 10   public ContactList(PhoneBook phoneBook){
 11     super("Tel Listesi",List.IMPLICIT);
 12     this.phoneBook=phoneBook;
 13     addCommand(refresh);
 14     addCommand(record);
 15     addCommand(delete);
 16     setCommandListener(this);
 17   }
 18   public void refresh(){
 19     try{
 20       clear();
 21       contacts = RecordManager.readRecords();
 22       for(int i=0;i<contacts.size();i++){
 23         Contact c=(Contact)contacts.elementAt(i);
 24         append(c.getName()+":"+c.getPhone(),null);
 25       }
 26     }catch(Exception e){
 27       phoneBook.showExceptionAlert(e,"Kayitlarin Okunmasi Sirasinda Bir Hata Olustu");
 28       e.printStackTrace();
 29     }
 30   }
 31   public void commandAction(Command command, Displayable displayable){
 32     if(command==refresh){
 33       refresh();
 34     }else if(command==record){
 35       phoneBook.showRecordForm();
 36     }else if(command==delete){
 37       try{
 38         Contact contact=getSelectedContact();
 39         if(contact!=null){
 40           RecordManager.deleteRecord(contact.getRecordId());
 41           refresh();
 42         }
 43       }catch(Exception e){
 44         e.printStackTrace();
 45         phoneBook.showExceptionAlert(e,"Kayitin Silinmesi Sirasinda Bir Hata Olustu");
 46       }
 47     }
 48   }
 49   private void clear(){
 50     contacts.removeAllElements();
 51     int size=size();
 52     for(int i=0;i<size;i++){
 53        delete(0);
 54     }
 55   }
 56   private Contact getSelectedContact(){
 57     int index = getSelectedIndex();
 58     String selected=getString(index);
 59     int iOf=selected.indexOf(":");
 60     String name=selected.substring(0,iOf);
 61     for(int i=0;i<contacts.size();i++){
 62       Contact c=(Contact)contacts.elementAt(i);
 63       if(c.getName().equals(name)){
 64         return c;
 65       }
 66     }
 67     return null;
 68   }
 69 }

Tüm kayıtların göründüğü listedir. refresh method'u çağrıldığında tüm kayıtları RecordManager class'ının readRecords method'unu kullanarak alınır. Eski kayıtlar listeden kaldırılır ve yeni kayıtlar listeye eklenir.

PhoneBook class'ı

Göster Gizle Kopar Satır Gizle Satır Göster
  1 import javax.microedition.midlet.*;
  2 import javax.microedition.lcdui.*;
  3 public class PhoneBook extends MIDlet{
  4   private ContactList list;
  5   private RecordForm form;
  6   protected void startApp(){
  7     list=new ContactList(this);
  8     form=new RecordForm(this);
  9     showContactList();
 10   }
 11   protected void pauseApp(){}
 12   protected void destroyApp(boolean boolean0){}
 13   public void showContactList(){
 14     list.refresh();
 15     Display.getDisplay(this).setCurrent(list);
 16   }
 17   public void showRecordForm(){
 18     Display.getDisplay(this).setCurrent(form);
 19   }
 20   public void showExceptionAlert(Exception e,String message){
 21     Alert alert=new Alert(e.getMessage(),message,null,AlertType.ERROR);
 22     alert.setTimeout(Alert.FOREVER);
 23     Display.getDisplay(this).setCurrent(alert);
 24   }
 25 }

PhoneBook midlet class'ıdır. Midlet yüklendiğinde önce liste gösterilir.

Dosya Listesi

İçindekilerGirişİndex
YukarıİlkÖncekiSonrakiSon
Geriİleri
Yazdır