DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Umut has posted 1 posts at DZone. View Full User Profile

Sending/Receiving SMS From MIDlets

02.07.2008
| 31948 views |
  • submit to reddit
        
// Lets send an SMS now.
//get a reference to the appropriate Connection object using an appropriate url
MessageConnection conn = (MessageConnection) Connector.open("sms://:50001");
//generate a new text message
TextMessage tmsg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
//set the message text and the address
tmsg.setPayloadText(message);
tmsg.setAddress("sms://" + number);
//finally send our message
conn.send();

// Time to receive one.
//get reference to MessageConnection object
MessageConnection conn = (MessageConnection) Connector.open("sms://:50001");
//set message listener
conn.setMessageListener(
   new MessageListener() {
      public void notifyIncomingMessage(MessageConnection conn) {
         Message msg = conn.receive();
         //do whatever you want with the message
         if(msg instanceof TextMessage) {
            TextMessage tmsg = (TextMessage) msg;
            System.out.println(tmsg.getPayloadText());
         } else if(msg instanceof BinaryMessage) {
            .....
         } else {
            ......
         }
      }
   }
);
    

Comments

Snippets Manager replied on Sun, 2010/05/02 - 10:46am

Hello. I've got a minor problem with this code. Getting a error at the last bit of 'sender', "conn.send() - send(javax.wireless.messaging.Message) in javax.wireless.messaging.MessageConnection cannot be applied to ()" Need any more imports to run it besides "javax.wireless.messaging.*;" ? Cheers.

Umut UTKAN replied on Thu, 2008/02/07 - 6:40am

I am not sure that the port have to be 50001 or not, but it is working on my phone. Connector.open("sms://:50001");