服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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 ){}
    }
  }

扫描关注微信公众号