簡訊傳送機的實現

raorq發表於2020-04-06

今天早上回來就產生了一個想法,不如用j2me實現一個簡訊傳送機的程式,然後只需要填入幾個數字就可以實現簡訊的自動傳送等。
經過大概2個小時的奮鬥,終於寫好了,並且在多部不同品牌的機器執行良好,而且很實用,不過可以有些手機需要數字簽名,否則的話,會不停的提示你。鬱悶,不過索愛跟三星就可以設定。
現在公佈原始碼跟按照檔案
先讓大家看個圖


java 程式碼
  1. /********************************************************************
  2. * 專案名稱 :j2me學習
  3. *
  4. * Copyright 2005-2006 Wuhua. All rights reserved
  5. ********************************************************************/
  6. package org.fox.sms;
  7. import java.io.IOException;
  8. import javax.microedition.io.Connector;
  9. import javax.microedition.lcdui.Command;
  10. import javax.microedition.lcdui.CommandListener;
  11. import javax.microedition.lcdui.Displayable;
  12. import javax.microedition.lcdui.Form;
  13. import javax.microedition.lcdui.TextField;
  14. import javax.wireless.messaging.MessageConnection;
  15. import javax.wireless.messaging.TextMessage;
  16. /**
  17. * 類名:SMSForm.java
  18. * 編寫日期: 2007-5-25
  19. * 程式功能描述:
  20. * Demo:
  21. * Bug:
  22. *
  23. * 程式變更日期 :
  24. * 變更作者 :
  25. * 變更說明 :
  26. *
  27. * @author wuhua
    rrq12345@163.com
  28. */
  29. public class SMSForm extends Form
  30. implements CommandListener, Runnable{
  31. Command send = new Command("傳送", Command.OK, 1);
  32. Command back = new Command("返回", Command.BACK, Command.BACK);
  33. TextField phone;
  34. TextField content;
  35. TextField num;
  36. TextField timeOut;
  37. TextField text;
  38. String serverPort = "5000";// getAppProperty("serverPort");
  39. int sms;
  40. Menu menu;
  41. public SMSForm(Menu m) {
  42. super("簡訊傳送機");
  43. setCommandListener(this);
  44. text = new TextField("狀態", "等待傳送簡訊", 20, TextField.ANY);
  45. phone = new TextField("號碼", "xxxxxx:", 20, TextField.NUMERIC);
  46. content = new TextField("指令", "777", 10, TextField.NUMERIC);
  47. num = new TextField("條數", "23", 10, TextField.NUMERIC);
  48. timeOut = new TextField("時間格", "10", 10, TextField.NUMERIC);
  49. this.append(phone);
  50. this.append(content);
  51. this.append(num);
  52. this.append(timeOut);
  53. this.append(text);
  54. this.addCommand(send);
  55. this.addCommand(back);
  56. this.menu = m;
  57. }
  58. public void commandAction(Command c, Displayable arg1) {
  59. if(c == send){
  60. new Thread(this).start();
  61. this.removeCommand(send);
  62. }else{
  63. SMSSenderMIDlet.display.setCurrent(menu);
  64. }
  65. }
  66. public void run() {
  67. int num = Integer.parseInt(this.num.getString());
  68. int sleep = Integer.parseInt(this.timeOut.getString());
  69. while(true){
  70. //System.out.println(sleep);
  71. if(sms < num){
  72. senderImpl();
  73. }
  74. else{
  75. SMSSenderMIDlet.display.setCurrent(menu);
  76. break;
  77. }
  78. try {
  79. //System.out.println(sleep);
  80. Thread.sleep(sleep*1000);
  81. //System.out.println(sleep);
  82. } catch (InterruptedException e) {
  83. e.printStackTrace();
  84. }
  85. }
  86. }
  87. private void senderImpl() {
  88. String addr = "sms://" + phone.getString();
  89. System.out.println("傳送地址為:" + addr);
  90. MessageConnection conn;
  91. try {
  92. conn = (MessageConnection) Connector.open(addr);
  93. TextMessage msg = (TextMessage) conn
  94. .newMessage(MessageConnection.TEXT_MESSAGE);
  95. msg.setPayloadText(content.getString());
  96. conn.send(msg);
  97. conn.close();
  98. sms++;
  99. //text = sms+"";
  100. text.setString("成功傳送" +this.num.getString() + "第" + sms + "條");
  101. } catch (IOException e) {
  102. // TODO 自動生成 catch 塊
  103. e.printStackTrace();
  104. }
  105. }
  106. }
  107. /********************************************************************
  108. * 專案名稱 :j2me學習
  109. *
  110. * Copyright 2005-2006 Wuhua. All rights reserved
  111. ********************************************************************/
  112. package org.fox.sms;
  113. import javax.microedition.lcdui.Command;
  114. import javax.microedition.lcdui.CommandListener;
  115. import javax.microedition.lcdui.Displayable;
  116. import javax.microedition.lcdui.List;
  117. /**
  118. * 類名:Menu.java
  119. * 編寫日期: 2007-5-25
  120. * 程式功能描述:
  121. * Demo:
  122. * Bug:
  123. *
  124. * 程式變更日期 :
  125. * 變更作者 :
  126. * 變更說明 :
  127. *
  128. * @author wuhua
    rrq12345@163.com
  129. */
  130. public class Menu extends List implements CommandListener{
  131. Command send = new Command("開啟傳送機", Command.OK, 1);
  132. public Menu(String title, int listType) {
  133. super(title, listType);
  134. this.append("開啟傳送機", null);
  135. this.addCommand(send);
  136. this.setCommandListener(this);
  137. }
  138. public void commandAction(Command c, Displayable d) {
  139. System.out.println("dfsdfsd");
  140. if(c == send){
  141. SMSSenderMIDlet.display.setCurrent(new SMSForm(this));
  142. }else{
  143. }
  144. }
  145. }
  146. /********************************************************************
  147. * 專案名稱 :j2me學習
  148. *
  149. * Copyright 2005-2006 Wuhua. All rights reserved
  150. ********************************************************************/
  151. package org.fox.sms;
  152. import java.io.IOException;
  153. import javax.microedition.io.Connector;
  154. import javax.microedition.lcdui.Choice;
  155. import javax.microedition.lcdui.Display;
  156. import javax.microedition.midlet.MIDlet;
  157. import javax.microedition.midlet.MIDletStateChangeException;
  158. import javax.wireless.messaging.MessageConnection;
  159. /**
  160. * 類名:SMSSenderMIDlet.java
  161. * 編寫日期: 2007-5-25
  162. * 程式功能描述:
  163. * Demo:
  164. * Bug:
  165. *
  166. * 程式變更日期 :
  167. * 變更作者 :
  168. * 變更說明 :
  169. *
  170. * @author wuhua
    rrq12345@163.com
  171. */
  172. public class SMSSenderMIDlet extends MIDlet {
  173. private MessageConnection sconn;
  174. public static Display display;
  175. public SMSSenderMIDlet() {
  176. }
  177. protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
  178. try {
  179. sconn.close();
  180. } catch (IOException e) {
  181. // TODO 自動生成 catch 塊
  182. e.printStackTrace();
  183. }
  184. }
  185. protected void pauseApp() {
  186. }
  187. protected void startApp() throws MIDletStateChangeException {
  188. String serverPort = "5000";
  189. try {
  190. sconn = (MessageConnection) Connector.open("sms://:" + serverPort);
  191. } catch (IOException e) {
  192. e.printStackTrace();
  193. }
  194. Menu m = new Menu("簡訊傳送機",Choice.IMPLICIT);
  195. display = Display.getDisplay(this);
  196. display.setCurrent(m);
  197. }
  198. }

相關文章