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

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

メッセージが j2me から送信された場合、Nokia デバイスはシステム アラートを表示しません。したがって、アラートを表示したい場合は、独自のポップアップとショーを作成する必要があります。

于 2012-03-08T05:07:55.333 に答える