【问题标题】:Getting error while implement OutboundMessageListener in blackberry?在黑莓中实现 OutboundMessageListener 时出错?
【发布时间】:2013-07-16 05:48:04
【问题描述】:

使用此代码使用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: 在客户端连接上不允许操作

请帮忙解决这个问题?

【问题讨论】:

    标签: java blackberry java-me blackberry-eclipse-plugin


    【解决方案1】:

    查看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 中指定了 端口。我还建议在真实设备上进行测试,而不是模拟器。

    【讨论】:

    • 这将在发送和接收短信中实现
    • @AmanAroraBB,OutboundMessageListener 也是MessageListener,所以只需添加notifyIncomingMessage() 方法。关键是要注意我在回答的结尾所说的话
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-02
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多