Monday, December 5, 2011

Select the Contact from PhoneBook and Send SMS Using J2ME

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

/**
* @author administrator
*/
public class SendSms extends MIDlet implements CommandListener, ItemCommandListener {
      private Display display;
      private Form composeSms;
      private TextField toWhom;
      private TextField message;
      private Alert alert;
      ContactListForm contactlistform;
      private StringItem send;
      private Command select, exit, add;
      HttpConnection httpconnection;

      public SendSms() {
            display = Display.getDisplay(this);
            composeSms = new Form("SMS");
            toWhom = new TextField("To", "", 13, TextField.PHONENUMBER);
            message = new TextField("Message", "", 160, TextField.ANY);
            select = new Command("Select", Command.OK, 0);
            send = new StringItem("", "Send", StringItem.BUTTON);
            exit = new Command("Exit", Command.EXIT, 2);
            add = new Command("Add", Command.OK, 0);
            composeSms.append(toWhom);
            composeSms.append(message);
            composeSms.append(send);
            toWhom.setDefaultCommand(add);
            send.setDefaultCommand(select);
            composeSms.addCommand(exit);
            toWhom.setItemCommandListener(this);
            send.setItemCommandListener(this);
            composeSms.setCommandListener(this);
      }

      public void startApp() {
            Displayable current = Display.getDisplay(this).getCurrent();
            if (current == null) {
                  boolean isAPIAvailable = (System.getProperty("microedition.pim.version") != null);
                  StringBuffer sbuf = new StringBuffer(getAppProperty("MIDlet-Name"));
                  sbuf.append("\n").append(getAppProperty("MIDlet-Vendor")).append(isAPIAvailable ? "" : "\nPIM API not available");
                  Alert alertScreen = new Alert(null, sbuf.toString(), null, AlertType.INFO);
                  alertScreen.setTimeout(3000);
                  if (!isAPIAvailable) {
                        display.setCurrent(alertScreen);
                  } else {
                        display.setCurrent(composeSms);
                  }
            } else {
                  Display.getDisplay(this).setCurrent(current);
            }
      }

      public void pauseApp() {
      }

      public void destroyApp(boolean unconditional) {
            notifyDestroyed();
      }

      void showMessage(String message, Displayable displayable) {
            Alert alert = new Alert("");
            alert.setTitle("Error");
            alert.setString(message);
            alert.setType(AlertType.ERROR);
            alert.setTimeout(5000);
            display.setCurrent(alert, displayable);
      }

      void showMain() {
            display.setCurrent(composeSms);
      }

      void showContactsList() {
            contactlistform = new ContactListForm(this);
            contactlistform.LoadContacts();
            display.setCurrent(contactlistform);
      }
      void contactSelected(String telephoneNumber) {
            this.setPhoneNumber(telephoneNumber);
            showMain();
      }

      void setPhoneNumber(String phoneNumber) {
            toWhom.setString(phoneNumber);
      }

      public void commandAction(Command cmd, Displayable disp) {
            if (cmd == exit && disp==composeSms) {
                  destroyApp(false);
            }
      }

      public void commandAction(Command cmd, Item item) {
            if (cmd == add && item==toWhom) {
                  showContactsList();
            }
            if (cmd == select && item==send) {
                  String mno = toWhom.getString();
                  String msg = message.getString();
                  String s = " ";
                  StringBuffer stringbuffer = null;
                  if (mno.equals("") || msg.equals("")) {
                        if (mno.equals("")) {
                              alert = new Alert("Alert");
                              alert.setString("Enter Mobile Number!!!");
                              alert.setTimeout(2000);
                              display.setCurrent(alert);
                        } else {
                              alert = new Alert("Alert");
                              alert.setString("Enter Message!!!");
                              alert.setTimeout(2000);
                              display.setCurrent(alert);
                        }
                  } else {
                        try {
                              httpconnection = (HttpConnection) Connector.open("http://www.....Mno=" + mno + "&Msg=" + msg);
                              int k = httpconnection.getResponseCode();
                              if (k != 200) {
                                    throw new IOException("HTTP response code: " + k);
                              }
                              InputStream inputstream = httpconnection.openInputStream();
                              alert = new Alert("Alert");
                              alert.setString("In Connecting.....");
                              alert.setTimeout(500);
                              int l = (int) httpconnection.getLength();
                              if (l > 0) {
                                    int i1 = 0;
                                    int j1 = 0;
                                    byte abyte0[] = new byte[l];
                                    while (j1 != l && i1 != -1) {
                                          i1 = inputstream.read(abyte0, j1, l - j1);
                                          j1 += i1;
                                          stringbuffer.append(new String(abyte0));
                                    }
                                    s = stringbuffer.toString();
                                    alert = new Alert("Alert");
                                    alert.setString(s);
                                    alert.setTimeout(2000);
                                    display.setCurrent(alert);
                              }
                              try {
                                    if (inputstream != null) {
                                          inputstream.close();
                                    }
                                    if (httpconnection != null) {
                                          httpconnection.close();
                                    }
                              } catch (Exception e) {
                                    s = e.toString();
                                    alert = new Alert("Alert");
                                    alert.setString(s);
                                    alert.setTimeout(2000);
                                    display.setCurrent(alert);
                              }
                        } catch (Exception e) {
                              s = e.toString();
                              alert = new Alert("Alert");
                              alert.setString(s);
                              alert.setTimeout(2000);
                              display.setCurrent(alert);
                        }
                  }
            }
      }
}


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.pim.*;

class ContactListForm extends List implements CommandListener {
      private final Command exitCommand, selectCommand, backCommand;
      private final SendSms parent;
      Contactnumbers contactnumbers;
      private boolean available;
      private Vector allTelNumbers = new Vector();
      private Display display;
      public ContactListForm(SendSms parent) {
            super("Select Memory", Choice.IMPLICIT);
            this.display = Display.getDisplay(parent);
            this.parent = parent;
            selectCommand = new Command("Select", Command.OK, 0);
            backCommand = new Command("Back", Command.BACK, 1);
            exitCommand = new Command("Exit", Command.EXIT, 1);
            addCommand(backCommand);
            setSelectCommand(selectCommand);
            addCommand(exitCommand);
            setCommandListener(this);
            setFitPolicy(Choice.TEXT_WRAP_ON);
      }

      public void commandAction(Command cmd, Displayable displayable) {
            if (cmd == selectCommand) {
                  int selected = getSelectedIndex();
                  if (selected >= 0) {
                        try {
                              contactnumbers = new Contactnumbers(parent);
                              contactnumbers.loadNames(getString(selected));
                              display.setCurrent(contactnumbers);
                        } catch (PIMException e) {
                              parent.showMessage(e.getMessage(), ContactListForm.this);
                              available = false;
                        } catch (SecurityException e) {
                              parent.showMessage(e.getMessage(), ContactListForm.this);
                              available = false;
                        }
                  } else {
                        parent.showMain();
                  }
            } else if (cmd == backCommand) {
                  parent.showMain();
            } else if (cmd == exitCommand) {
                  parent.destroyApp(false);
            }
      }

      private void displaycontactnames(String contactname) {
            append(contactname, null);
      }
      public void LoadContacts() {
            try {
                  String[] allContactLists = PIM.getInstance().listPIMLists(PIM.CONTACT_LIST);
                  if (allContactLists.length != 0) {
                        for (int i = 0; i < allContactLists.length; i++) {
                              displaycontactnames(allContactLists[i]);
                        }
                        addCommand(selectCommand);
                  }
                  else {
                        append("No Contact lists available", null);
                        available = false;
                  }
            }
            catch (SecurityException e) {
                  parent.showMessage(e.getMessage(), ContactListForm.this);
                  available = false;
            }
      }
}


import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.pim.*;


public class Contactnumbers extends Form implements CommandListener {
      private final Command exit, select, back;
      boolean available;
      private Vector allTelNumbers = new Vector();
      private Display display;
      private ChoiceGroup contactnum;
      SendSms parent;
      ContactListForm contactlistform;
      public Contactnumbers(SendSms parent) {
            super("");
            this.parent = parent;
            this.display = Display.getDisplay(parent);
            contactnum = new ChoiceGroup("Contacts", ChoiceGroup.EXCLUSIVE);
            contactnum.deleteAll();
            append(contactnum);
            exit = new Command("Exit", Command.SCREEN, 2);
            select = new Command("Submit", Command.BACK, 0);
            back = new Command("Back", Command.SCREEN, 1);
            addCommand(select);
            addCommand(back);
            addCommand(exit);
            setCommandListener(this);
      }

      public void commandAction(Command cmd, Displayable disp) {
            if (cmd == select) {
                  int selected = contactnum.getSelectedIndex();
                  if (selected >= 0) {
                        parent.contactSelected((String) allTelNumbers.elementAt(selected));
                  }
            }
            if (cmd == back) {
                  parent.showContactsList();
            }
            f (cmd == exit) {
                  parent.notifyDestroyed();
            }
      }
      public void loadNames(String name) throws PIMException, SecurityException {
            ContactList contactList = null;
            try {
                  contactList = (ContactList) PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY, name);
                  if (contactList.isSupportedField(Contact.FORMATTED_NAME) && contactList.isSupportedField(Contact.TEL)) {
                        Enumeration items = contactList.items();
                        Vector telNumbers = new Vector();
                        while (items.hasMoreElements()) {
                              Contact 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);
                                    for (int i = 0; i < telCount; i++) {
                                          int telAttributes = contact.getAttributes(Contact.TEL, i);
                                          String telNumber = contact.getString(Contact.TEL, i);
                                          telNumbers.addElement(telNumber);
                                          allTelNumbers.addElement(telNumber);
                                    }
                                    for (int i = 0; i < telNumbers.size(); i++) {
                                          contactnum.append(contactName, null);
                                          contactnum.setSelectedIndex(0, true);
                                    }
                                    telNumbers.removeAllElements();
                              }
                        }
                        available = true;
                  } else {
                        contactnum.append("Contact list required items not supported", null);
                        available = false;
                  }
            } finally {
                  if (contactList != null) {
                        contactList.close();
                  }
            }
      }
}

No comments:

Post a Comment