Sent Item code in java
- by Farhan Khan
I need urgent help, if any one can resolve my issue it will be very highly appriciated. I have create a SMS composer on jAVA netbians its on urdu language. the probelm is its not saving sent sms on Sent items.. i have tried my best to make the code but failed. Tomorrow is my last day to present the code on university please help me please below is the code that i have made till now.
Please any one....
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package newSms;
import javax.microedition.io.Connector;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
import org.netbeans.microedition.util.SimpleCancellableTask;
/**
* @author AHTISHAM
*/
public class composeurdu extends MIDlet implements CommandListener, ItemCommandListener, ItemStateListener {
private boolean midletPaused = false;
private boolean isUrdu;
String numb=" ";
Alert alert;
//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
private Form form;
private TextField number;
private TextField textUrdu;
private StringItem stringItem;
private StringItem send;
private Command exit;
private Command sendMesg;
private Command add;
private Command urdu;
private Command select;
private SimpleCancellableTask task;
//</editor-fold>//GEN-END:|fields|0|
MessageConnection clientConn;
private Display display;
public composeurdu() {
display = Display.getDisplay(this);
}
private void showMessage(){
display=Display.getDisplay(this);
//numb=number.getString();
if(number.getString().length()==0 || textUrdu.getString().length()==0){
Alert alert=new Alert("error ");
alert.setString(" Enter phone number");
alert.setTimeout(5000);
display.setCurrent(alert);
}
else if(number.getString().length()>11){
Alert alert=new Alert("error ");
alert.setString("invalid number");
alert.setTimeout(5000);
display.setCurrent(alert);
}
else{
Alert alert=new Alert("error ");
alert.setString("success");
alert.setTimeout(5000);
display.setCurrent(alert);
}
}
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);
}
//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
//</editor-fold>//GEN-END:|methods|0|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
/**
* Initializes the application. It is called only once when the MIDlet is
* started. The method is called before the
* <code>startMIDlet</code> method.
*/
private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
// write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
// write post-initialize user code here
}//GEN-BEGIN:|0-initialize|2|
//</editor-fold>//GEN-END:|0-initialize|2|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
/**
* Performs an action assigned to the Mobile Device - MIDlet Started point.
*/
public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
// write pre-action user code here
switchDisplayable(null, getForm());//GEN-LINE:|3-startMIDlet|1|3-postAction
// write post-action user code here
form.setCommandListener(this);
form.setItemStateListener(this);
}//GEN-BEGIN:|3-startMIDlet|2|
//</editor-fold>//GEN-END:|3-startMIDlet|2|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
/**
* Performs an action assigned to the Mobile Device - MIDlet Resumed point.
*/
public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
// write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
// write post-action user code here
}//GEN-BEGIN:|4-resumeMIDlet|2|
//</editor-fold>//GEN-END:|4-resumeMIDlet|2|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
/**
* Switches a current displayable in a display. The
* <code>display</code> instance is taken from
* <code>getDisplay</code> method. This method is used by all actions in the
* design for switching displayable.
*
* @param alert the Alert which is temporarily set to the display; if
* <code>null</code>, then
* <code>nextDisplayable</code> is set immediately
* @param nextDisplayable the Displayable to be set
*/
public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
// write pre-switch user code here
Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}//GEN-END:|5-switchDisplayable|1|5-postSwitch
// write post-switch user code here
}//GEN-BEGIN:|5-switchDisplayable|2|
//</editor-fold>//GEN-END:|5-switchDisplayable|2|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
/**
* Called by a system to indicated that a command has been invoked on a
* particular displayable.
*
* @param command the Command that was invoked
* @param displayable the Displayable where the command was invoked
*/
public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
// write pre-action user code here
if (displayable == form) {//GEN-BEGIN:|7-commandAction|1|16-preAction
if (command == exit) {//GEN-END:|7-commandAction|1|16-preAction
// write pre-action user code here
exitMIDlet();//GEN-LINE:|7-commandAction|2|16-postAction
// write post-action user code here
} else if (command == sendMesg) {//GEN-LINE:|7-commandAction|3|18-preAction
// write pre-action user code here
String mno=number.getString();
String msg=textUrdu.getString();
if(mno.equals("")) {
alert = new Alert("Alert");
alert.setString("Enter Mobile Number!!!");
alert.setTimeout(2000);
display.setCurrent(alert);
}
else {
try {
clientConn=(MessageConnection)Connector.open("sms://"+mno);
}
catch(Exception e) {
alert = new Alert("Alert");
alert.setString("Unable to connect to Station because of network problem");
alert.setTimeout(2000);
display.setCurrent(alert);
}
try {
TextMessage textmessage = (TextMessage) clientConn.newMessage(MessageConnection.TEXT_MESSAGE);
textmessage.setAddress("sms://"+mno);
textmessage.setPayloadText(msg);
clientConn.send(textmessage);
}
catch(Exception e)
{
Alert alert=new Alert("Alert","",null,AlertType.INFO);
alert.setTimeout(Alert.FOREVER);
alert.setString("Unable to send");
display.setCurrent(alert);
}
}
//GEN-LINE:|7-commandAction|4|18-postAction
// write post-action user code here
}//GEN-BEGIN:|7-commandAction|5|7-postCommandAction
}//GEN-END:|7-commandAction|5|7-postCommandAction
// write post-action user code here
}//GEN-BEGIN:|7-commandAction|6|
//</editor-fold>//GEN-END:|7-commandAction|6|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Items ">//GEN-BEGIN:|8-itemCommandAction|0|8-preItemCommandAction
/**
* Called by a system to indicated that a command has been invoked on a
* particular item.
*
* @param command the Command that was invoked
* @param displayable the Item where the command was invoked
*/
public void commandAction(Command command, Item item) {//GEN-END:|8-itemCommandAction|0|8-preItemCommandAction
// write pre-action user code here
if (item == number) {//GEN-BEGIN:|8-itemCommandAction|1|21-preAction
if (command == add) {//GEN-END:|8-itemCommandAction|1|21-preAction
// write pre-action user code here
//GEN-LINE:|8-itemCommandAction|2|21-postAction
// write post-action user code here
}//GEN-BEGIN:|8-itemCommandAction|3|28-preAction
} else if (item == send) {
if (command == select) {//GEN-END:|8-itemCommandAction|3|28-preAction
// write pre-action user code here
//GEN-LINE:|8-itemCommandAction|4|28-postAction
// write post-action user code here
}//GEN-BEGIN:|8-itemCommandAction|5|24-preAction
} else if (item == textUrdu) {
if (command == urdu) {//GEN-END:|8-itemCommandAction|5|24-preAction
// write pre-action user code here
if (isUrdu)
isUrdu = false;
else
{
isUrdu = true;
TextField tf = (TextField)item;
}
//GEN-LINE:|8-itemCommandAction|6|24-postAction
// write post-action user code here
}//GEN-BEGIN:|8-itemCommandAction|7|8-postItemCommandAction
}//GEN-END:|8-itemCommandAction|7|8-postItemCommandAction
// write post-action user code here
}//GEN-BEGIN:|8-itemCommandAction|8|
//</editor-fold>//GEN-END:|8-itemCommandAction|8|
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: form ">//GEN-BEGIN:|14-getter|0|14-preInit
/**
* Returns an initialized instance of form component.
*
* @return the initialized component instance
*/
public Form getForm() {
if (form == null) {//GEN-END:|14-getter|0|14-preInit
// write pre-init user code here
form = new Form("form", new Item[]{getNumber(), getTextUrdu(), getStringItem(), getSend()});//GEN-BEGIN:|14-getter|1|14-postInit
form.addCommand(getExit());
form.addCommand(getSendMesg());
form.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
// write post-init user code here
form.setItemStateListener(this);
// form.setCommandListener(this);
}//GEN-BEGIN:|14-getter|2|
return form;
}
//</editor-fold>//GEN-END:|14-getter|2|
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: number ">//GEN-BEGIN:|19-getter|0|19-preInit
/**
* Returns an initialized instance of number component.
*
* @return the initialized component instance
*/
public TextField getNumber() {
if (number == null) {//GEN-END:|19-getter|0|19-preInit
// write pre-init user code here
number = new TextField("Number ", null, 11, TextField.PHONENUMBER);//GEN-BEGIN:|19-getter|1|19-postInit
number.addCommand(getAdd());
number.setItemCommandListener(this);
number.setDefaultCommand(getAdd());//GEN-END:|19-getter|1|19-postInit
// write post-init user code here
}//GEN-BEGIN:|19-getter|2|
return number;
}
//</editor-fold>//GEN-END:|19-getter|2|
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: textUrdu ">//GEN-BEGIN:|22-getter|0|22-preInit
/**
* Returns an initialized instance of textUrdu component.
*
* @return the initialized component instance
*/
public TextField getTextUrdu() {
if (textUrdu == null) {//GEN-END:|22-getter|0|22-preInit
// write pre-init user code here
textUrdu = new TextField("Message", null, 2000, TextField.ANY);//GEN-BEGIN:|22-getter|1|22-postInit
textUrdu.addCommand(getUrdu());
textUrdu.setItemCommandListener(this);//GEN-END:|22-getter|1|22-postInit
// write post-init user code here
}//GEN-BEGIN:|22-getter|2|
return textUrdu;
}
//</editor-fold>//GEN-END:|22-getter|2|
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: exit ">//GEN-BEGIN:|15-getter|0|15-preInit
/**
* Returns an initialized instance of exit component.
*
* @return the initialized component instance
*/
public Command getExit() {
if (exit == null) {//GEN-END:|15-getter|0|15-preInit
// write pre-init user code here
exit = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|15-getter|1|15-postInit
// write post-init user code here
}//GEN-BEGIN:|15-getter|2|
return exit;
}
//</editor-fold>//GEN-END:|15-getter|2|
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: sendMesg ">//GEN-BEGIN:|17-getter|0|17-preInit
/**
* Returns an initialized instance of sendMesg component.
*
* @return the initialized component instance
*/
public Command getSendMesg() {
if (sendMesg == null) {//GEN-END:|17-getter|0|17-preInit
// write pre-init user code here
sendMesg = new Command("send", Command.OK, 0);//GEN-LINE:|17-getter|1|17-postInit
// write post-init user code here
}//GEN-BEGIN:|17-getter|2|
return sendMesg;
}
//</editor-fold>//GEN-END:|17-getter|2|
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: add ">//GEN-BEGIN:|20-getter|0|20-preInit
/**
* Returns an initialized instance of add component.
*
* @return the initialized component instance
*/
public Command getAdd() {
if (add == null) {//GEN-END:|20-getter|0|20-preInit
// write pre-init user code here
add = new Command("add", Command.OK, 0);//GEN-LINE:|20-getter|1|20-postInit
// write post-init user code here
}//GEN-BEGIN:|20-getter|2|
return add;
}
//</editor-fold>//GEN-END:|20-getter|2|
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: urdu ">//GEN-BEGIN:|23-getter|0|23-preInit
/**
* Returns an initialized instance of urdu component.
*
* @return the initialized component instance
*/
public Command getUrdu() {
if (urdu == null) {//GEN-END:|23-getter|0|23-preInit
// write pre-init user code here
urdu = new Command("urdu", Command.OK, 0);//GEN-LINE:|23-getter|1|23-postInit
// write post-init user code here
}//GEN-BEGIN:|23-getter|2|
return urdu;
}
//</editor-fold>//GEN-END:|23-getter|2|
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItem ">//GEN-BEGIN:|25-getter|0|25-preInit
/**
* Returns an initialized instance of stringItem component.
*
* @return the initialized component instance
*/
public StringItem getStringItem() {
if (stringItem == null) {//GEN-END:|25-getter|0|25-preInit
// write pre-init user code here
stringItem = new StringItem("string", null);//GEN-LINE:|25-getter|1|25-postInit
// write post-init user code here
}//GEN-BEGIN:|25-getter|2|
return stringItem;
}
//</editor-fold>//GEN-END:|25-getter|2|
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: send ">//GEN-BEGIN:|26-getter|0|26-preInit
/**
* Returns an initialized instance of send component.
*
* @return the initialized component instance
*/
public StringItem getSend() {
if (send == null) {//GEN-END:|26-getter|0|26-preInit
// write pre-init user code here
send = new StringItem("", "send", Item.BUTTON);//GEN-BEGIN:|26-getter|1|26-postInit
send.addCommand(getSelect());
send.setItemCommandListener(this);
send.setDefaultCommand(getSelect());//GEN-END:|26-getter|1|26-postInit
// write post-init user code here
}//GEN-BEGIN:|26-getter|2|
return send;
}
//</editor-fold>//GEN-END:|26-getter|2|
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: select ">//GEN-BEGIN:|27-getter|0|27-preInit
/**
* Returns an initialized instance of select component.
*
* @return the initialized component instance
*/
public Command getSelect() {
if (select == null) {//GEN-END:|27-getter|0|27-preInit
// write pre-init user code here
select = new Command("select", Command.OK, 0);//GEN-LINE:|27-getter|1|27-postInit
// write post-init user code here
}//GEN-BEGIN:|27-getter|2|
return select;
}
//</editor-fold>//GEN-END:|27-getter|2|
//<editor-fold defaultstate="collapsed" desc=" Generated Getter: task ">//GEN-BEGIN:|40-getter|0|40-preInit
/**
* Returns an initialized instance of task component.
*
* @return the initialized component instance
*/
public SimpleCancellableTask getTask() {
if (task == null) {//GEN-END:|40-getter|0|40-preInit
// write pre-init user code here
task = new SimpleCancellableTask();//GEN-BEGIN:|40-getter|1|40-execute
task.setExecutable(new org.netbeans.microedition.util.Executable() {
public void execute() throws Exception {//GEN-END:|40-getter|1|40-execute
// write task-execution user code here
}//GEN-BEGIN:|40-getter|2|40-postInit
});//GEN-END:|40-getter|2|40-postInit
// write post-init user code here
}//GEN-BEGIN:|40-getter|3|
return task;
}
//</editor-fold>//GEN-END:|40-getter|3|
/**
* Returns a display instance.
* @return the display instance.
*/
public Display getDisplay () {
return Display.getDisplay(this);
}
/**
* Exits MIDlet.
*/
public void exitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}
/**
* Called when MIDlet is started.
* Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
*/
public void startApp() {
startMIDlet();
display.setCurrent(form);
}
/**
* Called when MIDlet is paused.
*/
public void pauseApp() {
midletPaused = true;
}
/**
* Called to signal the MIDlet to terminate.
* @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
*/
public void destroyApp(boolean unconditional) {
}
public void itemStateChanged(Item item) { if (item == textUrdu)
{
if (isUrdu)
{
stringItem.setText("urdu");
TextField tf = (TextField)item;
String s = tf.getString();
char ch = s.charAt(s.length() - 1);
s = s.substring(0, s.length() - 1);
ch = Urdu.ToUrdu(ch);
s = s + String.valueOf(ch);
tf.setString("");
tf.setString(s);
}//end if
throw new UnsupportedOperationException("Not supported yet.");
}
}
}