Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
james9
Posts: 3
Registered: ‎10-23-2008
My Device: Not Specified
Accepted Solution

notifyIncomingMessage only called for first SMS

Hello,

 

I'm trying to detect incoming SMS's but the problem is I only receive the first one that is sent after my program starts, the rest all go straight to the inbox and ignore my program, I've tried attaching the debugger to the device and as I suspected the 2nd SMS never hits the breakpoint. Hopefully someone can answer some questions, I looked at the knowledgebase and it gives 3 ways to listen for SMS:

 

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800563/What_Is...

 

Are there any differences at all, and which is the best one to use? Also are there any differences between the port I choose in the open string, one example I saw had  "sms://:6222" and another had a different number. However I couldn't even receive a single SMS until I used "sms://:0".

 

I guess 0 listens for all SMSs and the numbers listen exclusively on that particular port, if so what is the port SMSs actually use by default, since I've never seen anything in my life that let me choose the port I sent an SMS on. Does this change from network to network?

 

Here's the code, it is the same as

http://www.j2mepolish.org/javadoc/j2me/de/enough/polish/messaging/MessageListener.html

 

Pause or destroy are never called, in my main class I call:

_smsListener = new SmsListener(this);
_smsListener.startApp();

 

and then never refer to it again.

 


public class SmsListener implements MessageListener
{
    MessageConnection messconn;
    boolean done;
    Reader reader;
    Screen _screen;
    
    public SmsListener(Screen screen)
    {
        _screen = screen;
    }
    
    // Initial tests setup and execution.
    public void startApp()
    {
        try
        {
            // Get our receiving port connection.
            messconn = (MessageConnection)Connector.open("sms://:0");
            // Register a listener for inbound messages.
            messconn.setMessageListener(this);
            // Start a message-reading thread.
            done = false;
            reader = new Reader();
            new Thread(reader).start();
        }
        catch (IOException e)
        {
             //Handle startup errors
        }
    }
    
    // Asynchronous callback for inbound message.
    public void notifyIncomingMessage(MessageConnection conn)
    {
        if (conn == messconn)
        {
            reader.handleMessage();
        }
    }
    
    // Required MIDlet method - release the connection and
    // signal the reader thread to terminate.
    public void pauseApp()
    {
        done = true;
        try
        {
            messconn.close();
        }
        catch (IOException e)
        {
            // Handle errors
        }
    }
    
    // Required MIDlet method - shutdown.
    // @param unconditional forced shutdown flag
    public void destroyApp(boolean unconditional)
    {
        done = true;
        try
        {
            messconn.setMessageListener(null);
            messconn.close();
        }
        catch (IOException e)
        {
            // Handle shutdown errors.
        }
    }
    
    // Isolate blocking I/O on a separate thread, so callback
    // can return immediately.
    class Reader implements Runnable
    {
        private int pendingMessages = 0;
        
        // The run method performs the actual message reading.
        public void run()
        {
            while (!done)
            {
                synchronized(this)
                {
                    if (pendingMessages == 0)
                    {
                        try
                        {
                            wait();
                        }
                        catch (Exception e)
                        {
                            // Handle interruption
                        }
                    }
                    pendingMessages--;
                }
                
                // The benefit of the MessageListener is here.
                // This thread could via similar triggers be
                // handling other kind of events as well in
                // addition to just receiving the messages.
                
                try
                {
                    Message mess = messconn.receive();
                    _screen.receivedSmsMessage(mess);
                }
                catch (IOException ioe)
                {
                    // Handle reading errors
                }
            }
        }
        
        public synchronized void handleMessage()
        {
            pendingMessages++;
            notify();
        }
        
    }
}

BlackBerry Development Advisor
MSohm
Posts: 14,864
Registered: ‎07-09-2008
My Device: BlackBerry Passport

Re: notifyIncomingMessage only called for first SMS

The port you are listening on must match the port the SMS message was sent on.  0 can be considered the default port.  Note that all carriers do not support sending SMS messages to a specific port.

 

 

What BlackBerry model and handheld software version are you testing on?  You can find this under Options, About on the BlackBerry.  Are you using the BlackBerry Simulator or a real BlackBerry device?

 

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
New Developer
james9
Posts: 3
Registered: ‎10-23-2008
My Device: Not Specified

Re: notifyIncomingMessage only called for first SMS

The about box says:

 

BlackBerry 8310

smartphone (EDGE)

v4.2.2.181 (Platform 2.5.0.35)

BlackBerry Development Advisor
MSohm
Posts: 14,864
Registered: ‎07-09-2008
My Device: BlackBerry Passport

Re: notifyIncomingMessage only called for first SMS

Please try running the smsdemo included with the BlackBerry JDE and let me know if you see similar results.  Note that you'll need to change the port to 0 to capture default SMS messages sent to the BlackBerry device.
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
New Developer
james9
Posts: 3
Registered: ‎10-23-2008
My Device: Not Specified

Re: notifyIncomingMessage only called for first SMS

It worked and I figured out what the problem was, the smsdemo was updating the UI with

 

invokeLater(new Runnable()
{
    public void run()
    {

        // ui stuff

    }

}

 

Thanks