Java操作簡訊貓發簡訊

來源:互聯網
上載者:User
// SendMessage.java - Sample application.//// This application shows you the basic procedure for sending messages.// You will find how to send synchronous and asynchronous messages.//// For asynchronous dispatch, the example application sets a callback// notification, to see what's happened with messages.package examples.modem;import org.smslib.IOutboundMessageNotification;import org.smslib.Library;import org.smslib.OutboundMessage;import org.smslib.Service;import org.smslib.Message.MessageEncodings;import org.smslib.modem.SerialModemGateway;public class SendMessage{    public void doIt(String PhoneNum,String Mesg) throws Exception    {        Service srv; //聲明一個服務        OutboundMessage msg; //OutboundMessage繼承IOutboundMessageNotification介面,重寫了process方法,用於網關設定回呼函數        OutboundNotification outboundNotification = new OutboundNotification();        System.out.println("Example: Send message from a serial gsm modem.");        System.out.println(Library.getLibraryDescription());//擷取類庫描述        System.out.println("Version: " + Library.getLibraryVersion());//擷取類庫版本        srv = new Service(); //初始化服務                //115200是傳輸速率,一般為9600。可以通過超級終端測試出來,聲明網關 第一個參數為:網關ID,第二個是本機上簡訊貓的com口名稱,第三是傳輸速率,第四是簡訊貓生產廠商,第五裝置的型號(可選)        SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM7", 9600, "Wavecom", "17254");                gateway.setInbound(true);//設定true,表示該網關可以接收簡訊,根據需求修改         gateway.setOutbound(true);//設定true,表示該網關可以傳送簡訊,根據需求修改          gateway.setSimPin("0000");//sim卡鎖,一般預設為0000或1234          gateway.setOutboundNotification(outboundNotification);//傳送簡訊成功後的回調函方法          srv.addGateway(gateway);     //將網關添加到簡訊貓服務中         srv.startService();       //啟動服務,進入簡訊發送就緒狀態          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();        // Send a message synchronously.                msg = new OutboundMessage(PhoneNum, Mesg);        msg.setEncoding(MessageEncodings.ENCUCS2);//這句話是發中文簡訊必須的        srv.sendMessage(msg);        System.out.println(msg);        System.out.println("Now Sleeping - Hit <enter> to terminate.");        System.in.read();        srv.stopService();    }    public class OutboundNotification implements IOutboundMessageNotification    {        public void process(String gatewayId, OutboundMessage msg)        {            System.out.println("Outbound handler called from Gateway: " + gatewayId);            System.out.println(msg);        }    }    public static void main(String args[])    {        SendMessage app = new SendMessage();        try        {            app.doIt("13215243120","Hello,test content");        }        catch (Exception e)        {            e.printStackTrace();        }    }}

需要兩個jar包

首先,把smslib-3.3.0b2.jar和comm.jar,放入工程lib中,javax.comm.properties放到%JAVA_HOME%/jre/lib下,
win32com.dll放到%JAVA_HOME%/jre/bin下。路徑放錯了,調用起來就會報錯的。
環境配置好了以後,把examples\modem下的SendMessage.java和ReadMessages.java拷貝到你的開發工具下

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.