当前页面: 开发资料首页 → J2ME 专题 → Wireless Messaging API(4)
Wireless Messaging API(4)
摘要: Wireless Messaging API(4) 
内容: 上面介绍了如何应用WMA发送短信息,应用WMA接收短信息更加简单,当打开一个Server Connection后(此时建立connection时,不需指定电话号码,只需要指定协议以及监听端口号),直接调用MessageConnection接口的receive()方法,该方法返回在当前设备的指定端口收到的下一个短信息。如果没有短信息到达,那么该方法将会阻塞,并等待下一个短信息的到达,或者由另一个不同的线程关闭此连接。请看下面的示例代码:
import java.io.*;
import javax.microedition.io.*;
import javax.wireless.messaging.*;
MessageConnection conn = null;
String url = "sms://:5678"; // no phone number!
try {
 conn = (MessageConnection) Connector.open( url );
 while( true ){
  Message msg = conn.receive(); // blocks
  if( msg instanceof BinaryMessage ){
   byte[] data =
     ((BinaryMessage) msg).getPayloadData();
   // do something here
  } else {
   String text =
     ((TextMessage) msg).getPayloadText();
   // do something here
  }
 }
}
catch( Exception e ){
 // handle it
}
finally {
 if( conn != null ){
  try { conn.close(); } catch( Exception e ){}
 }
}
  WMA的任务只是接收和发送短信息,并不去解释它们。通常来说,应用程序之间会来回发送二进制数据类型的信息,java.io包的DataInputstream,DataOutputstream,ByteArrayInputstream
和ByteArrayOutputStream 类在对这些二进制数据解码和编码时十分有用。
访问下面两个网址可以获得更多关于WMA的信息:
[1]http://java.sun.com/products/wma/
[2]http://jcp.org/aboutJava/communityprocess/final/jsr120/index.html.  
 
↑返回目录 
前一篇: 
封装多MIDIet程序  
后一篇: 
Wireless Messaging API(3)