返回首页
您还没有登录!   登录 | 免费注册 | 搜索 |
中国茶铺
  JAVA ME
所有分类
用j2me设计一个手机电子书阅读软件 部分源代码解释

终于足球项目告一段落,期间学辛苦,无奈与成长都是可写的。
在此期间我让UI折腾的紧,甚至曾经想放弃,终究是坚持下来了。想想在一个小小的屏幕上设计一个UI已经是很痛苦的已经事情,何况在pc上设计,真是佩服死了那些高手们。
最近,时间比较有空,所以把UI修改了下,然后在此基础上+jsr75规范做了个电子书阅读软件。等我设计好了以后,打算开源,大家共同学习,虽然写的不好,各位高手多指教。现在发布一些读取手机目录的方法。

代码
  1. package org.pook.file;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.Enumeration;  
  5. import java.util.Vector;  
  6.   
  7. import javax.microedition.io.Connector;  
  8. import javax.microedition.io.file.FileConnection;  
  9. import javax.microedition.io.file.FileSystemRegistry;  
  10.   
  11. import org.pook.log.Log;  
  12.   
  13. /**  
  14.  * <b>类名:BookFileImpl.java</b> </br>   
  15.  * 编写日期: 2006-10-12 <br/>  
  16.  * 程序功能描述: <br/>  
  17.  * Demo: <br/>  
  18.  * Bug: <br/>  
  19.  *   
  20.  * 程序变更日期 :<br/>   
  21.  * 变更作者 :<br/>   
  22.  * 变更说明 :<br/>  
  23.  *   
  24.  * @author wuhua </br> <a href="mailto:rrq12345@163.com">rrq12345@163.com</a>  
  25.  */  
  26. public class BookFileImpl implements BookFile {  
  27.     private static Log log = Log.getLog("BookFileImpl");  
  28.       
  29.     FileConnection conn = null;  
  30.       
  31.     BookFileImpl(String url){  
  32.         try {  
  33.             conn = (FileConnection) Connector.open( url, Connector.READ);  
  34.   
  35.              
  36.         }catch( IOException e ){  
  37.             e.printStackTrace();  
  38.         }  
  39.         catch( SecurityException e ){  
  40.            e.printStackTrace();  
  41.         }  
  42.   
  43.     }  
  44.       
  45.       
  46.     public Vector listName() throws IOException {  
  47.            
  48.         Vector vc = null;  
  49.          if( conn.isDirectory() ){  
  50.                 Enumeration names = conn.list();  
  51.                 vc = new Vector();       
  52.                 while( names.hasMoreElements() ){  
  53.                     vc.addElement(names.nextElement());  
  54.                       
  55.                 }  
  56.                 return vc;  
  57.             } else {  
  58.                 return null;  
  59.           }  
  60.     }  
  61.   
  62.     public String read() throws IOException {  
  63.           return null;  
  64.     }  
  65.   
  66.     public boolean isFile() {  
  67.           
  68.         if(conn.isDirectory())  
  69.             return false;  
  70.         else  
  71.             return true;  
  72.     }  
  73.   
  74.     public String getFilePath() {  
  75.         checkConn();  
  76.         return conn.getPath();  
  77.     }  
  78.   
  79.     private void checkConn() {  
  80.         if(conn == null)  
  81.             throw new NullPointerException("文件资源不存在!");  
  82.           
  83.     }  
  84.   
  85.     public String getFileName() {  
  86.         checkConn();  
  87.         return conn.getName();  
  88.     }  
  89.   
  90.     public void close() {  
  91.         if(conn != null)  
  92.             try {  
  93.                 conn.close();  
  94.             } catch (IOException e) {  
  95.                 // TODO 自动生成 catch 块  
  96.                 e.printStackTrace();  
  97.             }  
  98.           
  99.     }  
  100.   
  101.    
代码
  1. package org.pook.file;  
  2.   
  3. import java.util.Enumeration;  
  4. import java.util.Vector;  
  5.   
  6. import javax.microedition.io.file.FileSystemRegistry;  
  7.   
  8. import org.pook.log.Log;  
  9.   
  10. /**  
  11.  * <b>类名:BookFileManager.java</b> </br> 编写日期: 2006-10-12 <br/> 程序功能描述: <br/>  
  12.  * Demo: <br/> Bug: <br/>  
  13.  *   
  14.  * 程序变更日期 :<br/> 变更作者 :<br/> 变更说明 :<br/>  
  15.  *   
  16.  * @author wuhua </br> <a href="mailto:rrq12345@163.com">rrq12345@163.com</a>  
  17.  */  
  18. public class BookFileManager {  
  19.     private static Log log = Log.getLog("BookFileManager");  
  20.     public static boolean available() {  
  21.         String version = System  
  22.                 .getProperty("microedition.io.file.FileConnection.version");  
  23.   
  24.         if (version != null) {  
  25.             return true;  
  26.         } else {  
  27.             return false;  
  28.         }  
  29.   
  30.     }  
  31.   
  32.     public static BookFile openBookFile(String url) {  
  33.         return new BookFileImpl(url);  
  34.     }  
  35.       
  36.       
  37.        
  38.   
  39.     public static Vector listRoots() {  
  40.   
  41.        
  42.         Vector vc = null;  
  43.   
  44.         Enumeration names = FileSystemRegistry.listRoots();  
  45.            
  46.         vc = new Vector();  
  47.   
  48.         while (names.hasMoreElements()) {         
  49.             vc.addElement(names.nextElement());  
  50.             //log.debug(names.nextElement());  
  51.         }  
  52.            
  53.         return vc;  
  54.   
  55.     }  
  56.   

阅读全文>>
2007-07-17 16:25:01 |  浏览 (2398) | 
短信发送机的实现
这几天由于公司的需要,见到有些同事用手机刷卡,觉得很痛苦。
今天早上回来就产生了一个想法,不如用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("号码", "XXXX:", 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. }
阅读全文>>
2007-07-17 09:47:33 |  浏览 (1684) | 
J2ME中多线程网络连接编程的分析,与设计
关键字: J2ME j2me 网络连接,多线程    

注意。本文引用http://www.j2medev.com网上一些内容,只是出于文章的连贯性,请见谅
引言

J2ME(Java 2 Micro Edition)是Java 2的一个组成部分,它与J2SE、J2EE并称。J2ME是一种高度优化的Java运行环境,主要针对消费类电子设备的,例如蜂窝电话、可视电话、数字机 顶盒和汽车导航系统等等。即J2ME是为消费电子产品和手持设备量身定制的Java专用版本。
J2ME的出现使开发跨平台的消费类电子产品的应用软件成为可能。Java语言的与平台无关的特性移植到小型电子设备上,允许移动无线设备之间 共享应用程序。它提供了基于HTTP的高级Internet协议,使移动电话能以Client/Server方式直接访问Internet的全部信息,从 而使得不同的Client访问不同的资源。

  在将来的无线通信时代中,大量的网络应用程序将被开发出来去满足无线移动通讯的要求,而要充分的发挥无线移动通讯设备的通信能力,J2ME网络编程就变得尤为重要。那么为了高效地进行网络编程,就要利用Java语言的多线程编程机制。

下面我将给出代码来怎么才可以设计一个好的多线程连网。多线程在设计中的地位是很高的,也是比较难设计好的,没经验的人很容易就使程序产生死锁,崩溃等(在下现在还经常这样^_^)

我的例子是基于socket。
1。数据发送线程SenderTask,

代码
  1. package org.wuhua.net;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.OutputStream;  
  5.   
  6. /**  
  7. * <b>类名:SenderTask.java</b> </br> 编写日期: 2006-7-16 <br/> 程序功能描述   
  8. * 发 命令线程 如果OputStream 出现异常,则断开 有连 <br/> 断开 Demo: <br/> Bug: <br/>  
  9. *   
  10. * 程序变更日期 <br/> 变更作 <br/> 变更说明 <br/>  
  11. *   
  12. * @author wuhua </br> <a href="mailto:rrq12345@163.com">rrq12345@163.com</a>  
  13. */  
  14. public class SenderTask implements Runnable {  
  15.   
  16.      public OutputStream os;  
  17.   
  18.      private String message;  
  19.   
  20.      private boolean stop = false;  
  21.   
  22.      SenderTask(OutputStream os) {  
  23.         this.os = os;  
  24.   
  25.      }  
  26.   
  27.      /**  
  28.       * 发送指令,具体的指令格式,可以按照自己定义的规则。  
  29.       *   
  30.       * @param _msg  
  31.       */  
  32.      public synchronized void send(String _msg) {  
  33.         message = _msg;  
  34.          notify(); // 执行运行  
  35.      }  
  36.   
  37.      /**  
  38.       * 执行,监听客户发送指令,如果指令不为null则工作 , 否则暂停工作直到有客户发送指令为 止才工作,  
  39.       */  
  40.      public synchronized void run() {  
  41.          try {  
  42.              runImpl();  
  43.          } catch (Throwable tw) {  
  44.              tw.printStackTrace();  
  45.   
  46.          }  
  47.      }  
  48.   
  49.      private void runImpl() throws IOException {  
  50.          while (true) {  
  51.              if (stop)  
  52.                  break;  
  53.   
  54.              // If no client to deal, wait until one connects  
  55.              if (message == null) {  
  56.                  try {  
  57.                      wait();  
  58.                  } catch (Exception e) {  
  59.   
  60.                  }  
  61.              }  
  62.              wirte();  
  63.   
  64.          }  
  65.      }  
  66.   
  67.      /**  
  68.       * 具体发 送指令的实现  
  69.       *   
  70.       * @throws IOException  
  71.       *   
  72.       */  
  73.      private void wirte() throws IOException {  
  74.   
  75.          os.write(message.getBytes());  
  76.          os.flush();  
  77.   
  78.         message = null; // 指令为空,等待下一个指 ,  
  79.      }  
  80.   
  81.      public synchronized void stop() {  
  82.         stop = true;  
  83.          if (os != null) {  
  84.              try {  
  85.                  os.close();  
  86.                 os = null;  
  87.              } catch (IOException e) {  
  88.   
  89.                  e.printStackTrace();  
  90.              }  
  91.          }  
  92.   
  93.          notify();  
  94.   
  95.      }  
  96.   
  97. }  

2。IncepterTask。负责接收从数据传送过来的数据,并且根据服务器要求,做自己的工作,代码:

代码
  1. package org.wuhua.net;  
  2.   
  3. import java.io.DataInputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.DataInputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;   
  9. /**  
  10. * <b>类名:CheckForm.java</b> </br> 编写日期: 2006-7-18 <br/>  
  11. * 程序功能描述:检测从服务器上传过来的数据,并判断是否需要弹出窗口显示给用户,并更新相关的信息 <br/>  
  12. * 此程序的操作网络的核心类,负责网络,跟一些消息 辑处理<br/> Demo: <br/>  
  13. *   
  14. * Bug: <br/>  
  15. *   
  16. * 程序变更日期 <br/> 变更作 <br/> 变更说明 <br/>  
  17. *   
  18. * @author wuhua </br> <a href="mailto:rrq12345@163.com">rrq12345@163.com</a>  
  19. */  
  20. public class IncepterTask implements Runnable   {  
  21.      private DataInputStream in;  
  22.      private boolean stop;  
  23.      private byte[] content = null;  
  24.      IncepterTask(InputStream _in) {  
  25.         in = new DataInputStream(_in);    
  26.      }  
  27.      /**  
  28.       * 程序启动后不停的获取数据,并根据数据的类型做相应的工作  
  29.       */  
  30.      public synchronized void run() {  
  31.          try{       
  32.              runImpl();  
  33.          }catch(Throwable tw){  
  34.              tw.printStackTrace();  
  35.                
  36.          }  
  37.           
  38.      }  
  39.   
  40.      private void runImpl() throws IOException {  
  41.          while (true) {  
  42.              if (stop)  
  43.                  break;  
  44.               
  45.              while (true) {  
  46.                  readMessage();  
  47.   
  48.                  if (content == null)  
  49.                      continue;             
  50.                   
  51.                  executeIncepter( );  
  52.    
  53.              }  
  54.          }  
  55.      }  
  56.       
  57.      //获取数据后的具体工作  
  58.      private void executeIncepter() {  
  59.          // TODO Auto-generated method stub  
  60.           
  61.      }  
  62.      /**  
  63.       * 读取数据  
  64.       *   
  65.       * @throws IOException  
  66.       */  
  67.      private void readMessage() throws IOException {  
  68.   
  69.         content = null;  
  70.          int length = 0;  
  71.          int offer = 0;  
  72.   
  73.          // 如果没数据则阻塞在这 ,等啊等啊等啊.  
  74.         length = in.readShort();  
  75.   
  76.          if (length < 0)  
  77.             lengthlength = length + 65536;  
  78.         content = new byte[length];  
  79.          while (true) {  
  80.              int i = in.read(content, offer, length - offer);  
  81.              offer += i;  
  82.              if (offer == length)  
  83.                  break;  
  84.          }  
  85.      }  
  86.    
  87.      public synchronized void stop() {  
  88.         stop = true;  
  89.          if(in != null){  
  90.              try {  
  91.                  in.close();  
  92.                 in = null;  
  93.              } catch (IOException e) {  
  94.                    
  95.                  e.printStackTrace();  
  96.              }  
  97.          }  
  98.               
  99.          notify();  
  100.            
  101.      }  
  102. }  

1。Client类,负责打开连接以及管理,接收数据,与发送数据线程。

代码
  1. package org.wuhua.net;  
  2. import java.io.IOException;  
  3.   
  4. import javax.microedition.io.Connector;  
  5. import javax.microedition.io.SocketConnection;  
  6. public class Client {  
  7.     static SocketConnection sc;   
  8.     static IncepterTask it;  
  9.     static SenderTask st;  
  10.       
  11.     static int restart;  
  12.     public static boolean IS_START = false;  
  13.    
  14.     private Client() {  
  15.         try {  
  16.              sc = (SocketConnection) Connector.open(  
  17.                     "socket://127。0。0。1:1232", 3, false);   
  18.               
  19.               
  20.              it = new IncepterTask(sc.openInputStream());  
  21.             new Thread(it).start(); //启动拦截数据线程  
  22.               
  23.              st = new SenderTask(sc.openOutputStream());  
  24.             new Thread(st).start();//启动数据发送线程  
  25.              IS_START = true;  
  26.               
  27.          } catch (Exception e) {  
  28.                
  29.              IS_START = false;  
  30.                
  31.          }  
  32.      }  
  33.       
  34.     public  static void start(){  
  35.         new Client();         
  36.      }  
  37.       
  38.     public static void stop() {  
  39.         try {  
  40.             if (sc != null)  
  41.                  sc.close();  
  42.             if(it != null)  
  43.                  it.stop();  
  44.             if(st != null)  
  45.                  st.stop();  
  46.          } catch (IOException e) {  
  47.                
  48.          }  
  49.          sc = null;    
  50.          st = null;  
  51.          it = null;  
  52.      }  
  53.   
  54.     /**
  55.       * 发送指令
  56.       */  
  57.   
  58.     public static void sendDictate(String type) {  
  59.   
  60.         try {  
  61.              st.send(type);  
  62.          } catch (Exception e) {  
  63.              e.printStackTrace();  
  64.          }  
  65.   
  66.      }  
  67.   
  68. }  

以上是连接网络的基本框架,如果需要加些功能可以很好的利用这些代码

阅读全文>>
2007-07-16 18:54:24 |  浏览 (2222) | 
  1/1页  共 3 篇博文 1   跳至