?? smsthread.java
字號:
/*
* SMSThread.java
*
* Created on 2005年3月15日, 下午8:13
*/
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
import java.io.IOException;
/**
*
* @author Liu Bin
* @version
*/
public class SMSThread extends MIDlet
implements CommandListener, MessageListener {
/** 退出命令按鈕 */
private Command cmdExit;
/** 發送消息的命令按鈕 */
private Command cmdSendMsg;
//Display管理
private Display display;
private Form form;
/** 消息內容 */
private TextField tfMsgText;
/** 用于輸發送到的電話號碼 */
private TextField tfPhoneNumber;
/** 用于接收消息 */
private MessageConnection mcon;
/** 完成標志 */
boolean done;
Reader reader;
public SMSThread() {
display = Display.getDisplay(this);
cmdExit = new Command("退出程序", Command.EXIT, 1);
cmdSendMsg = new Command("發送消息", Command.SCREEN, 2);
tfMsgText = new TextField("請輸入消息內容:", "", 255, TextField.ANY);
tfPhoneNumber = new TextField("請輸入接收號碼:", "", 255,
TextField.PHONENUMBER);
}
/**
* 開始運行MIDlet
*/
public void startApp() {
try {
mcon = (MessageConnection)Connector.open
("sms://:5008");
// Register the listener for inbound messages.
mcon.setMessageListener(this);
done = false;
reader = new Reader();
new Thread(reader).start();
} catch (IOException ioe) {
System.out.println("不能進行接收消息連接:" + ioe.toString());
}
form = new Form("接收/發送消息演示 - 接收端口為5008");
form.append(tfMsgText);
form.append(tfPhoneNumber);
form.addCommand(cmdExit);
form.addCommand(cmdSendMsg);
form.setCommandListener(this);
display.setCurrent(form);
}
public void pauseApp() {
done = true;
try {
mcon.close();
} catch (IOException e) {
// Handle errors
}
}
public void destroyApp(boolean unconditional) {
done = true;
try {
mcon.setMessageListener(null);
mcon.close();
} catch (IOException e) {
// Handle shutdown errors.
}
notifyDestroyed();
}
/**
* 處理命令按鈕事件
*/
public void commandAction(Command c, Displayable s) {
if (c == cmdExit) {
destroyApp(false);
} else if (c == cmdSendMsg) {
//檢查電話號碼是否存在
String pn = tfPhoneNumber.getString();
if (pn.equals("")) { //注意如果使用pn==""會不起作用
Alert alert = new Alert("發送消息錯誤",
"請輸入接收的電話號碼", null,
AlertType.ERROR);
alert.setTimeout(2000);
display.setCurrent(alert, form);
AlertType.ERROR.playSound(display);
} else {
try {
send(tfMsgText.getString(), pn);
} catch (Exception exc) {
exc.printStackTrace();
}
}
}
}
/**
* 給指定號碼發送短信息
* <p>
* @param content 短信息內容
* @param phoneNumber 手機號碼
* <p>
* @return 發送成功返回true,否則返回false
*/
public boolean send(String content,String phoneNumber){
//返回值
boolean result = true;
try{
//地址
String address = "sms://+" + phoneNumber;
//建立連接
MessageConnection conn=(MessageConnection)Connector.open(address);
//設置短信息類型為文本
TextMessage msg = (TextMessage)conn.newMessage(
MessageConnection.TEXT_MESSAGE);
//設置消息地址
msg.setAddress(address);
//設置信息內容
msg.setPayloadText(content);
//發送消息
conn.send(msg);
}catch(Exception e){
result = false;
System.out.println("發送短消息錯誤:" + e.toString());
}
return result;
}
/**
* 非同步調用,當有消息到達時執行該方法
*/
public void notifyIncomingMessage(MessageConnection mc) {
if (mc == mcon) {
reader.handleMessage();
}
}
/**
* 用于讀取短信的獨立線程
*/
class Reader implements Runnable {
private int pendingMessages = 0;
/**
* 執行實際的讀取短信的操作
*/
public void run() {
Message msg=null;
while (!done) {
synchronized(this) {
if (pendingMessages == 0) {
try {
wait();
} catch (Exception e) {
System.out.println("線程等待時出現異常:" +
e.toString());
}
}
pendingMessages--;
}
try {
msg = mcon.receive();
} catch (IOException ioe) {
//處理讀取消息時發生的錯誤
System.out.println("讀取消息時發生異常:" +
ioe.toString());
}
//處理讀取的文本消息
if ((msg != null) && (msg instanceof TextMessage)) {
TextMessage tmsg = (TextMessage)msg;
form.append("接收到一條消息,接收時間:" +
tmsg.getTimestamp().toString() + "\n");
form.append("消息發送方:" + tmsg.getAddress() + "\n");
form.append("消息內容:" + tmsg.getPayloadText());
}
}
}
public synchronized void handleMessage() {
pendingMessages++;
notify();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -