今天早上回來就產生了一個想法,不如用J2ME實現一個短信發送機的程序,然後只需要填入幾個數字就可以實現短信的自動發送等。
經過大概2個小時的奮斗,終於寫好了,並且在多部不同品牌的機器運行良好,而且很實用,不過可以有些手機需要數字簽名,否則的話,會不停的提示你。郁悶,不過索愛跟三星就可以設置。
現在公布源代碼跟按照文件
先讓大家看個圖
Java 代碼
- /********************************************************************
- * 項目名稱 :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
[email protected]
- */
- 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
[email protected]
*/
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
[email protected]
*/
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);
}
}