短信猫新闻   News
联系我们   Contact
搜索   Search

Java开发短信猫 读取短信示例代码 readmessage【短信猫开发】

2018-1-29 16:20:23      点击:

Java开发短信猫是目前常用的一种短信猫开发语言,在我们配置好Java工程之后就是编写代码实现短信猫的收发短信,下面为大家贴出Java开发短信猫 读取短信的代码程序

【Java开发短信猫可以支持目前所有的GSM短信猫设备CDMA短信猫则不支持

Java开发包及相关文档说明可到网站下载中心获取

  
  
import java.util.ArrayList;  
import java.util.List;  
import javax.crypto.spec.SecretKeySpec;  
import org.smslib.AGateway;  
import org.smslib.ICallNotification;  
import org.smslib.IGatewayStatusNotification;  
import org.smslib.IInboundMessageNotification;  
import org.smslib.IOrphanedMessageNotification;  
import org.smslib.InboundMessage;  
import org.smslib.Library;  
import org.smslib.Service;  
import org.smslib.AGateway.GatewayStatuses;  
import org.smslib.AGateway.Protocols;  
import org.smslib.InboundMessage.MessageClasses;  
import org.smslib.Message.MessageTypes;  
import org.smslib.crypto.AESKey;  
import org.smslib.modem.SerialModemGateway;  
  
public class ReadMessages {  
    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();  
        try {  
            System.out.println("Example: Read messages from a serial gsm modem.");  
            System.out.println(Library.getLibraryDescription());  
            System.out.println("Version: " + Library.getLibraryVersion());  
            SerialModemGateway gateway = new SerialModemGateway("modem.com3", "COM7", 115200, null, null);  //端口参数
            gateway.setProtocol(Protocols.PDU);  
            gateway.setInbound(true);  
            gateway.setOutbound(true);  
            srv.setInboundMessageNotification(inboundNotification);  
            srv.setCallNotification(callNotification);  
            srv.setGatewayStatusNotification(statusNotification);  
            srv.setOrphanedMessageNotification(orphanedMessageNotification);  
            srv.addGateway(gateway);  
            srv.startService();  
            System.out.println();  
            System.out.println("Modem Information:");  
            System.out.println(" Manufacturer: " + gateway.getManufacturer());  
            System.out.println(" Model: " + gateway.getModel());  
            System.out.println(" Serial No: " + gateway.getSerialNo());  
            System.out.println(" SIM IMSI: " + gateway.getImsi());  
            System.out.println(" Signal Level: " + gateway.getSignalLevel() + "%");  
            System.out.println(" Battery Level: " + gateway.getBatteryLevel() + "%");  
            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);  
//              srv.deleteMessage(msg);     //删除短信  
            }  
            System.out.println("Now Sleeping - Hit <enter> to stop service.");  
            System.in.read();  
            System.in.read();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
  
    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[]) {  
        ReadMessages app = new ReadMessages();  
        try {  
            app.doIt();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}  
相关连接发送短信代码示例