扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:中国IT实验室 来源:中国IT实验室 2007年9月22日
关键字:
在本页阅读全文(共4页)
通过以下HelloWorld.java的主要部分代码:
HelloWorld.java |
public class HelloWorld extends MIDlet implements Runnable, SnapEventListener { /***开始运行***/ public void startApp() { /***初始化获取访问权限***/ protocol = getAppProperty("SNAP-Mobile-Protocol"); hostname = getAppProperty("SNAP-Mobile-Host"); port = Integer.parseInt(getAppProperty("SNAP-Mobile-Port")); username = getAppProperty("username"); password = getAppProperty("password"); gameClassID = 49152; thread = new Thread(this); thread.start(); } /***线程运行***/ public void run() { try { /***创建服务器连接对象(主机名,端口号,协议)***/ comm = new ServerComm(hostname, port, protocol); /***添加snap事件监听器到该对象***/ comm.addSnapEventListener(this); /***统一登陆SNAP,IMPS和Web 服务器(用户名,密码,游戏类ID,显示属性)***/ comm.extendedLogin(username, password, gameClassID, null); /***发送信息***/ comm.sendBuddyMessage(username, "Hello World!"); } catch (Exception e) { } /***该对象移除snap事件监听器***/ comm.removeSnapEventListener(this); /***统一登出服务器***/ comm.unifiedLogout(); } /***处理事件***/ public void processEvents(Vector list) { ItemList il; for (int i=0; i /***获取所有来自服务器的信息***/ il = (ItemList)list.elementAt(i); switch(il.getInteger("id")) { case ItemList.IMPS_IM_MESSAGE: console.println("- IMPS_IM_MESSAGE EVENT", Console.YELLOW); console.println(" from id: " + il.getString("fromID"), Console.WHITE); console.println(" from name: " + il.getString("fromName"), Console.WHITE); console.println(" location: " + il.getString("location"), Console.WHITE); console.println(" message: " + il.getString("message"), Console.WHITE); if (username.equals( il.getString("fromName"))) { done = true; console.println("Message from ourself, done!", Console.WHITE); } else { console.println("Message from someone else, not done.", Console.WHITE); } break; } } } } |
我们可以清楚地看到,Nokia已经在SNAPMobile里面已经封装好了连接接口和连接框架。让我们登陆SNAPMobile服务器成为一件很简单的事情。
(具体细节可以参考《SNAP_Mobile_HelloWorld_Tutorial_v1_0_en.pdf》的章节)
说到连接接口和连接框架咱们就不能跳过实现SNAP Mobile的通信协议。
濠碘€冲€归悘澶愬箖閵娾晜濮滈悽顖涚摃閹烩晠宕氶崶鈺傜暠闁诡垰鍘栫花锛勬喆椤ゅ弧濡澘妫楅悡娆撳嫉閳ь剟寮0渚€鐛撻柛婵呮缁楀矂骞庨埀顒勫嫉椤栨瑤绻嗛柟顓у灲缁辨繈鏌囬敐鍕杽閻犱降鍨藉Σ鍕嚊閹跺鈧﹦绱旈幋鐐参楅柡鍫灦閸嬫牗绂掔捄铏规闁哄嫷鍨遍崑宥夋儍閸曨剚浠樺ù锝嗗▕閳ь剚鏌ㄧ欢鐐寸▕鐎b晝顏遍柕鍡嫹