public class MultiPartReceiver extends java.lang.Object implements MessageEventListener
Additionally, the MultiPartReceiver analyzes incoming SMSes and transforms
them into the corresponding SmsMessage subclass, e.g. an incoming Nokia Ringtone
will be converted to a jSMS Ringtone
.
The MultiPartReceiver can be used as a proxy for an existing MessageEventListener
.
Instead of directly registering your Event Listener to a SmsService, register
this listener to the MultiPartReceiver and then register the MultiPartReceiver
itself to the SmsService.
Example:
SmsService myService = new GsmSmsService(); myService.init(myProperties); MessageEventListener myListener = new MessageEventListener() { public void handleMessageEvent(MessageEvent evt) { switch( evt.getType() ) { case MESSAGE_RECEIVED: Message msg = evt.getMessage(); if( msg instanceof EMSMessage ) { // EMS } else if( msg instanceof SmartMessage ) { // Smart Message (Nokia) } break; case MESSAGE_SENT: ... } }); MultiPartReceiver mpReceiver = new MultiPartReceiver(CACHE_TIMEOUT, CACHE_SIZE, myListener); myService.addMessageEventListener(mpReceiver);
The MultiPartReceiver will only handle events of type MESSAGE_RECEIVED
,
all other events are directly passed to the event listener registered with
the MultiPartReceiver.
Incoming SMS that are part of a concatenated (multipart) SMS will be cached in memory until all parts of the multipart SMS have been received or a timeout occurs. A timeout occurs if not all parts forming a multipart message arrive whithin a certain time.
In case of a timeout, the MultiPartReceiver signals this to its registered
event listeners by broadcasting a MULTIPART_FAILURE
event. The Message associated to this event can be safely cast to a MultiPartMessage
holding the incomplete multipart message. It is then up to the application to
decide how to handle the incomplete message.
Constructor and Description |
---|
MultiPartReceiver(long timeout,
int size,
MessageEventListener listener)
Construct a new MultiPartReceiver.
|
Modifier and Type | Method and Description |
---|---|
void |
addMessageEventListener(MessageEventListener listener)
Add a Message Event listener this this MultiPartReceiver.
|
static SmsMessage |
buildMessage(SmsMessage[] messages)
Assemble one or multiple message fragments.
|
void |
destroy()
Shutdown the MultiPartReceiver.
|
void |
handleMessageEvent(MessageEvent event)
Invoked when an event is dispatched from a Message Service
|
void |
removeMessageEventListener(MessageEventListener listener)
Remove a Listener from the list of MessageEvent listeners.
|
public MultiPartReceiver(long timeout, int size, MessageEventListener listener)
timeout
- the maximum time in milliseconds to wait for all parts of a
multipart message to arrive.size
- the maximum number of entries in the message cache.listener
- a listener receiving events.java.lang.IllegalArgumentException
- if timeout is lower 1000 or or size
is lower than than 10.public void destroy()
Make sure that you first de-register
the MultiPartReceiver with the SmsService in use.
public void handleMessageEvent(MessageEvent event)
MessageEventListener
handleMessageEvent
in interface MessageEventListener
public void addMessageEventListener(MessageEventListener listener)
listener
- an Object implementing the MessageEventListener interface.MessageEvent
,
MessageEventListener
public void removeMessageEventListener(MessageEventListener listener)
listener
- the listener to removepublic static SmsMessage buildMessage(SmsMessage[] messages) throws MessageException
SmartMessage
,
EMSMessage
, or SmsMessage
.messages
- an array of messagesMessageException
- if assembling the message fails.object XP, Inc. © 2000-2013. All rights reserved object XP