by March 7, 2006 0 comments

Windows Mobile 5.0 lets developers write applications for
PocketPC and Smartphone for a variety of scenarios. One such good feature is SMS
Interception which lets an SMS is received be intercepted by your application
which can then perform some action on the message. In this article, we are going
to discuss this feature and build a real life application around the following
scenario. As a scenario, imagine that you have forgotten your PocketPC or
Smartphone at home. It’s possible that people call you up (only to know that
no one is answering as the phone is unattended) or SMS you. Since the phone is
at home, you do not know the number of missed calls you got (and from whom) or
messages you received. Our sample will let you query your phone for the number
of unread SMS you got, from any cellphone which is capable of sending SMS.

NET Compact Framework developers
Leveraging message interception in Windows Mobile 5.0 for application design
Google keywords:
win mobile sdk 
On PCQLive CD:
pcq_labsmarch\March 2006-SMS Interception in Windows Mobile 5.0

Introducing SMS interception
The SMS interception functionality for Windows Mobile 5.0 is based on the
‘Microsoft.WindowsMobile.PocketOutlook. MessageInterception’ namespace that
is implemented in the ‘Microsoft.WindowsMobile.PocketOutlook’ assembly. To
set up an application, create an object of the type ‘MessageInterceptor’
(declared at the class level so that it lives for the lifetime of the
application) and set up an event handler for its ‘MessageReceived’ event.

interceptor = new


+= new MessageInterceptorEvent Handler(interceptor_MessageReceived);

The Activate button when clicked activates or deactivates SMS interception by the smartphone our application is installed on

This initialization of message interception is done
typically in the Form’s Load event as it is required just once. The
constructor, as you can see, takes an argument that defines what kind of action
the system will take on the intercepted message. After initializing the
interception, the handler is implemented. Interception can be either as Notify
in which case the message is checked and moved to the device’s inbox; or
NotifyAndDelete which would process and delete the message from the device.

interceptor_MessageReceived(object sender,

MessageInterceptorEventArgs e) {    

SmsMessage sms = (SmsMessage)e.Message;


The ‘MessageInterceptorEventArgs’ object contains a
Message property, which can be casted to ‘SmsMessage’ type, to extract the
SMS message contents.That sets up our message interception.

Building QueryDevice
‘QueryDevice’ is our Windows Mobile 5.0 application that can be queried
for a number of unread SMSes from any other phone capable of sending them. We
start off by creating a new Windows Mobile 5.0 Pocket PC device application.
Windows Mobile 5.0 applications can be designed using VS .NET 2005. Once this
IDE has been installed, you can install the Windows Mobile 5.0 SDKs from this
month’s PCQLive! CD. Once you install one of them, you will see the PocketPC
and/or Smartphone templates in VS .NET.

When creating a PocketPC device application, we add a
reference to Microsoft.WindowsMobile and Microsoft.WindowsMobile.PocketOutlook
assemblies and imports the following three namespaces in our code:
Microsoft.WindowsMobile, Microsoft. WindowsMobile.PocketOutlook, and
Microsoft.WindowsMobile. PocketOutlook.MessageInterception. The second and third
namespaces are both implemented in the ‘Microsoft.WindowsMobile. PocketOutlook’
assembly. Next, we switch to the design view and drop a button control on the
form and set its Text property to Activate as shown below. Add a Click event
handler and write the below code to activate message interception or deactivate

if (btnActivate.Text.IndexOf(“Activate”)
!= -1)


   interceptor =
new MessageInterceptor(InterceptionAction.


interceptor.MessageReceived += new

btnActivate.Text = “Deactivate”;


if (btnActivate.Text.IndexOf(“Deactivate”)
!= -1)



   interceptor =

= “Activate”;


Now, we are all set to activate and deactivate message
interception on demand. All we need to do is to implement the event handler to
invoke when messages are intercepted to check what the user wants to know
(unread SMS count, unread e-mail count). To do this, we need to define commands
for our application. We will define three commands: getunreadsms, getunreademail
and getunreadmms to retrieve the number of unread SMS, email and MMS messages
from the device.

To perform one of the three, simply send an SMS to that
phone with the command name (eg: getunreadsms) and the application on the phone
would intercept this SMS, find the number and reply with the result. To do all
this, our application needs to use the the SystemState class that’s defined in
the Microsoft.Windows Mobile.Status namespace. Status assembly and import the
namespace, Microsoft.WindowsMobile.Status. We are all set now to implement our
event handler to process the intercepted SMS and process it.

Inside message interception handler
The message interception handler takes an argument of type
MessageInterceptorEventArgs with a Message property that returns a reference to
the SmsMessage object (intercepted SMS).

Below is the code for the message interception handler that
exemplifies this and also how to process the commands we defined:

interceptor_MessageReceived(object sender,



   SmsMessage sms =

   string strFrom =

strMessageText = sms.Body.Trim().ToLower();

   bool bGotCommand
= false;

strReplyMessage = “”;

   switch (strMessageText)


case “getunreadsms”:

strReplyMessage = “Unread SMS: “+SystemState.MessagingSmsUnread;

bGotCommand = true;


case “getunreademail”:

strReplyMessage = “Unread Email: “+SystemState.MessagingTotalEmailUnread;

bGotCommand = true;


case “getunreadmms”:

strReplyMessage = “Unread MMS: “+SystemState.MessagingMmsUnread;

bGotCommand = true;



   if (bGotCommand
== true)


SmsMessage replySMS = new SmsMessage();

replySMS.To.Add(new Recipient(strFrom));

replySMS.Body = strReplyMessage;




Upon getting the reference to the intercepted SMS, we
extract its message body and also the address of the sender. Next, we set a flag
to indicate if we found any command in the message body to process. We use a
switch-case to check for the presence of any of the defined commands, and if
found, we use the SystemState class to get the

information from the OS.Once out of the switch-case, we
check if we found a command, and if so, we proceed to create a reply SMS to
sender of the command with the data we got. And that’s all there is to
querying your device for information, even if you forgot it at home! Once
activated, it will respond to you no matter where you are! It took us just 35
lines of code to implement it (excluding the commands and braces, but does
include the namespace import statements specific to Windows Mobile 5.0)!

Kumar Gaurav Khanna

No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.