smslib發(fā)短信實(shí)例代碼(電腦發(fā)短信)
SMSLib是一個(gè)由很多程序員共同開發(fā)的,用于支持GSM貓或者手機(jī)發(fā)送短信的開源項(xiàng)目,下面來個(gè)實(shí)例代碼
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 {
// 短信網(wǎng)關(guān)
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);
}
}
// 初始化短信網(wǎng)關(guān)
gateway = new SerialModemGateway("modem." + portName, portName, port,
"wavecom", "17254");
} catch (Exception e) {
LOG.error("網(wǎng)關(guān)初始化失?。? + 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);
}
/**
* 發(fā)送短信
* @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);// 這句話是發(fā)中文短信必須的
srv.sendMessage(msg);
}
/**
* 發(fā)送消息類
* @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 + "條;發(fā)件人:"+msg.getOriginator() + ";內(nèi)容:" + msg.getText() + "\n");
//sb.append(msg.toString() + "\n");
LOG.info("第" + num + "條;發(fā)件人:"+msg.getOriginator() + ";內(nèi)容:" + 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);
}
}
}
相關(guān)文章
利用Thumbnailator輕松實(shí)現(xiàn)圖片縮放、旋轉(zhuǎn)與加水印
java開發(fā)中經(jīng)常遇到對(duì)圖片的處理,JDK中也提供了對(duì)應(yīng)的工具類,不過處理起來很麻煩,Thumbnailator是一個(gè)優(yōu)秀的圖片處理的開源Java類庫,處理效果遠(yuǎn)比Java API的好,這篇文章主要介紹了利用Thumbnailator如何輕松的實(shí)現(xiàn)圖片縮放、旋轉(zhuǎn)與加水印,需要的朋友可以參考下2017-01-01java.lang.NullPointerException 如何處理空指針異常的實(shí)現(xiàn)
這篇文章主要介紹了java.lang.NullPointerException 如何處理空指針異常的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12Spring實(shí)戰(zhàn)之Bean的作用域singleton和prototype用法分析
這篇文章主要介紹了Spring實(shí)戰(zhàn)之Bean的作用域singleton和prototype用法,結(jié)合實(shí)例形式分析了Bean的作用域singleton和prototype相關(guān)使用方法及操作注意事項(xiàng),需要的朋友可以參考下2019-11-11Redis 訂閱發(fā)布_Jedis實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猂edis 訂閱發(fā)布_Jedis實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06Mybatis動(dòng)態(tài)SQL?foreach批量操作方法
這篇文章主要介紹了Mybatis動(dòng)態(tài)SQL?foreach批量操作方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03Java中Integer.parseInt和Integer.valueOf區(qū)別小結(jié)
在Java中,Integer.parseInt()和Integer.valueOf()都可以將字符串轉(zhuǎn)換為整數(shù)類型,那么他們有哪些區(qū)別呢,本文就來詳細(xì)的介紹一下2023-09-09Java for循環(huán)常見優(yōu)化方法案例詳解
這篇文章主要介紹了Java for循環(huán)常見優(yōu)化方法案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08java+Okhttp3調(diào)用接口的實(shí)例
這篇文章主要介紹了java+Okhttp3調(diào)用接口的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12