4

我正在寻找使用 J2ME 发送 SMS 的代码。

4

2 回答 2

4

您可以尝试下面的代码来实现这一点:

private boolean SendSMS(String sPhoneNo, String sMessage) {
    boolean result = true;

    try {
        String addr = "sms://" + sPhoneNo;
        MessageConnection conn = (MessageConnection) Connector.open(addr);
        TextMessage msg = (TextMessage) 
            conn.newMessage(MessageConnection.TEXT_MESSAGE);
        msg.setPayloadText(sMessage);
        conn.send(msg);
        conn.close();
    } 

    catch (SecurityException se) {
        result = false;
    } 

    catch (Exception e) {
        result = false;
    }

    return result;
}  

您可以通过添加以下内容来指定任何特殊端口":port_no"

"String addr = "sms://" + sPhoneNo"
于 2012-04-18T05:50:59.607 回答
3

启动此线程发送短信

public class SendSMS extends Thread {

private String receiver;
private String receivedMsg;
private HomeScreen home;
private boolean bool = false;
private boolean notsent;

public SendSMS(HomeScreen gen, String msg, String number) {
    this.home = gen;
    this.receiver = number;
    this.receivedMsg = msg;
}

public void run() {
    while (!bool) {
        SendMessage();
    }
}

/**
 * Send the mesage using WMA api.
 */
private void SendMessage() { 
    String s = "sms://" + receiver;
    send(s);
}

private void send(String url) {
    MessageConnection messageconnection = null;
    try {
        messageconnection = (MessageConnection) Connector.open(url);
        TextMessage textmessage = (TextMessage) messageconnection.newMessage(MessageConnection.TEXT_MESSAGE);
        textmessage.setAddress(url);
        textmessage.setPayloadText(receivedMsg);
        messageconnection.send(textmessage);
    } catch (Exception throwable) {
        notsent = true;
        home.genericObject.setSmsStatus(false);
        if (!home.isNokia()) {
            new PopUp("Message not sent"); // not sent
        }
        bool = true;
        try {
            messageconnection.close();
        } catch (Exception e) {
        }
    }

    if (messageconnection != null) {
        try {
            messageconnection.close();
            if (!notsent) {
                home.genericObject.setSmsStatus(false);
                if (!home.isNokia()) {
                    new PopUp("Message Sent"); // sent
                }
            }
            bool = true;
        } catch (Exception ie) {
            ie.printStackTrace();
        }
    }
  }
}

如果消息是从 j2me 发送的,诺基亚设备不会显示系统警报。因此,如果您想显示警报,则必须创建自己的 PopUp 并显示。

于 2012-03-08T05:07:55.333 回答