作者:hunhun1981
出自:http://blog.csdn.net/hunhun1981/
这是短信功能的测试,比WTK提供的示范代码更直观,简洁。
还有一点比较有趣,就是在开发BlackBerry时,这个代码能够成功发送短信,而WTK自带的代码却不能(没有仔仔细研究,有兴趣的朋友比较一下,别忘了跟我分享一下结论)。
import
javax.microedition.io.Connector;
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.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
public class SMSMIDlet extends MIDlet implements CommandListener {
private Command exit;
private Command send;
private TextField number;
private TextField content;
private Form form;
public SMSMIDlet() {
number = new TextField( " Phone Number: " , " 13488755864 " , 50 ,
TextField.ANY);
content = new TextField( " Content: " , " sms test " , 256 , TextField.ANY);
send = new Command( " send " , Command.OK, 0 );
exit = new Command( " exit " , Command.BACK, 1 );
form = new Form( " SMS Test " );
form.append(number);
form.append(content);
form.addCommand(send);
form.addCommand(exit);
form.setCommandListener( this );
}
public void commandAction(Command c, Displayable d) {
if (d.equals(form)) {
if (c.equals(send)) {
String num = number.getString();
String con = content.getString();
if (num != null && num.length() > 1 ) {
if (con != null && con.length() > 1 ) {
sendSMS(num, con);
}
}
} else if (c.equals(exit)) {
notifyDestroyed();
}
}
}
protected void destroyApp( boolean arg0) {
}
protected void pauseApp() {
}
protected void startApp() {
Display.getDisplay( this ).setCurrent(form);
}
public void sendSMS( final String phoneNumber, final String message) {
new Thread() {
public void run() {
try {
// 地址
String address = " sms:// " + phoneNumber;
// 建立连接
MessageConnection conn = (MessageConnection) Connector
.open(address);
form.append( " 建立连接 " );
// 设置短信息类型为文本,短信息有文本和二进制两种类型
TextMessage msg = (TextMessage) conn
.newMessage(MessageConnection.TEXT_MESSAGE);
form.append( " 创建信息 " );
// 设置信息内容
msg.setPayloadText(message);
form.append( " 设置信息 " );
// 发送
conn.send(msg);
form.append( " 发送完成 " );
conn.close();
form.append( " 关闭连接 " );
} catch (Exception e) {
form.append(e.toString());
// 未处理
}
}
}.start();
}
}
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.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
public class SMSMIDlet extends MIDlet implements CommandListener {
private Command exit;
private Command send;
private TextField number;
private TextField content;
private Form form;
public SMSMIDlet() {
number = new TextField( " Phone Number: " , " 13488755864 " , 50 ,
TextField.ANY);
content = new TextField( " Content: " , " sms test " , 256 , TextField.ANY);
send = new Command( " send " , Command.OK, 0 );
exit = new Command( " exit " , Command.BACK, 1 );
form = new Form( " SMS Test " );
form.append(number);
form.append(content);
form.addCommand(send);
form.addCommand(exit);
form.setCommandListener( this );
}
public void commandAction(Command c, Displayable d) {
if (d.equals(form)) {
if (c.equals(send)) {
String num = number.getString();
String con = content.getString();
if (num != null && num.length() > 1 ) {
if (con != null && con.length() > 1 ) {
sendSMS(num, con);
}
}
} else if (c.equals(exit)) {
notifyDestroyed();
}
}
}
protected void destroyApp( boolean arg0) {
}
protected void pauseApp() {
}
protected void startApp() {
Display.getDisplay( this ).setCurrent(form);
}
public void sendSMS( final String phoneNumber, final String message) {
new Thread() {
public void run() {
try {
// 地址
String address = " sms:// " + phoneNumber;
// 建立连接
MessageConnection conn = (MessageConnection) Connector
.open(address);
form.append( " 建立连接 " );
// 设置短信息类型为文本,短信息有文本和二进制两种类型
TextMessage msg = (TextMessage) conn
.newMessage(MessageConnection.TEXT_MESSAGE);
form.append( " 创建信息 " );
// 设置信息内容
msg.setPayloadText(message);
form.append( " 设置信息 " );
// 发送
conn.send(msg);
form.append( " 发送完成 " );
conn.close();
form.append( " 关闭连接 " );
} catch (Exception e) {
form.append(e.toString());
// 未处理
}
}
}.start();
}
}