Code sample to Send SMS from a J2ME application.

By: Leon Emailed: 1788 times Printed: 2623 times    

A java application, where on making a Push to talk call, and the call does not get matured due the the callers phone being busy in another call, busy in packet data, switched off or not in coverage, the java appplication installed in the callers phone should send out a sms, automatically to the called number. The number to send the SMS is taken from the phone book for the called number.

Here at at first we created a form called send sms and added commands to exit and send.

On clicking the send the control goes to public void sendit(String paramString1, String paramString2). where it first creates a connection with defined addree and and then creates an empty msg of type text. We then added the text that has to be sent and the pass that in the url of the address

 public void startApp()
 {
   this.display = Display.getDisplay(this);
   this.form = new Form("send sms");
   this.text = new TextField("number", "", 256, 0);
   this.send = new Command("send", 4, 1);
   this.exit = new Command("exit", 3, 2);
   this.form.append(this.text);
   this.form.addCommand(this.send);
   this.form.addCommand(this.exit);
   this.form.setCommandListener(this);
   this.display.setCurrent(this.form);
 }

 public void pauseApp()
 {
 }

 public void destroyApp(boolean paramBoolean)
 {
 }

 public void commandAction(Command paramCommand, Displayable
paramDisplayable) {
   if (paramCommand == this.exit) {
     notifyDestroyed();
   }
   if (paramCommand == this.send) {
     this.number = this.text.getString();
     System.out.println("in loop" + this.number);
     this.message1 = "please call me back";
     sendit(this.message1, this.number);
   }
 }

 public void sendit(String paramString1, String paramString2)
 {
   this.phoneNumber = paramString2;
   this.message = paramString1;
   StringBuffer localStringBuffer = new StringBuffer(20);

   String str = "sms://" + paramString2 ";

   this.form2 = new Form("");
   MessageConnection localMessageConnection = null;
   try
   {
     localMessageConnection = (MessageConnection)Connector.open(str);

     TextMessage localTextMessage =
(TextMessage)localMessageConnection.newMessage("text");
     localTextMessage.setAddress(str);
     localTextMessage.setPayloadText(paramString1);
     localMessageConnection.send(localTextMessage);

     this.form2.append("message sent");
     this.display.setCurrent(this.form2);
   }
   catch (Exception localException) {
     this.form2.append("phone number" + str + localException.getMessage());
     this.display.setCurrent(this.form2);
   }
 }

Most Viewed Articles (in J2ME )

Latest Articles (in J2ME)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. hi! im new in j2me applications so i just cleaned
View Tutorial          By: resego at 2010-08-04 07:29:29

2. we want to learn j2me
View Tutorial          By: vijay at 2010-09-27 02:34:59

3. hi.I am new in j2me and really want to learn more.
View Tutorial          By: grace at 2010-10-25 10:40:32

4. hi.I am new in j2me and really want to learn more
View Tutorial          By: Deepak kumar at 2010-11-15 03:04:15

5. Hi every one. I am working on my project in j2me a
View Tutorial          By: wazir at 2010-11-26 08:27:43

6. I need code for read write mifare 1k card using No
View Tutorial          By: amarendra at 2010-12-02 21:12:08

7. Hi All,

I am new to J2ME Applicatio

View Tutorial          By: golemnagesh at 2010-12-05 22:43:50

8. Hallo every one. I am working on my project in j2m
View Tutorial          By: radhe at 2010-12-14 08:08:25

9. Hello every one. I am working on my project in j2m
View Tutorial          By: Pradeep Reddy at 2011-01-04 05:15:51

10. HI EVERY ONE I AM NEW IN J2ME SO I WANT TO LEARN S
View Tutorial          By: KALAI at 2011-04-04 03:10:32

11. Hi every one. I am working on my project in j2me a
View Tutorial          By: Anonymous at 2011-05-01 09:04:16

12. I have a source code of j2ee. but i want to conver
View Tutorial          By: bangaram at 2011-06-07 09:28:45

13. I am to learn J2ME
View Tutorial          By: ravi kumar at 2011-06-20 05:50:20

14. I do my projest in j2me.so please send me your ide
View Tutorial          By: sivasakthivel.s at 2011-06-22 13:26:54

15. please send detail informatoin about j2me.. send a
View Tutorial          By: Saranya at 2011-07-06 03:00:21

16. hello guys... plz help me in j2me i want to learn
View Tutorial          By: bishnu prakash at 2011-07-11 13:18:39

17. hiii frds i want to run this program can i run usi
View Tutorial          By: arun kumar ojha at 2011-07-20 00:59:10

18. pls help me build a jar application for sending bu
View Tutorial          By: sam at 2011-08-13 11:31:03

19. hi..i did a sms sending pgm..it works without erro
View Tutorial          By: Ragesh Gopal at 2011-09-02 10:02:59

20. i am new i dont know how to make a program so ple
View Tutorial          By: imran khan at 2011-10-09 09:44:55

21. Very Nice cood works very well

Lea

View Tutorial          By: sACHIN at 2011-10-28 03:29:04

22. hi.I am new in j2me and really want to learn more
View Tutorial          By: snehal at 2011-12-01 11:46:22

23. hi.I am new in j2me and really want to learn more
View Tutorial          By: Vicky Mittal at 2012-01-05 11:45:53

24. sir , i am new in j2me and i want to learn more s
View Tutorial          By: vipin Luhach at 2012-01-19 06:43:27

25. does anyone know what exactly to use to desgn a ph
View Tutorial          By: andrew willy at 2012-01-29 14:51:13

26. anybody can help me with java me coding . . . ple
View Tutorial          By: sha at 2012-04-04 06:47:41

27. regarding 'TextMessage' and 'MessageConnection' er
View Tutorial          By: gaurav at 2012-04-24 11:57:17

28. hi i am shahid i have made SMS application in j2me
View Tutorial          By: shahid khan at 2012-06-18 13:19:06

29. hi.. glad to every one.. i'm developing j2me appli
View Tutorial          By: Lakshmanan at 2012-07-05 06:16:21

30. hi I have made a SMS application which sends more
View Tutorial          By: shahid at 2012-07-23 20:27:40

31. hi.I am new in j2me and really want to learn more
View Tutorial          By: A.vinothKumar at 2012-08-20 07:50:26

32. Can u plz send me woking application (Send sms) J
View Tutorial          By: Ritanshu Goel at 2012-09-22 05:52:49

33. Can u plz send me woking application (Send sms) J
View Tutorial          By: Ritanshu Goel at 2012-09-22 05:53:24

34. hi.I am new in j2me and really want to learn more
View Tutorial          By: vinoth kumar. A at 2012-10-26 05:20:31

35. what are the required equipments to run j2me
View Tutorial          By: messi at 2012-11-13 13:15:18

36. i want to learn j2me basic to ....
View Tutorial          By: gopiraj at 2012-12-21 11:52:01

37. vinod kumar for j2me app development we need one I
View Tutorial          By: saurabh dixit at 2013-04-18 09:59:23

38. I am new to J2ME but this was really help full and
View Tutorial          By: Subhash at 2013-05-05 08:57:38

39. i'm interest in j2me so i have to learn about j2me
View Tutorial          By: cha at 2013-10-14 13:17:22

40. This is great, hope to learn more from this commun
View Tutorial          By: Wamaguru at 2014-07-12 09:05:30

41. Hi ,
please i need j2me appliacation :

View Tutorial          By: youyou92 at 2014-12-01 18:20:52

42. go to hell
View Tutorial          By: aaaaaaaaaaaaaaaa at 2014-12-11 10:38:12

43. I was able to find good information from your

View Tutorial          By: http://geschenkefuermaenner.info at 2017-04-30 21:26:08