MessageListener时出错

MessageListener时出错

使用以下代码在使用OutboundMessageListenerMessageListener时出错:

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);
            }
        });
    }
}


使用此代码并出现错误


  IOException:不允许在客户端连接上进行操作


请帮忙解决这个问题?

最佳答案

查看this example on the BlackBerry support forums,他们使用以下代码:

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
    }
}


注册听众

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


请注意,端口是在Connection.open() URL中指定的。我还建议您在真实的设备上而不是在模拟器上进行测试。

10-07 20:47