浏览 1948 次
锁定老帖子 主题:监听短息
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-12-23
最后修改:2010-01-26
import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import com.siemens.mp.gsm.SMS; import com.siemens.mp.NotAllowedException; import java.io.IOException; import com.siemens.mp.io.*; public class StartMIDlet extends MIDlet { static StartMIDlet instance; UIMainList MainList = new UIMainList(); public StartMIDlet() { instance = this; } public void startApp() throws MIDletStateChangeException { try { Display.getDisplay(this).setCurrent(MainList); } catch(Exception e) { System.out.println("e = "+e); } } public void pauseApp() { } public void destroyApp(boolean UIcondition) { } public static void quitApp() { instance.destroyApp(true); instance.notifyDestroyed(); instance = null; } } class UIMainList extends List { private static String[] Str_List = {"A", "B", "C"}; static Displayable instance = null; private Command cmExit; private Command cmInfo; public UIMainList() //throws Exception { super("测试", List.IMPLICIT, Str_List, null); setCommandListener(new CommandListener() { public void commandAction(Command c, Displayable d) { DisplayCommand(c,d); } } ); cmInfo = new Command("Help", 1, 1); cmExit = new Command("Exit", 1, 2); jbInit(); instance = this; temp_run(); } public void temp_run() //throws Exception { Receiver rec = new Receiver(); Connection con = new Connection ("SMS: 223322332"); Alert str = new Alert("fail"); try { con.setListener(rec); //把上面这句注释后程序能跑,一打开模拟器就死。为什么? //没有这句,我就监听不到短信的到来啊 } catch(Exception e) { Display.getDisplay(StartMIDlet.instance).setCurrent(str); } } public void DisplayCommand(Command c,Displayable d) { if(c == cmExit) { instance = null; StartMIDlet.quitApp(); } else if( c == cmInfo) { Alert Al = new Alert("Help"); Al.setString("测试的帮助文件"); Display.getDisplay(StartMIDlet.instance).setCurrent(Al); } } private void jbInit() { addCommand(cmInfo); addCommand(cmExit); } } class Receiver implements ConnectionListener { public void receiveData(byte[] data) { try { Alert str = new Alert("sms succ"); Display.getDisplay(StartMIDlet.instance).setCurrent(str); } catch(Exception e) { Alert str = new Alert("sms fail"); Display.getDisplay(StartMIDlet.instance).setCurrent(str); } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |