PushRegistry SMS Listener

I have attached a small code snippet which implements an SMS listener. For more details on PushRegistry click here. This will invoke notifyIncomingMessage() of the MessageListener. This code will not create a permanent entry in the registry, but will listen to the specified port as long as the application is alive.

package subin.rnd.mobile.game;

import javax.microedition.io.Connector;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.MessageListener;
import javax.wireless.messaging.TextMessage;

public class SmsListener
implements MessageListener
{
    private int port;
    private MessageConnection messageConnection;

    public SmsListener(int port)
    {
        try
        {
            this.messageConnection = (MessageConnection) Connector.open("sms://:" + port);
            this.messageConnection.setMessageListener(this);
        }
        catch (Exception e)
        {
            System.out.println("Exception: " + e);
        }
    }

    public void notifyIncomingMessage(MessageConnection messageConnection)
    {
        System.out.println("Recieved SMS from " + messageConnection);

        new Thread()
        {
            MessageConnection conn;
            Thread set(MessageConnection conn)
            {
                this.conn = conn;
                return (this);
            }
            public void run()
            {
                try
                {
                    TextMessage msg = (TextMessage) conn.receive();
                    System.out.println("Recieved SMS: " + msg.getPayloadText());
                }
                catch (Exception e)
                {
                    System.out.println("SMS recieve Exception " + e);
                }
            }
        }.set(messageConnection).start();
    }
}
Advertisements