這幾天由于公司的需要,見到有些同事用手機刷卡,覺得很痛苦。
今天早上回來就產生了一個想法,不如用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
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("號碼", "XXXX:", 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);
-
- }
-
- }
|