J2ME SMS : Sending SMS in J2ME

/************************************************** ***
J2ME SMS
Sending SMS in J2ME


insert this line into JAD file:
Port-SMS: 50056


************************************************** ****/

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

public class SendSMSextends MIDlet
implements CommandListener, Runnable {

private Display display;
private Command cmdExit, cmdContinue, cmdBack, cmdSend;
private Displayable prevScreen;
private TextBox tfNum, tfText;
private String port;
private Thread thread;
private Alert alert;

private final String INVALID_PHONE =
"Nomor yang dimasukkan tidak absah";

public SendSMS() {
display = Display.getDisplay(this);

port = "50056";
if(getAppProperty("Port-SMS") != null)
port = getAppProperty("Port-SMS");

cmdExit = new Command("Exit", Command.EXIT, 1);
cmdContinue = new Command("Continue", Command.SCREEN, 1);
cmdBack = new Command("Back", Command.BACK, 1);
cmdSend = new Command("Send", Command.SCREEN, 1);

alert = new Alert(null);
alert.setTimeout(3000);
}

public void startApp() {
tfNum = generatePhoneInput();
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void commandAction(Command c, Displayable s) {
if (c == cmdExit) {
destroyApp(false);
notifyDestroyed();
} else if (c == cmdBack) {
display.setCurrent(tfNum);
} else if (c == cmdContinue) {
if (!isValidPhoneNumber(tfNum.getString())) {
alert.setType(AlertType.ERROR);
alert.setString(INVALID_PHONE);
display.setCurrent(alert,tfNum);
} else {
tfText = generateMessageInput();
}
} else {
thread = new Thread(this);
thread.start();
}
}

public void run() {
MessageConnection conn = null;
String address= "sms://" + tfNum.getString() + ":" + port;
try {
conn = (MessageConnection) Connector.open(address);
TextMessage msg = (TextMessage)
conn.newMessage(MessageConnection.TEXT_MESSAGE);
msg.setAddress(address);
msg.setPayloadText(tfText.getString());
conn.send(msg);
alert.setString("Sending to " +
address.substring(6));
alert.setType(AlertType.INFO);
display.setCurrent(alert);
} catch (IOException ioe) {
ioe.printStackTrace();
}
if (conn != null) {
try {
conn.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}

public TextBox generatePhoneInput() {
TextBox tfPhone = new TextBox("Number?", null,
15, TextField.PHONENUMBER);
tfPhone.addCommand(cmdExit);
tfPhone.addCommand(cmdContinue);
tfPhone.setCommandListener(this);
display.setCurrent(tfPhone);
return tfPhone;
}

public TextBox generateMessageInput() {
TextBox tfMessage = new TextBox("Text Message", null,
500, TextField.ANY);
tfMessage.addCommand(cmdBack);
tfMessage.addCommand(cmdSend);
tfMessage.setCommandListener(this);
display.setCurrent(tfMessage);
return tfMessage;
}

private static boolean isValidPhoneNumber(String number) {
char[] chars = number.toCharArray();
if (chars.length == 0) {
return false;
}
int startPos = 0;
//+
if (chars[0] == '+') {
startPos = 1;
}
for (int i = startPos; i < chars.length; ++i) {
if (!Character.isDigit(chars[i])) {
return false;
}
}
return true;
}
}

30 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. we are not getting SMSSender class so we are getting error in receiving code.Can u please help us

    ReplyDelete
  3. terima kasih atas scriptnya,,
    mau nanya mas? ada nggak script untuk membuat aplikasi adzan yang berbasis j2me, karena saya lagi butuh ni buat sebagian dari TA saya.
    kalo ada tolong mas kirim ke email saya.
    syarif_27@yahoo.co.id
    makasih...

    ReplyDelete
  4. could you send me the working project pls? (androuis@yahoo.co.uk)

    ReplyDelete
  5. Question:

    Gimana caranya mengirim pesan ke >1 nomor?

    ReplyDelete
  6. Through j2me i need to connect to Googlemaps
    pls tell me the code to connect to Google maps.
    (Specify clearly)...send the code to abhilashbaddam@gmail.com this mail id. Thanks in advance...

    ReplyDelete
  7. sudah di coba, berjalan baik.
    tapi bagaimana script untuk mengirimkan pesan ke beberapa nomor?
    yoseph.dani@gmail.com

    ReplyDelete
  8. Ada yang tau gak script dwl atau java untuk buat block sms masuk ke gsm modem seperti maestro, intercel / fastrack
    angin_16@yahoo.co.id

    ReplyDelete
  9. I want code for sending picture msg not MMS
    plz mail me
    rujul_trivedi07@yahoo.co.in

    ReplyDelete
  10. to j2me developers, Please help how to encrypt sms sending.Please mail me a sample codes if any,Thanks

    ReplyDelete
  11. to j2me developers, Please help how to encrypt sms sending.Please mail me a sample codes if any,may email eduardo.delito@yahoo.com Thanks

    ReplyDelete
  12. Hi there.. Can you send me a working project? please send it to janmichael_bais@yahoo.com.. We need SMS for our thesis though. I'm looking forward for your reply

    ReplyDelete
  13. hi..
    can u please tell me how to write into the JAD file?

    ReplyDelete
  14. hi
    Can you send me code please send it to s.al_tamimi@hotmail.com We need send SMS from computer to mobile (is a text sms) and send turn or off the computer from mobile
    and please tell me how to write into the JAD file
    please I wetting u

    ReplyDelete
  15. Its Not Working in Real World Env..
    How can i know the Port no. of receiver phone???

    it may vary from provider to provider..

    ReplyDelete
  16. i need code to send sms from j2me(mobile) to gmail inbox.....plz send me the code...thanks in advance...

    ReplyDelete
  17. i need code to send sms from j2me(mobile) to gmail inbox.....plz send me the code...thanks in advance... saritha_17041988@rediffmail.com

    ReplyDelete
  18. i need code to send sms from j2me(mobile) to gmail inbox.....plz send me the code...thanks in advance

    mauga25@yahoo.com

    ReplyDelete
  19. I need code for getting tower information from mobile using j2me pls help me. gunasekaran.arts@gmail.com

    ReplyDelete
  20. i need a code in browsing inbox messages in j2me..pls..tnx

    ReplyDelete
  21. can u please tell me how to write into the JAD file?

    ReplyDelete
  22. I NEED CODE FOR BANKING THROUGHT MOBILE...
    CAN YOU HELP ME??

    ReplyDelete
  23. We need send SMS from computer to mobile (is a text sms) and send turn or off the computer from mobile

    sneharsh10@gmail.com

    ReplyDelete
  24. I need the J2ME source of the data collection mobile server and then mailed to her. Thanks a lot everyone. My email: thuhien_0811@yahoo.com

    ReplyDelete
  25. Have an error in public SendSMS() code.Can u please help us
    My email: hayvebenanh_boy91@yahoo.com

    ReplyDelete
  26. Have some error
    '{' expected
    public class SendSMSextends MIDlet implements Runnable {
    and
    return type required
    public SendSMS() {

    can u help us! Thanks!
    My email: vovanbacnd@gmail.com

    ReplyDelete
    Replies
    1. seperate SendSMSextends as SendSMS extend and re-run it.. errors gone

      Delete
  27. it shows msg gone bt how to receive it on other phone pls help..

    ReplyDelete
  28. can u send me the working application code?
    Thanx in advance.. (goelritanshu1991@gmail.com)

    ReplyDelete

 

Design by Blogger Buster | Distributed by Blogging Tips