Shumaila Malik
Shumaila Malik

Reputation: 21

how to receive sms in netbeans mobile application

I am developing a mobile application in net-beans that will send and receive SMS, I'm done with sending text but I don't know how to receive SMS in NetBeans mobile application ?

Upvotes: 1

Views: 1411

Answers (1)

Lucifer
Lucifer

Reputation: 29670

WMA (Wireless Messaging API) is a wireless messaging api defined in MIDP 2.0. These apis are designed to handle text, binary and multipart messages. To make a connection, the application obtains an object implementing the MessageConnection from the Connector class by providing an URL connection string that identifies the address.

/* Make a connection */
public boolean connectSMSServer() 
{
  try 
  {
    messageConnection messageConnection = 
      (MessageConnection)Connector.open("sms://:" + port);
    messageConnection.setMessageListener(this);
  }
  catch (Exception e) {
  }
}

/* Send text message */
public void sendTextmessage(String address,String message) 
{
  try 
  {
    //creates a new TextMessage
    TextMessage textMessage = (TextMessage)messageConnection.newMessage(
            MessageConnection.TEXT_MESSAGE, address);
    textMessage.setPayloadText(message);
    messageConnection.send(textMessage);
  } 
  catch (Exception e) {
  }
}

/* Recieve text message */
public void receiveTextMessage() 
{
  try 
  {
    Message message = messageConnection.receive();
    if (message instanceof TextMessage) 
    {
      TextMessage textMessage = (TextMessage)message;
    } 
    else 
    {
      //Message can be binary or multipart
    }                   
  } 
  catch (Exception e) {
  }
}

/* Notify Incoming Message */
public synchronized void notifyIncomingMessage(MessageConnection conn) 
{
  //notiy thread of incoming message
  synchronized (this)
  {
    notify();
  }
}

/* Close Connection */
public void closeConnection() 
{
  if (messageConnection != null) 
  {
    try 
    {
      messageConnection.setMessageListener(null);
      messageConnection.close();
    } 
    catch (Exception e) {
    }
    }
  }
}

When you are coding for Receiving SMS, you need to listen to one particular port. J2ME Application can not read directly from the inbox.

Upvotes: 3

Related Questions