Code for sending SMS using J2ME

Can someone help me with the code to send SMS using J2ME?

thanks

+4
source share
2 answers

You can try the code below to implement this:

private boolean SendSMS(String sPhoneNo, String sMessage) { boolean result = true; try { String addr = "sms://" + sPhoneNo; MessageConnection conn = (MessageConnection) Connector.open(addr); TextMessage msg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE); msg.setPayloadText(sMessage); conn.send(msg); conn.close(); } catch (SecurityException se) { result = false; } catch (Exception e) { result = false; } return result; } 

You can specify any special port by simply adding ":port_no" after:

 "String addr = "sms://" + sPhoneNo" 
+4
source

Post this topic to send SMS

 public class SendSMS extends Thread { private String receiver; private String receivedMsg; private HomeScreen home; private boolean bool = false; private boolean notsent; public SendSMS(HomeScreen gen, String msg, String number) { this.home = gen; this.receiver = number; this.receivedMsg = msg; } public void run() { while (!bool) { SendMessage(); } } /** * Send the mesage using WMA api. */ private void SendMessage() { String s = "sms://" + receiver; send(s); } private void send(String url) { MessageConnection messageconnection = null; try { messageconnection = (MessageConnection) Connector.open(url); TextMessage textmessage = (TextMessage) messageconnection.newMessage(MessageConnection.TEXT_MESSAGE); textmessage.setAddress(url); textmessage.setPayloadText(receivedMsg); messageconnection.send(textmessage); } catch (Exception throwable) { notsent = true; home.genericObject.setSmsStatus(false); if (!home.isNokia()) { new PopUp("Message not sent"); // not sent } bool = true; try { messageconnection.close(); } catch (Exception e) { } } if (messageconnection != null) { try { messageconnection.close(); if (!notsent) { home.genericObject.setSmsStatus(false); if (!home.isNokia()) { new PopUp("Message Sent"); // sent } } bool = true; } catch (Exception ie) { ie.printStackTrace(); } } } } 

Nokia devices do not display a system alert if a message is sent from j2me. Therefore, if you want to show a warning, you need to create your own PopUp and show.

+3
source

All Articles