短信貓簡單配置:https://www.cnblogs.com/Big-Boss/p/9699880.html
測試發送短信代碼:https://www.cnblogs.com/Big-Boss/p/9699960.html
讀取短信:
package utils; import java.util.ArrayList; import java.util.List; import org.smslib.AGateway; import org.smslib.AGateway.GatewayStatuses; import org.smslib.AGateway.Protocols; import org.smslib.ICallNotification; import org.smslib.IGatewayStatusNotification; import org.smslib.IInboundMessageNotification; import org.smslib.IOrphanedMessageNotification; import org.smslib.InboundMessage; import org.smslib.InboundMessage.MessageClasses; import org.smslib.Message.MessageTypes; import org.smslib.Service; import org.smslib.modem.SerialModemGateway; /** * 讀取短信 * @author 【】 * */ public class ReadMessagesUtil { public static Service srv = Service.getInstance(); public void doIt() throws Exception { List<InboundMessage> msgList; InboundNotification inboundNotification = new InboundNotification(); CallNotification callNotification = new CallNotification(); GatewayStatusNotification statusNotification = new GatewayStatusNotification(); OrphanedMessageNotification orphanedMessageNotification = new OrphanedMessageNotification(); // ---------------創建串口設備,如果有多個,就創建多個-------------- // 1、連接網關的id // 2、com口名稱,如COM1或/dev/ttyS1(根據實際情況修改) // 3、串口波特率,如9600(根據實際情況修改) // 4、開發商 // 5、型號 SerialModemGateway gateway = new SerialModemGateway("modem.com6", "COM6", 9600, "null", "null"); gateway.setProtocol(Protocols.PDU); // 設置true,表示該網關可以接收短信,根據需求修改 gateway.setInbound(true); // 設置true,表示該網關可以發送短信,根據需求修改 gateway.setOutbound(true); // sim卡鎖,一般默認為0000或1234 gateway.setSimPin("1234"); try { // 接收到新短信時回調 srv.setInboundMessageNotification(inboundNotification); // 接收到電話時回調 srv.setCallNotification(callNotification); // gateway狀態發生改變時回調 srv.setGatewayStatusNotification(statusNotification); // 監測到孤兒短信(不完整短信)存在時回調 srv.setOrphanedMessageNotification(orphanedMessageNotification); // 將網關添加到短信貓服務中 srv.addGateway(gateway); // 啟動服務 srv.startService(); System.out.println("---------------------------服務啟動完畢----------------------------"); // 設置加密 //srv.getKeyManager().registerKey("短信中心號碼", new AESKey(new SecretKeySpec("0011223344556677".getBytes(), "AES"))); msgList = new ArrayList<InboundMessage>(); // 讀取短信 srv.readMessages(msgList, MessageClasses.ALL); for (InboundMessage msg : msgList) { System.out.println(msg.getMemIndex()); System.out.println("發信人:" + msg.getOriginator()); System.out.println("短信內容:" + msg.getText()); /*System.out.println("-----------------開始刪除----------------"); srv.deleteMessage(msg); //刪除短信 System.out.println("-----------------刪除完畢----------------");*/ } System.out.println("Now Sleeping - Hit <enter> to stop service."); System.in.read(); System.in.read(); } catch (Exception e) { e.printStackTrace(); } finally { srv.stopService(); srv.removeGateway(gateway); } } public class InboundNotification implements IInboundMessageNotification { public void process(AGateway gateway, MessageTypes msgType, InboundMessage msg) { if (msgType == MessageTypes.INBOUND) System.out.println(">>> New Inbound message detected from Gateway: " + gateway.getGatewayId()); else if (msgType == MessageTypes.STATUSREPORT) System.out.println(">>> New Inbound Status Report message detected from Gateway: " + gateway.getGatewayId()); System.out.println(msg); } } public class CallNotification implements ICallNotification { public void process(AGateway gateway, String callerId) { System.out.println(">>> New call detected from Gateway: " + gateway.getGatewayId() + " : " + callerId); } } public class GatewayStatusNotification implements IGatewayStatusNotification { public void process(AGateway gateway, GatewayStatuses oldStatus, GatewayStatuses newStatus) { System.out.println(">>> Gateway Status change for " + gateway.getGatewayId() + ", OLD: " + oldStatus + " -> NEW: " + newStatus); } } public class OrphanedMessageNotification implements IOrphanedMessageNotification { public boolean process(AGateway gateway, InboundMessage msg) { System.out.println(">>> Orphaned message part detected from " + gateway.getGatewayId()); System.out.println(msg); return false; } } public static void main(String args[]) { ReadMessagesUtil app = new ReadMessagesUtil(); try { app.doIt(); } catch (Exception e) { e.printStackTrace(); } } }
讀取到的短信信息: