📄 receiveexample.java
字号:
package mms;import java.io.File;import java.io.IOException;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.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.SmsService;/** * This example demonstrates how to receive a MultiMediaMessage (MMS) over MM1. * * The MMS Relay/Server sends a notification using WAP-Push (SMS) to indicate * incoming messages. Therefore, a GsmSmsService will be used to receive those * notifications. * */public class ReceiveExample implements MessageEventListener{ private SmsService service; private MMSService mmsService; private String configFile; private String serviceName; public static void main(String[] args) throws Exception { if(args.length < 1 ) { System.err.println("Usage: ReceiveExample <configfile> [service-name]"); System.exit(1); } ReceiveExample receiver = new ReceiveExample(args[0], args.length>1?args[1]:null); // Start receiving MMS receiver.startGsmService(); // Wait for key-press while( System.in.available() < 1 ) { Thread.sleep(1000); } receiver.shutdown(); } public ReceiveExample(String configFile, String serviceName) { this.configFile=configFile; this.serviceName=serviceName; } /** * Implementation of the MessageEventListener interface. * Handle MessageEvents from the SMS service */ public void handleMessageEvent(MessageEvent event) { if( event.getType() != MessageEvent.MESSAGE_RECEIVED ) { // We are only interested in incoming messages return; } Message msg = event.getMessage(); if (msg == null || !(msg instanceof MMSNotification)) { // incomming message is a regular SMS... return; } MMSNotification notification = (MMSNotification)msg; System.out.println("MMS notification received:" + "\nSender : "+notification.getFrom()+ "\nSubject : "+notification.getSubject()+ "\nSize : "+notification.getMessageSize()+ "\nTRX-ID : "+notification.getTransactionId()+ "\nLocation: "+notification.getContentLocation()); // Disconnect the GSMService if (service != null){ try { System.out.print("Stopping receiption of SMSes..."); service.stopReceiving(); System.out.println("OK"); System.out.print("Disconnecting from GSM device..."); service.disconnect(); System.out.println("OK"); } catch (MessageException e) { System.err.println("disconnection SmsService failed."); e.printStackTrace(); } finally { service.destroy(); } } try { // Create a service factory first MMSServiceFactory factory = MMSServiceFactory.createFactory(new File(configFile)); // Get the default MMS Service if( serviceName == null ) { mmsService = factory.getDefaultService(); } else { mmsService = factory.getService(serviceName); } // Connect to the MMSC System.out.print("Connecting to MMSC..."); mmsService.connect(); System.out.println("OK"); // Fetch the MMS MMSMessage mms = null; System.out.print("Fetching MMS..."); mms = mmsService.fetch(notification); System.out.println("OK"); // Show incoming message System.out.println("MMS Fetched:\n"+mms.toString()); } catch (Exception e) { System.err.println("could not fetch MMSMessage with id "+notification.getTransactionId()); e.printStackTrace(); } finally { // Disconnect from MMSC if (mmsService!=null) { try { System.out.print("Disconnecting from MMSC..."); mmsService.disconnect(); System.out.println("OK"); } catch (Exception e) { System.err.println("Could not disconnect mmsService after fetching."+e.getMessage()); e.printStackTrace(); } } } // reconnect GSM service for further messages try { startGsmService(); } catch (Exception e) { System.err.println("could not reconnect GsmSmsSerice."); e.printStackTrace(); } } private void startGsmService() throws IOException, MessageException { // Create and initialize a GSM SmsService service = new GsmSmsService(); System.out.print("Initializing GSM device..."); service.init(new File(configFile)); System.out.println("OK"); // Since MMS notifications come in multiple fragments, we engage the // MultiPartReceiver for assembling them. MultiPartReceiver mpr = new MultiPartReceiver(360*1000, 50, this); service.addMessageEventListener(mpr); System.out.print("Connecting to GSM device..."); service.connect(); System.out.println("OK"); System.out.print("Start receiving Messages..."); service.startReceiving(); System.out.println("OK"); } private void shutdown() throws MMSException, ProtocolException, TransportException, IOException { if( service != null ) { service.destroy(); } if( mmsService != null ) { mmsService.disconnect(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -