开发者

Receive text message using J2ME

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;
           开发者_如何学C         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?


try 5000 port no.

some phone have this port as sms listener


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜