Getting error while using OutboundMessageListener and MessageListener by using this code:

public class MainClass extends UiApplication implements OutboundMessageListener,MessageListener
{
    public static void main(String[] args)
    {
        MainClass mainClass = new MainClass();
        mainClass.enterEventDispatcher();
    }

    public MainClass()
    {
        try
        {
            MessageConnection _mc = (MessageConnection)Connector.open("sms://");
            _mc.setMessageListener(this);
        }
        catch (IOException e)
        {
        }
        UiApplication.getUiApplication().pushScreen(new SmsCountScreen());
    }

    public void notifyIncomingMessage(MessageConnection conn)
    {
        UiApplication.getUiApplication().invokeAndWait(new Runnable()
        {
            public void run()
            {
                Dialog dialog = new Dialog(Dialog.D_OK, "Message Received!", 0, null,    Dialog.FIELD_HCENTER);
                Ui.getUiEngine().pushGlobalScreen(dialog, 1, UiEngine.GLOBAL_MODAL);
            }
        });
    }

    public void notifyOutgoingMessage(Message message)
    {
        UiApplication.getUiApplication().invokeAndWait(new Runnable()
        {
            public void run()
            {
                Dialog dialog = new Dialog(Dialog.D_OK, "Message Sent!", 0, null, Dialog.FIELD_HCENTER);
                Ui.getUiEngine().pushGlobalScreen(dialog, 1, UiEngine.GLOBAL_MODAL);
            }
        });
    }
}

using this code and getting error

IOException: operation not permitted on a client connection

Please help to solve this?

有帮助吗?

解决方案

Looking at this example on the BlackBerry support forums, they use this code:

public class MyMessageListener implements OutboundMessageListener
{
    public void notifyOutgoingMessage(javax.wireless.messaging.Message m)
    {
        try {
            String msg = null;
            msg = getMessage(m); // my call to convert Message to String
            //... process msg
        }
        catch(Exception ex) {
            // handle exception
        }
    }

    public void notifyIncomingMessage(MessageConnection conn) 
    {
        // handle received sms here
    }
}

to register the listener

MyMessageListener ml = new MyMessageListener();
MessageConnection mc;
try {
    mc = (MessageConnection)Connector.open("sms://:0");
    mc.setMessageListener(el);
} catch (Exception e) {
    // handle exception
}

Note that the port is specified in the Connection.open() URL. I'd also recommend testing this on a real device, not the simulators.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top