這幾天由于公司的需要,見到有些同事用手機(jī)刷卡,覺得很痛苦。
今天早上回來就產(chǎn)生了一個(gè)想法,不如用j2me實(shí)現(xiàn)一個(gè)短信發(fā)送機(jī)的程序,然后只需要填入幾個(gè)數(shù)字就可以實(shí)現(xiàn)短信的自動(dòng)發(fā)送等。
經(jīng)過大概2個(gè)小時(shí)的奮斗,終于寫好了,并且在多部不同品牌的機(jī)器運(yùn)行良好,而且很實(shí)用,不過可以有些手機(jī)需要數(shù)字簽名,否則的話,會(huì)不停的提示你。郁悶,不過索愛跟三星就可以設(shè)置。
現(xiàn)在公布源代碼跟按照文件
先讓大家看個(gè)圖
java 代碼
- /********************************************************************
- * 項(xiàng)目名稱 :j2me學(xué)習(xí)
- *
- * 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("發(fā)送", 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("短信發(fā)送機(jī)");
-
- setCommandListener(this);
- text = new TextField("狀態(tài)", "等待發(fā)送短信", 20, TextField.ANY);
- phone = new TextField("號(hào)碼", "XXXX:", 20, TextField.NUMERIC);
- content = new TextField("指令", "777", 10, TextField.NUMERIC);
- num = new TextField("條數(shù)", "23", 10, TextField.NUMERIC);
- timeOut = new TextField("時(shí)間格", "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("發(fā)送地址為:" + 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("成功發(fā)送" +this.num.getString() + "第" + sms + "條");
-
- } catch (IOException e) {
- // TODO 自動(dòng)生成 catch 塊
- e.printStackTrace();
- }
- }
-
- }
-
-
-
- /********************************************************************
- * 項(xiàng)目名稱 :j2me學(xué)習(xí)
- *
- * 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("打開發(fā)送機(jī)", Command.OK, 1);
- public Menu(String title, int listType) {
- super(title, listType);
-
- this.append("打開發(fā)送機(jī)", 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{
-
- }
- }
-
- }
-
-
- /********************************************************************
- * 項(xiàng)目名稱 :j2me學(xué)習(xí)
- *
- * 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 自動(dòng)生成 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("短信發(fā)送機(jī)",Choice.IMPLICIT);
- display = Display.getDisplay(this);
- display.setCurrent(m);
-
- }
-
- }
|