我正在寻找使用J2ME发送短信的代码。
答案 0 :(得分: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"
答案 1 :(得分: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并显示。