⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 twomodemexample.java

📁 控制手机发送短信程序
💻 JAVA
字号:
package mms;import java.io.File;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.util.Date;import java.util.Properties;import com.objectxp.mms.MMSAddress;import com.objectxp.mms.MMSException;import com.objectxp.mms.MMSNotification;import com.objectxp.mms.MMSService;import com.objectxp.mms.MMSServiceFactory;import com.objectxp.mms.message.MMSMessage;import com.objectxp.mms.message.SMILMessage;import com.objectxp.mms.protocol.ProtocolException;import com.objectxp.mms.transport.TransportException;import com.objectxp.msg.GsmSmsService;import com.objectxp.msg.Message;import com.objectxp.msg.MessageEvent;import com.objectxp.msg.MessageEventListener;import com.objectxp.msg.MessageException;import com.objectxp.msg.MultiPartReceiver;import com.objectxp.msg.SmsMessage;import com.objectxp.msg.SmsService;/** * The ReceiveExample shows how to receive a MultiMediaMessage (MMS) with two * modems. *  * - One modem is listening as GSM service for incomming SMS's and could  *   also be used to send common SMS's. * - The other modem is used by the MMSService. With this modem, a GPRS connection *   to the MMS Relay/Server is established to fetch and send MMS's. *  * If a MMS is sent, the GSMService will receive a WapPush message, parse it and promote * it as MMSNotification. The MMSService will fetch the MMS with the ID of the  * MMSNotification. The response of the MMSService fetch methode is the MMS. *  */public class TwoModemExample implements MessageEventListener{    private SmsService iSmsService;    private MMSService iMmsService;    private String iRecipient;    private String iConfigFile = "bin/jsms.conf";    /**     * usage: TwoModemExample <receiver> <configFile>     */    public static void main(String[] args) throws Exception    {      TwoModemExample receiver = new TwoModemExample();          // read attributes      if (args.length<1 || args.length>2) {        System.out.println("usage: TwoModemExample <receiver> <propertyFile> \n\n" +          "<receiver>     is the phoneNumer of the destination of the MMS.\n" +          "<propertyFile> is the reference to the configuration file. Per default,\n" +          "               the jMMS.conf file is taken. This argment is optional.\n\n");        return;      }          if (args.length>=1){        receiver.iRecipient=args[0];      }      if (args.length>=2) {        receiver.iConfigFile = args[1];        System.out.println("TwoModemExample (using property file ["+receiver.iConfigFile+"])");      }             try{        // start GSMServiece to send and receive SMS and also receive the MMSNotification        receiver.connectGSMService();        // start MMSService to send and fetch MMS        receiver.connectMMSServcie();      } catch (Exception e){        System.err.println("Faild starting services, disconnect!");        e.printStackTrace();        if (receiver.iSmsService != null){          try{receiver.iSmsService.destroy();}catch (Exception ignore){}        }        if (receiver.iMmsService != null){          try{receiver.iMmsService.disconnect();}catch (Exception ignore){}        }        return;      }            System.out.println("TwoModemExample ready to send and receive messages");      receiver.userInput();    }    /**     * receiving Message events coming over the GSM connection     *      * If the MessageEvent is holding a MMSNotification, the      * MMS will be fetched using the MMSService.     */    public void handleMessageEvent(MessageEvent event) {      if (event==null){        System.err.println("event is null!!!");        return;      }      if (event.getException() != null){        System.err.println("received Exception :"+ event.getException().getMessage());        event.getException().printStackTrace();      }      if (event.getType()==MessageEvent.MESSAGE_RECEIVED) {        Message msg = event.getMessage();        if (msg instanceof MMSNotification){          /*          * MMSNotification arrived --> fetch mms          */          MMSNotification notification = (MMSNotification)msg;          System.out.println("received MMSNotification, fetch MMS id:"+notification.getTransactionId());          try {            // fetch mms            MMSMessage mms = iMmsService.fetch(notification);            // show msg            System.out.println("fetched mms:"+mms);          } catch (Exception e) {            System.err.println("could not fetch MMSMessage with id "+notification.getTransactionId());            e.printStackTrace();          }        }        else         {          // received Message is a common SMS (SmsMessage, EmsMessage, SmartMessage)           System.out.println("received msg:"+msg);        }      }    }      /* ******************************************************************* */    /*                         private                                     */    /* ******************************************************************* */    /**     * helper method to invoke and initialize a jSMS GsmSmsService      */    private void connectGSMService() throws IOException, MessageException    {      // Create the SmsService       iSmsService = new GsmSmsService();          // instantiate receiver with the cache limits.      MultiPartReceiver mpr = new MultiPartReceiver(360*1000, 50, this);      // initialize service      InputStream in = this.getClass().getClassLoader().getResourceAsStream(iConfigFile);      if (in == null)throw new IllegalArgumentException("could not find file:"+iConfigFile);      Properties props = new Properties();      props.load(in);      iSmsService.init(props);      // add the multipart receiver as listener to the service      iSmsService.addMessageEventListener(mpr);      iSmsService.connect();      iSmsService.startReceiving();      System.out.println("GSMService startedReceiving -> ready for incoming MMSNotification");       }        /**      * helper method to invoke and initialize the MMSService     */      private void connectMMSServcie() throws MMSException, ProtocolException, TransportException, IOException    {      // invoke MMSService      MMSServiceFactory factory = MMSServiceFactory.createFactory(new File(iConfigFile));      iMmsService = factory.getDefaultService();      iMmsService.connect();    }        /**     * wait for a input command:     *      * 0: to stop example     * 1: to send a simple sms     * 2: to send a news-MMS     * 3: to send a sport-MMS     * 4: to send a business-MMS     *     */    private void userInput()     {      // wait for manualy break      System.out.print("0: to stop example\n1: to send a simple sms\n2: to send a news-MMS\n3: to send a sport-MMS\n4: to send a business-MMS\n");      int i = -1;			try {				i = System.in.read();			} catch (IOException e) {        System.err.println("could not read input.");        e.printStackTrace();			}      switch (i){        case '0':          // stop GsmService          try {            iSmsService.stopReceiving();						iSmsService.disconnect();					} catch (Exception e) {            System.err.println("could not disconnect SmsService.");            e.printStackTrace();					}          // stop MMSService          try {						iMmsService.disconnect();					} catch (Exception e) {            System.err.println("could not disconnect MmsService.");            e.printStackTrace();					}          return;                  case '1':           // send a simple sms          try {            SmsMessage msg = new SmsMessage();            msg.setMessage("common sms at "+new Date());            msg.setRecipient(iRecipient);						iSmsService.sendMessage(msg);					} catch (Exception e) {            System.err.println("could not send sms.");            e.printStackTrace();					}          break;                  case '2':           sendMMS("./media/news/news.smil");           break;        case '3':           sendMMS("./media/news/sport.smil");           break;                case '4':           sendMMS("./media/news/business.smil");           break;         default:           System.out.println((char)i+": command not supported, ignore.");          break;      }      userInput();    }        /**     * sends a MMS using the MMSService     * @param location : the path to the smil document which should be sent.     */    private void sendMMS(String location)    {      try{        // load smilDocument from the given URL        URL url = this.getClass().getClassLoader().getResource(location);        MMSMessage msg = new SMILMessage(url);        msg.addTO(new MMSAddress(MMSAddress.TYPE_PLMN, iRecipient));          // send Message        iMmsService.send(msg);      } catch (Exception e){        System.err.println("could not send MMS "+location);        e.printStackTrace();      }    }  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -