当前位置:  编程技术>java/j2ee

java使用smslib连接短信猫发送短信代码分享

    来源: 互联网  发布时间:2014-11-02

    本文导语:  代码如下:import java.util.ArrayList;import java.util.List; import org.apache.log4j.Logger;import org.smslib.ICallNotification;import org.smslib.IInboundMessageNotification;import org.smslib.IOutboundMessageNotification;import org.smslib.InboundMessage;import org.smslib.InboundMessage.Message...

代码如下:

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.smslib.ICallNotification;
import org.smslib.IInboundMessageNotification;
import org.smslib.IOutboundMessageNotification;
import org.smslib.InboundMessage;
import org.smslib.InboundMessage.MessageClasses;
import org.smslib.Library;
import org.smslib.Message.MessageEncodings;
import org.smslib.Message.MessageTypes;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.modem.SerialModemGateway;

/**
 * @author terry
 *
 */
public class SmsModem {

 // 短信网关
 private SerialModemGateway gateway = null;
 java.util.ResourceBundle rb = null;//ResourceBundle.getBundle("SMS");
 static SmsModem smsModem = null;
 OutboundNotification outboundNotification = new OutboundNotification();
 private static final Logger LOG = Logger.getLogger(SmsModem.class);
 Service srv;
 InboundNotification inboundNotification = new InboundNotification();
 // Create the notification callback method for inbound voice calls.
 CallNotification callNotification = new CallNotification();

 public SmsModem() {
  try {
   //ReadMessages rm = new ReadMessages();
   //rm.doIt();

   rb = ResourceBundle.getBundle("sms");
   String portName= "COM10";
   int port = 9600;
   LOG.info("default portName:" + portName);
   LOG.info("default port:" + port);
   if(rb != null)
   {
    LOG.info("RB is not null");
    if(rb.getString("smsport") != null && !"".equals(rb.getString("smsport")))
    {
     portName = rb.getString("smsport");
     LOG.info("portName:" + portName);
    }
    if(rb.getString("smsbolv") != null && !"".equals(rb.getString("smsbolv")))
    {
     port = Integer.valueOf(rb.getString("smsbolv"));
     LOG.info("port:" + port);
    }
   }
   // 初始化短信网关
   gateway = new SerialModemGateway("modem." + portName, portName, port,
     "wavecom", "17254");

  } catch (Exception e) {
   LOG.error("网关初始化失败:" + e.getMessage());
   e.printStackTrace();
  }
 }

 public static SmsModem getInstant() {
  if (smsModem == null) {
   smsModem = new SmsModem();
  }
  return smsModem;
 }

 public SerialModemGateway getGateway() {
  return gateway;
 }

 public void sendMessage(String phone, String content) throws Exception {
  doIt(phone, content);
 }

 /**
  * 发送短信
  * @param phone
  * @param content
  * @throws Exception
  */
 public void doIt(String phone, String content) throws Exception {

  OutboundMessage msg;

  LOG.info("Sent Example: Send message from a serial gsm modem.");
  LOG.info(Library.getLibraryDescription());
  LOG.info("Sent Version: " + Library.getLibraryVersion());
  if (srv == null) {
   srv = new Service();
   srv.S.SERIAL_POLLING = true;
   gateway.setInbound(true);
   gateway.setOutbound(true);
   gateway.setSimPin("0000");
   gateway.setOutboundNotification(outboundNotification);
   gateway.setInboundNotification(inboundNotification);
   gateway.setCallNotification(callNotification);
   srv.addGateway(gateway);
   srv.startService();
  }

  if (gateway != null) {
   LOG.info("Sent Modem Information:");
   LOG.info("Sent  Manufacturer: " + gateway.getManufacturer());
   LOG.info("Sent  Model: " + gateway.getModel());
   LOG.info("Sent  Serial No: " + gateway.getSerialNo());
   LOG.info("Sent  SIM IMSI: " + gateway.getImsi());
   LOG.info("Sent  Signal Level: " + gateway.getSignalLevel() + "%");
   LOG.info("Sent  Battery Level: " + gateway.getBatteryLevel() + "%");
  }
  // Send a message synchronously.

  msg = new OutboundMessage(phone, content);
  msg.setEncoding(MessageEncodings.ENCUCS2);// 这句话是发中文短信必须的
  srv.sendMessage(msg);
 }

 /**
  * 发送消息类
  * @author terry
  *
  */
 public class OutboundNotification implements IOutboundMessageNotification {
  public void process(String gatewayId, OutboundMessage msg) {
   LOG.info("Sent Outbound handler called from Gateway: " + gatewayId);
   LOG.info(msg);
  }
 }
 //接收消息类
 public String readMessage()
 {
  StringBuffer sb = new StringBuffer("");
  List msgList;
  // Create the notification callback method for Inbound & Status Report
  // messages.

  try
  {
   System.out.println("Read Example: Read messages from a serial gsm modem.");
   System.out.println(Library.getLibraryDescription());
   System.out.println("Read Version: " + Library.getLibraryVersion());
   // Create new Service object - the parent of all and the main interface
   // to you.
   if (srv == null) {
    srv = new Service();
    srv.S.SERIAL_POLLING = true;
    gateway.setInbound(true);
    gateway.setOutbound(true);
    gateway.setSimPin("0000");
    gateway.setOutboundNotification(outboundNotification);
    gateway.setInboundNotification(inboundNotification);
    gateway.setCallNotification(callNotification);
    srv.addGateway(gateway);
    srv.startService();
   }

   
   // Similarly, you may define as many Gateway objects, representing
   // various GSM modems, add them in the Service object and control all of them.
   //
   // Start! (i.e. connect to all defined Gateways)

   LOG.info("Read Modem Information:");
   LOG.info("Read   Manufacturer: " + gateway.getManufacturer());
   LOG.info("Read   Model: " + gateway.getModel());
   LOG.info("Read   Serial No: " + gateway.getSerialNo());
   LOG.info("Read   SIM IMSI: " + gateway.getImsi());
   LOG.info("Read   Signal Level: " + gateway.getSignalLevel() + "%");
   LOG.info("Read   Battery Level: " + gateway.getBatteryLevel() + "%");
   // Read Messages. The reading is done via the Service object and
   // affects all Gateway objects defined. This can also be more directed to a specific
   // Gateway - look the JavaDocs for information on the Service method calls.
   msgList = new ArrayList();
   this.srv.readMessages(msgList, MessageClasses.ALL);
   int num = 1;
   for (InboundMessage msg : msgList)
   {
    sb.append("第" + num + "条;发件人:"+msg.getOriginator() + ";内容:" + msg.getText() + "n");
    //sb.append(msg.toString() + "n");
    LOG.info("第" + num + "条;发件人:"+msg.getOriginator() + ";内容:" + msg.getText() + "n");
    num++;
    LOG.info(msg);
   }
   // Sleep now. Emulate real world situation and give a chance to the notifications
   // methods to be called in the event of message or voice call reception.
   //System.out.println("Now Sleeping - Hit to terminate.");
   //System.in.read();
  }
  catch (Exception e)
  {
   sb.append(e.getMessage());
   e.printStackTrace();
  }
  finally
  {
   //this.srv.stopService();
  }
  return sb.toString();
 }

 public class InboundNotification implements IInboundMessageNotification
 {
  public void process(String gatewayId, MessageTypes msgType, InboundMessage msg)
  {
   if (msgType == MessageTypes.INBOUND) System.out.println(">>> New Inbound message detected from Gateway: " + gatewayId);
   else if (msgType == MessageTypes.STATUSREPORT) System.out.println(">>> New Inbound Status Report message detected from Gateway: " + gatewayId);
   System.out.println(msg);
   try
   {
    // Uncomment following line if you wish to delete the message upon arrival.
    // srv.deleteMessage(msg);
   }
   catch (Exception e)
   {
    System.out.println("Oops!!! Something gone bad...");
    e.printStackTrace();
   }
  }
 }

 public class CallNotification implements ICallNotification
 {
  public void process(String gatewayId, String callerId)
  {
   System.out.println(">>> New call detected from Gateway: " + gatewayId + " : " + callerId);
  }
 }

}


    
 
 

您可能感兴趣的文章:

  • java使用短信设备发送sms短信的示例(java发送短信)
  • java调用短信猫发短信示例
  • Java发送短信包 LemonSMS
  • Java的短信收发包 jSMSEngine
  • 终于可以用JAVA支持手机短信发送、接收了,高兴散粉!!!
  • 哪位有java通信方面的经验,谈谈如何利用java编写服务器程序来接受unix主机发送的数据信息?
  • vc通过socket发送数据给java的问题
  • 如何在java 客户端使用http将cookies 信息发送给服务端.
  • ******JAVA手机或PDA发送,接收短消息******
  • java ServletResponse 发送mime数据问题(解决了马上结帐!)
  • 100分紧急求助:java程序在linux下不能发送邮件,windows下正常。
  • java能做短信息发送软件吗?接口是什么?谁能明确说说这方面知识
  • java发送mail,smtp的用户名密码问题
  • 小弟初入java这一行,很想看看已经编好的源程序,愿意指教的请发送到samgundam@sina.com!谢谢
  • javascript开源软件 iis7站长之家
  • java发送get请求和post请求示例
  • java网络编程中向指定URL发送GET POST请求示例
  • java发送url请求获取返回值的二种方法
  • java发送邮件的具体实现
  • Java邮件发送程序(可以同时发给多个地址、可以带附件)
  • java使用httpclient发送post请求示例
  • java 发送邮件的实例代码(可移植)
  • Java mail 发送邮件的具体实例
  • JAVA发送HTTP请求,返回HTTP响应内容,应用及实例代码
  • java发送heartbeat心跳包(byte转16进制)
  •  
    本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
    本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • java数据库连接池和数据库连接示例
  • Java连接池 Nanopool
  • Java连接池 Proxool
  • Java连接池 DBCP
  • 如何用java连接mysql数据库?
  • Java数据库连接池 BoneCP
  • 怎样用java调用DLL动态连接库?
  • Linux系统下利用java连接Oracle 10G
  • 请教:java中如何调用linux下的静态连接库.a文件?
  • java如何连接数据库?
  • 请教:java中如何调用linux下的静态连接库.a文件?
  • java连接DB2问题
  • java连接SQL SERVER
  • Java 连接池组件 JConnectionPool
  • oracle和Java的连接,急!!
  • 用java开发数据库,连接db2应该如何设置classpath (或者说应引入哪个包)
  • 在java中怎么连接MSSQL这样的数据库?database的属性里用什么驱动?
  • Java 和 Access数据库连接问题。谢谢!!
  • 请问JAVA怎么连接Sybase ASA 6.0 ?
  • 请问启动的多个java虚拟机如何共享一个连接池?
  • java命名空间java.sql类types的类成员方法: java_object定义及介绍
  • 我想学JAVA ,是买THINK IN JAVA 还是JAVA2核心技术:卷1 好???
  • java命名空间java.awt.datatransfer类dataflavor的类成员方法: imageflavor定义及介绍
  • 请问Java高手,Java的优势在那里??,Java主要适合于开发哪类应用程序
  • java命名空间java.lang.management类managementfactory的类成员方法: getcompilationmxbean定义及介绍
  • 如何将java.util.Date转化为java.sql.Date?数据库中Date类型对应于java的哪个Date呢
  • java命名空间java.lang.management接口runtimemxbean的类成员方法: getlibrarypath定义及介绍
  • 谁有电子版的《Java编程思想第二版(Thinking in java second)》和《Java2编程详解(special edition java2)》?得到给分
  • java命名空间java.lang.management接口runtimemxbean的类成员方法: getstarttime定义及介绍
  • 本人想学java,请问java程序员的待遇如何,和java主要有几个比较强的方向
  • java命名空间java.awt.datatransfer类dataflavor的类成员方法: stringflavor定义及介绍


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3