Receive text message using J2ME

感情迁移 提交于 2019-12-23 12:43:03

问题


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?


回答1:


try 5000 port no.

some phone have this port as sms listener




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/5626475/receive-text-message-using-j2me

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!