使用J2ME接收短信

发布于 2024-10-31 22:41:45 字数 1830 浏览 5 评论 0原文

我正在尝试制作一个 J2ME 应用程序来发送和接收短信。我已完成发送部分,但无法接收任何消息。

以下是我为接收短信​​而尝试的操作;

    try {
        MessageConnection conn = (MessageConnection) Connector.open("sms://:50001");
        conn.setMessageListener(new MessageListener() {
            public void notifyIncomingMessage(MessageConnection conn) {
                try {
                    Message msg;
                    msg = conn.receive();
                    if (msg instanceof TextMessage) {
                        TextMessage tmsg = (TextMessage) msg;
                        stringItem.setText("Msg: " + tmsg.getPayloadText());
                        System.out.println(tmsg.getPayloadText());
                    }
                    // else if(msg instanceof BinaryMessage) {
                    // .....
                    // } else {
                    // ......
                    // }
                } catch (IOException ex) {
                    ex.printStackTrace();
                } finally {
                    try {
                        conn.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });
    } catch (Exception e1) {
        System.out.println(e1);
    }

但这不起作用...也没有显示任何错误...我做错了什么?...我们可以使用 J2ME 接收消息吗?

发送消息的代码:(已更新)

MessageConnection conn = (MessageConnection) Connector.open("sms://:50001");
TextMessage tmsg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
tmsg.setPayloadText(message);
tmsg.setAddress("sms://" + number);
conn.send();

我有两种不同形式的 sendreceive 功能。我所做的是在两个不同的手机中安装并启动应用程序,从一部手机向另一部手机发送消息并在另一部手机中接收。

消息已成功发送和接收,但在应用程序中未成功。该消息将发送至另一部手机的收件箱。

我能做些什么?

I am trying to make a J2ME application to SEND and RECEIVE text messages. I'm done with the sending part of it but I am not able to receive any message..

Below is what I tried in order to receive text message;

    try {
        MessageConnection conn = (MessageConnection) Connector.open("sms://:50001");
        conn.setMessageListener(new MessageListener() {
            public void notifyIncomingMessage(MessageConnection conn) {
                try {
                    Message msg;
                    msg = conn.receive();
                    if (msg instanceof TextMessage) {
                        TextMessage tmsg = (TextMessage) msg;
                        stringItem.setText("Msg: " + tmsg.getPayloadText());
                        System.out.println(tmsg.getPayloadText());
                    }
                    // else if(msg instanceof BinaryMessage) {
                    // .....
                    // } else {
                    // ......
                    // }
                } catch (IOException ex) {
                    ex.printStackTrace();
                } finally {
                    try {
                        conn.close();
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                }
            }
        });
    } catch (Exception e1) {
        System.out.println(e1);
    }

But this is not working...No errors are showing up either...what is that I am doing wrong?...Can we receive message using J2ME?

The code for sending a message: (UPDATED)

MessageConnection conn = (MessageConnection) Connector.open("sms://:50001");
TextMessage tmsg = (TextMessage) conn.newMessage(MessageConnection.TEXT_MESSAGE);
tmsg.setPayloadText(message);
tmsg.setAddress("sms://" + number);
conn.send();

I have both the send and receive functions in two different forms. What I did is to install and start the application in two different mobiles, send a message from one mobile to the other and receive in the other.

The message is sent and received successfully, but not in the application. The message goes to the inbox of the other mobile.

What can I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那请放手 2024-11-07 22:41:45

尝试 5000 端口号

有些手机有这个端口作为短信监听器

try 5000 port no.

some phone have this port as sms listener

鲜肉鲜肉永远不皱 2024-11-07 22:41:45

尝试将 tmsg.setAddress("sms://" + number); 替换为 tmsg.setAddress("sms://" + number + ":50001");

Try replacing tmsg.setAddress("sms://" + number); with tmsg.setAddress("sms://" + number + ":50001");.

等待我真够勒 2024-11-07 22:41:45

您可以做的最好的事情是在收到消息时启动一个线程,并确保在收听消息通知之前打开您的端口。然后在线程内执行 conn.receive(); 方法来读取消息并对其执行任何您想要执行的操作。

Best thing you can do is to start a thread at the time you receive a message and make sure your ports are opened before listening to the message notification. Then inside the thread just perform conn.receive(); method to read the message and do whatever you want to do with it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文