smslib发短信实例代码(电脑发短信)

 更新时间:2013年12月19日 10:49:26   作者:  
smslib发短信实例,大家可以参考使用开发自己的程序

SMSLib是一个由很多程序员共同开发的,用于支持GSM猫或者手机发送短信的开源项目,下面来个实例代码

复制代码 代码如下:

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<InboundMessage> 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<InboundMessage>();
   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 <enter> 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);
  }
 }

}

相关文章

  • 浅谈多线程_让程序更高效的运行

    浅谈多线程_让程序更高效的运行

    下面小编就为大家带来一篇浅谈多线程_让程序更高效的运行。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • 利用Thumbnailator轻松实现图片缩放、旋转与加水印

    利用Thumbnailator轻松实现图片缩放、旋转与加水印

    java开发中经常遇到对图片的处理,JDK中也提供了对应的工具类,不过处理起来很麻烦,Thumbnailator是一个优秀的图片处理的开源Java类库,处理效果远比Java API的好,这篇文章主要介绍了利用Thumbnailator如何轻松的实现图片缩放、旋转与加水印,需要的朋友可以参考下
    2017-01-01
  • Java中break的第三种用法说明

    Java中break的第三种用法说明

    这篇文章主要介绍了Java中break的第三种用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • java.lang.NullPointerException 如何处理空指针异常的实现

    java.lang.NullPointerException 如何处理空指针异常的实现

    这篇文章主要介绍了java.lang.NullPointerException 如何处理空指针异常的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-12-12
  • Spring实战之Bean的作用域singleton和prototype用法分析

    Spring实战之Bean的作用域singleton和prototype用法分析

    这篇文章主要介绍了Spring实战之Bean的作用域singleton和prototype用法,结合实例形式分析了Bean的作用域singleton和prototype相关使用方法及操作注意事项,需要的朋友可以参考下
    2019-11-11
  • Redis 订阅发布_Jedis实现方法

    Redis 订阅发布_Jedis实现方法

    下面小编就为大家带来一篇Redis 订阅发布_Jedis实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • Mybatis动态SQL foreach批量操作方法

    Mybatis动态SQL foreach批量操作方法

    这篇文章主要介绍了Mybatis动态SQL foreach批量操作方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-03-03
  • Java中Integer.parseInt和Integer.valueOf区别小结

    Java中Integer.parseInt和Integer.valueOf区别小结

    在Java中,Integer.parseInt()和Integer.valueOf()都可以将字符串转换为整数类型,那么他们有哪些区别呢,本文就来详细的介绍一下
    2023-09-09
  • Java for循环常见优化方法案例详解

    Java for循环常见优化方法案例详解

    这篇文章主要介绍了Java for循环常见优化方法案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • java+Okhttp3调用接口的实例

    java+Okhttp3调用接口的实例

    这篇文章主要介绍了java+Okhttp3调用接口的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12

最新评论