?? smsthread.java
字號(hào):
/*
* 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;
/** 發(fā)送消息的命令按鈕 */
private Command cmdSendMsg;
//Display管理
private Display display;
private Form form;
/** 消息內(nèi)容 */
private TextField tfMsgText;
/** 用于輸發(fā)送到的電話號(hào)碼 */
private TextField tfPhoneNumber;
/** 用于接收消息 */
private MessageConnection mcon;
/** 完成標(biāo)志 */
boolean done;
Reader reader;
public SMSThread() {
display = Display.getDisplay(this);
cmdExit = new Command("退出程序", Command.EXIT, 1);
cmdSendMsg = new Command("發(fā)送消息", Command.SCREEN, 2);
tfMsgText = new TextField("請(qǐng)輸入消息內(nèi)容:", "", 255, TextField.ANY);
tfPhoneNumber = new TextField("請(qǐng)輸入接收號(hào)碼:", "", 255,
TextField.PHONENUMBER);
}
/**
* 開始運(yùn)行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("不能進(jìn)行接收消息連接:" + ioe.toString());
}
form = new Form("接收/發(fā)送消息演示 - 接收端口為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) {
//檢查電話號(hào)碼是否存在
String pn = tfPhoneNumber.getString();
if (pn.equals("")) { //注意如果使用pn==""會(huì)不起作用
Alert alert = new Alert("發(fā)送消息錯(cuò)誤",
"請(qǐng)輸入接收的電話號(hào)碼", 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();
}
}
}
}
/**
* 給指定號(hào)碼發(fā)送短信息
* <p>
* @param content 短信息內(nèi)容
* @param phoneNumber 手機(jī)號(hào)碼
* <p>
* @return 發(fā)送成功返回true,否則返回false
*/
public boolean send(String content,String phoneNumber){
//返回值
boolean result = true;
try{
//地址
String address = "sms://+" + phoneNumber;
//建立連接
MessageConnection conn=(MessageConnection)Connector.open(address);
//設(shè)置短信息類型為文本
TextMessage msg = (TextMessage)conn.newMessage(
MessageConnection.TEXT_MESSAGE);
//設(shè)置消息地址
msg.setAddress(address);
//設(shè)置信息內(nèi)容
msg.setPayloadText(content);
//發(fā)送消息
conn.send(msg);
}catch(Exception e){
result = false;
System.out.println("發(fā)送短消息錯(cuò)誤:" + e.toString());
}
return result;
}
/**
* 非同步調(diào)用,當(dāng)有消息到達(dá)時(shí)執(zhí)行該方法
*/
public void notifyIncomingMessage(MessageConnection mc) {
if (mc == mcon) {
reader.handleMessage();
}
}
/**
* 用于讀取短信的獨(dú)立線程
*/
class Reader implements Runnable {
private int pendingMessages = 0;
/**
* 執(zhí)行實(shí)際的讀取短信的操作
*/
public void run() {
Message msg=null;
while (!done) {
synchronized(this) {
if (pendingMessages == 0) {
try {
wait();
} catch (Exception e) {
System.out.println("線程等待時(shí)出現(xiàn)異常:" +
e.toString());
}
}
pendingMessages--;
}
try {
msg = mcon.receive();
} catch (IOException ioe) {
//處理讀取消息時(shí)發(fā)生的錯(cuò)誤
System.out.println("讀取消息時(shí)發(fā)生異常:" +
ioe.toString());
}
//處理讀取的文本消息
if ((msg != null) && (msg instanceof TextMessage)) {
TextMessage tmsg = (TextMessage)msg;
form.append("接收到一條消息,接收時(shí)間:" +
tmsg.getTimestamp().toString() + "\n");
form.append("消息發(fā)送方:" + tmsg.getAddress() + "\n");
form.append("消息內(nèi)容:" + tmsg.getPayloadText());
}
}
}
public synchronized void handleMessage() {
pendingMessages++;
notify();
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -