使用J2ME发送短信的代码

发布于 2024-07-19 06:42:35 字数 28 浏览 9 评论 0原文

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

I am looking for code to send SMS using J2ME.

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

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

发布评论

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

评论(2

森林迷了鹿 2024-07-26 06:42:35

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

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"

You can try the code below to implement this:

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;
}  

You can specify any special port by just adding ":port_no" after:

"String addr = "sms://" + sPhoneNo"
—━☆沉默づ 2024-07-26 06:42:35

启动此线程发送短信

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 并显示。

Start this thread to send SMS

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();
        }
    }
  }
}

Nokia devices don't show system alert if message is sent from j2me. So if you want to show alert, then you have to create your own PopUp and show.

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