如何起动一个应用程序在收到短信时
2010-04-02 14:39
288 查看
How to cell phone automatically active a J2ME application when receives a special message? WMA(Wireless message API) and Push Registry of MIDP 2.0 had provided all available methods.
Firstly, you must specify a port number in a J2ME application to receive message. For example:
And, you need to know the format of message specified port number either. It is a text message included a UDH header. The UDH header is "060504<Destinating Port>(octets)<Originating Port>(octets)". If you have a NowSMS gateway(http://www.nowsms.com), you can easily use a URL of NowSMS to send out this message. The URL is http://<NowSMS gateway IP address>:<Port Number>/?phone=<Cell Phone Number>&udh=0605041E611E61&text=<Message content>. For detail of message PDU, you can use a tool of port monitor to catch them. After you send out this message, if the J2ME application of destinating cell phone is running, it will can receive this message.
To automatically active a J2ME application when it isn't running, you have to use PushRegistry method to register this event in J2ME program. I will provide a complete code for your reference.
Currently, this J2ME application isn't supported by all cell phone. It need Midlet 2.0 and WMAPI. You can find a available device list on http://www.j2mepolish.org/devices/midp2.html
Informative Reference:
"The MIDP 2.0 Push Registry" URL: http://developers.sun.com/techtopics/mobility/midp/articles/pushreg/
"Wireless Messaging API (WMA); JSR 120, JSR 205" URL: http://java.sun.com/products/wma/index.jsp
"The Wireless Messaging API" URL: http://developers.sun.com/techtopics/mobility/midp/articles/wma/index.html
A complete message receiver of J2ME, for Example: (BTW, when this program is launched again in J2ME wireless Toolkit 2.2, it will catch an IOException. I ignored it. If u know why, pls tell me. Thanks)
Firstly, you must specify a port number in a J2ME application to receive message. For example:
String smsPort = "7777"; String smsConnection = "sms://:" + smsPort; MessageConnection smsconn; try { smsconn = (MessageConnection) Connector.open(conn); } catch (IOException x) { //... } |
To automatically active a J2ME application when it isn't running, you have to use PushRegistry method to register this event in J2ME program. I will provide a complete code for your reference.
Currently, this J2ME application isn't supported by all cell phone. It need Midlet 2.0 and WMAPI. You can find a available device list on http://www.j2mepolish.org/devices/midp2.html
Informative Reference:
"The MIDP 2.0 Push Registry" URL: http://developers.sun.com/techtopics/mobility/midp/articles/pushreg/
"Wireless Messaging API (WMA); JSR 120, JSR 205" URL: http://java.sun.com/products/wma/index.jsp
"The Wireless Messaging API" URL: http://developers.sun.com/techtopics/mobility/midp/articles/wma/index.html
A complete message receiver of J2ME, for Example: (BTW, when this program is launched again in J2ME wireless Toolkit 2.2, it will catch an IOException. I ignored it. If u know why, pls tell me. Thanks)
package SMSReceiver; /** * <p>Title: SMS receiver</p> * * <p>Description: </p> * * </p>Copyright: Copyright (c) 2006</p> * * <p>Company: </p> * * jql1k@hotmail.com * @version 1.0 */ import javax.microedition.midlet.*; import javax.microedition.io.*; import javax.microedition.lcdui.*; import javax.wireless.messaging.*; import java.io.IOException; public class SMSReceiver extends MIDlet implements CommandListener, Runnable, MessageListener{ /** user interface command for indicating Exit request. */ Command exitCommand = new Command("Exit", Command.EXIT, 2); /** user interface text box for the contents of the fetched URL. */ Alert content; /** current display. */ Display display; /** instance of a thread for asynchronous networking and user interface. */ Thread thread; /** Flag to signal end of processing. */ boolean done; /** The port on which we listen for SMS messages */ String smsPort; /** SMS message connection for inbound text messages. */ MessageConnection smsconn; /** Current message read from the network. */ Message msg; /** Address of the message's sender */ String senderAddress; /** The screen to display when we return from being paused */ Displayable resumeScreen; private boolean readMessages = true; private int pendingMessages; String smsConnection; String []connections; public SMSReceiver() { smsPort = "7777"; //getAppProperty("SMS-Port"); display = Display.getDisplay(this); content = new Alert("SMS Receive"); content.setTimeout(Alert.FOREVER); content.addCommand(exitCommand); content.setCommandListener(this); //content.setString("Receiving..."); content.setString("Waiting for SMS on port " + smsPort + "..."); resumeScreen = content; smsConnection = "sms://:" + smsPort; connections = PushRegistry.listConnections(true); int n = connections.length; if( n == 0 ) { if(!openAndregisterConn(smsConnection)) return; } else { boolean isFound = false; for(int ccnt=0; ccnt < connections.length; ccnt++) { if(connections[ccnt].indexOf(smsConnection) >= 0) { isFound = true; break; } } if(!isFound) { if(!openAndregisterConn(smsConnection)) return; } } } public boolean openAndregisterConn(String conn) { try { smsconn = (MessageConnection) Connector.open(conn); PushRegistry.registerConnection(conn, "SMSReceiver.SMSReceiver", "*"); } catch (IOException x) { x.printStackTrace(); return false; } catch (ClassNotFoundException x) { x.printStackTrace(); return false; } return true; } /** * Start creates the thread to do the MessageConnection receive * text. * It should return immediately to keep the dispatcher * from hanging. */ public void startApp() { /** SMS connection to be read. */ /** Open the message connection. */ if (smsconn == null) { connections = PushRegistry.listConnections(true); if (connections.length > 0) { // There is a pending datagram that can be received. for(int ccnt=0; ccnt < connections.length; ccnt++) { if(connections[ccnt].indexOf(smsConnection) >= 0) try { smsconn = (MessageConnection) Connector.open(connections[ccnt]); } catch (IOException ioe) { ioe.printStackTrace(); } break; } } else { // There are not any pending datagrams, but open // the connection for later use. connections = PushRegistry.listConnections(false); if (connections.length > 0) { for(int ccnt=0; ccnt < connections.length; ccnt++) { if(connections[ccnt].indexOf(smsConnection) >= 0) { try { smsconn = (MessageConnection)Connector.open(connections[ccnt]); } catch (IOException ioe) { ioe.printStackTrace(); } break; } } } } } done = false; thread = new Thread(this); thread.start(); content.setString("Waiting for SMS on port " + smsPort + "..."); display.setCurrent(resumeScreen); } /** * Notification that a message arrived. * @param conn the connection with messages available */ public void notifyIncomingMessage(MessageConnection conn) { if (thread == null) { done = false; thread = new Thread(this); thread.start(); } } /** Message reading thread. */ public void run() { /** Check for sms connection. */ try { msg = smsconn.receive(); if (msg != null) { senderAddress = msg.getAddress(); content.setTitle("From: " + senderAddress); if (msg instanceof TextMessage) { content.setString( ( (TextMessage) msg).getPayloadText()); } else { StringBuffer buf = new StringBuffer(); byte[] data = ( (BinaryMessage) msg).getPayloadData(); for (int i = 0; i < data.length; i++) { int intData = (int) data[i] & 0xFF; if (intData < 0x10) { buf.append("0"); } buf.append(Integer.toHexString(intData)); buf.append(' '); } content.setString(buf.toString()); } //content.addCommand(replyCommand); display.setCurrent(content); } } catch (IOException e) { // e.printStackTrace(); } } /** * Pause signals the thread to stop by clearing the thread field. * If stopped before done with the iterations it will * be restarted from scratch later. */ public void pauseApp() { done = true; thread = null; resumeScreen = display.getCurrent(); } /** * Destroy must cleanup everything. The thread is signaled * to stop and no result is produced. * @param unconditional true if a forced shutdown was requested */ public void destroyApp(boolean unconditional) { done = true; thread = null; if (smsconn != null) { try { smsconn.close(); } catch (IOException e) { // Ignore any errors on shutdown } } } /** * Respond to commands, including exit * @param c user interface command requested * @param s screen object initiating the request */ public void commandAction(Command c, Displayable s) { try { if (c == exitCommand || c == Alert.DISMISS_COMMAND) { destroyApp(false); notifyDestroyed(); } } catch (Exception ex) { ex.printStackTrace(); } } } |
相关文章推荐
- 如何让应用程序只有一个实例运行
- 如何使用LTFrame去创建一个应用程序?
- 如何创建一个iPhone或iPad的应用程序,并在App Store成功游戏
- 如何在iphone应用程序中发送短信
- 一个应用程序多次点击时,如何只让它只运行一个
- iOS 如何在一个应用程序中调用另一个应用程序
- 如何限制到 Visual C++ 中的一个实例的 32 位应用程序
- ASP.NET MVC Tip #17 – 如何运行一个ASP.NET MVC应用程序
- 如何使应用程序只运行一个实例
- VC中如何让一个应用程序只启动一次
- 如何编写一个编译c#控制台应用程序的批处理程序
- [转] 如何用VB.Net创建一个三层的数据库应用程序
- Linux系统:Ubuntu下如何在桌面创建一个应用程序的“快捷方式”?
- 如何处理服务器SSL收到了一个弱临时Diffie-Hellman 密钥?
- 【面试题解答】如何在一个对象释放的时候收到通知
- 如何让Qt应用程序只有一个实例
- 一个发送邮件功能,用户反映没有收到邮件,如何处理bug
- CUDA: 如何创建一个CUDA应用程序
- 如何使应用程序只运行一个实例
- QuickFIX/N入门:一、如何创建一个QuickFIX/N的应用程序