今天早上回来就产生了一个想法,不如用j2me实现一个短信发送机的程序,然后只需要填入几个数字就可以实现短信的自动发送等。
经过大概2个小时的奋斗,终于写好了,并且在多部不同品牌的机器运行良好,而且很实用,不过可以有些手机需要数字签名,否则的话,会不停的提示你。郁闷,不过索爱跟三星就可以设置。
/********************************************************************
* 项目名称 :j2me学习
*
* Copyright 2005-2006 Wuhua. All rights reserved
********************************************************************/
package org.fox.sms;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;
/**
* 类名:SMSForm.java
* 编写日期: 2007-5-25
* 程序功能描述:
* Demo:
* Bug:
*
* 程序变更日期 :
* 变更作者 :
* 变更说明 :
*
* @author wuhua
rrq12345@163.com
*/
public class SMSForm extends Form
implements CommandListener, Runnable{
Command send = new Command("发送", Command.OK, 1);
Command back = new Command("返回", Command.BACK, Command.BACK);
TextField phone;
TextField content;
TextField num;
TextField timeOut;
TextField text;
String serverPort = "5000";// getAppProperty("serverPort");
int sms;
Menu menu;
public SMSForm(Menu m) {
super("短信发送机");
setCommandListener(this);
text = new TextField("状态", "等待发送短信", 20, TextField.ANY);
phone = new TextField("号码", "xxxxxx:", 20, TextField.NUMERIC);
content = new TextField("指令", "777", 10, TextField.NUMERIC);
num = new TextField("条数", "23", 10, TextField.NUMERIC);
timeOut = new TextField("时间格", "10", 10, TextField.NUMERIC);
this.append(phone);
this.append(content);
this.append(num);
this.append(timeOut);
this.append(text);
this.addCommand(send);
this.addCommand(back);
this.menu = m;
}
public void commandAction(Command c, Displayable arg1) {
if(c == send){
new Thread(this).start();
this.removeCommand(send);
}else{
SMSSenderMIDlet.display.setCurrent(menu);
}
}
public void run() {
int num = Integer.parseInt(this.num.getString());
int sleep = Integer.parseInt(this.timeOut.getString());
while(true){
// System.out.println(sleep);
if(sms < num){
senderImpl();
}
else{
SMSSenderMIDlet.display.setCurrent(menu);
break;
}
try {
// System.out.println(sleep);
Thread.sleep(sleep*1000);
// System.out.println(sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void senderImpl() {
String addr = "sms://" + phone.getString();
System.out.println("发送地址为:" + addr);
MessageConnection conn;
try {
conn = (MessageConnection) Connector.open(addr);
TextMessage msg = (TextMessage) conn
.newMessage(MessageConnection.TEXT_MESSAGE);
msg.setPayloadText(content.getString());
conn.send(msg);
conn.close();
sms++;
// text = sms+"";
text.setString("成功发送" +this.num.getString() + "第" + sms + "条");
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
/********************************************************************
* 项目名称 :j2me学习
*
* Copyright 2005-2006 Wuhua. All rights reserved
********************************************************************/
package org.fox.sms;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
/**
* 类名:Menu.java
* 编写日期: 2007-5-25
* 程序功能描述:
* Demo:
* Bug:
*
* 程序变更日期 :
* 变更作者 :
* 变更说明 :
*
* @author wuhua
rrq12345@163.com
*/
public class Menu extends List implements CommandListener{
Command send = new Command("打开发送机", Command.OK, 1);
public Menu(String title, int listType) {
super(title, listType);
this.append("打开发送机", null);
this.addCommand(send);
this.setCommandListener(this);
}
public void commandAction(Command c, Displayable d) {
System.out.println("dfsdfsd");
if(c == send){
SMSSenderMIDlet.display.setCurrent(new SMSForm(this));
}else{
}
}
}
/********************************************************************
* 项目名称 :j2me学习
*
* Copyright 2005-2006 Wuhua. All rights reserved
********************************************************************/
package org.fox.sms;
import java.io.IOException;
import javax.microedition.io.Connector;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.wireless.messaging.MessageConnection;
/**
* 类名:SMSSenderMIDlet.java
* 编写日期: 2007-5-25
* 程序功能描述:
* Demo:
* Bug:
*
* 程序变更日期 :
* 变更作者 :
* 变更说明 :
*
* @author wuhua
rrq12345@163.com
*/
public class SMSSenderMIDlet extends MIDlet {
private MessageConnection sconn;
public static Display display;
public SMSSenderMIDlet() {
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
try {
sconn.close();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
String serverPort = "5000";
try {
sconn = (MessageConnection) Connector.open("sms://:" + serverPort);
} catch (IOException e) {
e.printStackTrace();
}
Menu m = new Menu("短信发送机",Choice.IMPLICIT);
display = Display.getDisplay(this);
display.setCurrent(m);
}
}