?? sendsmsmsg.java
字號:
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
public class SendSMSMsg extends MIDlet implements CommandListener {
private Display display = null;
/** 退出命令按鈕 */
private Command cmdExit = new Command("退出", Command.EXIT, 1);
/** 發送消息的命令按鈕 */
private Command cmdSendMsg = new Command("發送", Command.SCREEN, 1);
private Form form = new Form("發送文本信息");
/** 用于輸發送到的電話號碼 */
private TextField tfPhoneNumber = new TextField("請輸入接收號碼:",
"+5550000", 20,
TextField.PHONENUMBER);
/** 用于輸發送端口 */
private TextField tfPort = new TextField("請輸入接收端口:",
"", 20,
TextField.PHONENUMBER);
/** 消息內容 */
private TextField tfMsgText = new TextField("請輸入消息內容:",
"", 255,
TextField.ANY);
public SendSMSMsg() {
super();
form.append(tfPhoneNumber);
form.append(tfPort);
form.append(tfMsgText);
form.addCommand(cmdExit);
form.addCommand(cmdSendMsg);
form.setCommandListener(this);
}
protected void startApp() throws MIDletStateChangeException {
display = Display.getDisplay(this);
display.setCurrent(form);
}
protected void pauseApp() {
// TODO 自動生成方法存根
}
protected void destroyApp(boolean arg0)
throws MIDletStateChangeException {
// TODO 自動生成方法存根
}
/**
* 處理命令按鈕事件
*/
public void commandAction(Command c, Displayable s) {
if (c == cmdExit) {
notifyDestroyed();
} else if (c == cmdSendMsg) {
send();
}
}
/**
* 在獨立的線程中發送消息
*/
private void send() {
new Thread() {
public void run() {
//檢查電話號碼是否存在
String number = tfPhoneNumber.getString();
if (number.equals("")) {
Alert alert = new Alert("發送消息錯誤",
"請輸入接收的電話號碼", null,
AlertType.ERROR);
alert.setTimeout(2000);
display.setCurrent(alert, form);
AlertType.ERROR.playSound(display);
return;
}
//地址
String address;
if (number.startsWith("+")) {
address = "sms://" + number;
} else {
address = "sms://+" + number;
}
//獲得端口
String port = tfPort.getString();
if (!port.equals("")) {
address+=":"+port;
}
System.out.println(address);
try {
//建立連接
MessageConnection mc = (MessageConnection)Connector.open(address);
//設置短信息類型為文本
TextMessage msg = (TextMessage)mc.newMessage(
MessageConnection.TEXT_MESSAGE);
//設置消息地址
msg.setAddress(address);
//設置信息內容
msg.setPayloadText(tfMsgText.getString());
//發送消息
mc.send(msg);
mc.close();
mc = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -