-
-5 10
-
这适合初学J2ME者开发手机软件,更有利于对高级界面的掌握和理解。
主要功能:写短信,发短信,返回,返回时步重新加载短信内容,支持小灵通互发短信。
测试机型:NOKIA N72 S60 3RD
package main;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Alert;
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.TextBox;
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 SendMessage extends MIDlet implements CommandListener {
public String destAddr;
private String msg;
private String sendno;
private Display dis;
private Form frm;
private TextBox content;
private TextField sendto;
// private TextField content;
private Command cmdok, send, exit, back;public SendMessage() {
// TODO Auto-generated constructor stub
frm = new Form("发送到");
sendto = new TextField("发送到:", null, 11, TextField.NUMERIC);
content = new TextBox("发送内容:", "发送内容", 10000, TextField.ANY);
// 按钮的定义
cmdok = new Command("确定", 1, Command.OK);
send = new Command("发送", 1, Command.OK);
exit = new Command("退出", 2, Command.CANCEL);
back = new Command("返回", 2, Command.CANCEL);
// content.setLayout(new BorderLayout(10,10));
dis = Display.getDisplay(this);
frm.append(sendto);
// 发送到添加监听器
frm.addCommand(send);
frm.addCommand(back);
// 短信内容页面添加监听器
content.addCommand(exit);
content.addCommand(cmdok);
// 切换默认界面1
dis.setCurrent(content);
/* **************************监听器********************* */
// 给content添加监听器
content.setCommandListener(this);
// 给frm添加监听器
frm.setCommandListener(this);
}protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub}
protected void pauseApp() {
// TODO Auto-generated method stub}
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub}
public void commandAction(Command cmd, Displayable disa) {
// TODO Auto-generated method stub
if (cmd.equals(exit)) {
this.notifyDestroyed();
} else if (cmd.equals(cmdok)) {
dis.setCurrent(frm);
msg = content.getString();
} else if (cmd.equals(back)) {
dis.setCurrent(content);
content.setString(msg);
} else if (cmd.equals(send)) {
sendno = sendto.getString();
boolean res=SendSMS(sendno,msg);
if(res){
dis.setCurrent(new Alert("发送成功!"));
}else{
dis.setCurrent(new Alert("发送失败!"));
}
}
}public boolean SendSMS(String Number, String Message) { // Number就是手机号码,Message就是短信的内容
char[] Addr;
Addr = Number.toCharArray();
if (Addr[0] == '1' && Addr[1] == '0' && Addr[2] == '6')// 当是小灵通号码(106开头)
destAddr = "sms://" + Number;
else { // 当是手机时if (Addr[0] != '+')
destAddr = "sms://+86" + Number;
if (Addr[0] == '+')
destAddr = "sms://" + Number;
}
boolean result = true;
try {
MessageConnection conn = (MessageConnection) Connector.open(destAddr);
// 设置短信息类型为文本,短信息有文本和二进制两种类型
TextMessage msg = (TextMessage) conn
.newMessage(MessageConnection.TEXT_MESSAGE);// 设置信息内容
msg.setAddress(destAddr);
msg.setPayloadText(Message);
// 发送
conn.send(msg);
result = true;
} catch (Exception e) {
result = false;
// 未处理
}
return result;
}
}
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
- 评论(2)
发表评论 TrackBack