Microsoft provides us with 'Microsoft.WindowsMobile.PocketOutlook.MessageInterception.MessageInterceptor' class.
MessageInterceptor API Reference here
There are basically two types of Interception.
1. Transient Interception - This is when you application is running. Whenever an SMS gets delivered, you application receives a notification with the SMS details.
2. Persistent Interception - This will send notification of incoming SMS to you application, event if the application is not running. Cool :)
Let's go through step-by-step.
First Create a Message Interceptor
MessageInterceptor interceptor =
new MessageInterceptor(InterceptionAction.NotifyAndDelete);The InterceptionAction provides two options,
1. Notify - Notifies the application, while the SMS lies in the Inbox also.
2. NotifyAndDelete - Notifies the application and deletes the SMS from the Inbox.
Set the Interceptor Condition
Set a condition for the SMS Interceptor. Say, you may want to look for specific formats in the SMS, like, those that start with "CODE:2222", or something like that.
interceptor.MessageCondition = new MessageCondition(
MessageProperty.Body,
MessagePropertyComparisonType.StartsWith, "CODE:2222");For Transient Interception
Just set the interceptor On SMS Received event, with your function.
interceptor.MessageReceived += SmsInterceptor_MessageReceived;If you're done with interception, then remove it.
interceptor.MessageReceived -= SmsInterceptor_MessageReceived;And define the method,
void SmsInterceptor_MessageReceived(object sender, MessageInterceptorEventArgs e) { SmsMessage msg = e.Message as SmsMessage; if (msg != null) { // Process the SMS text message .... } }
For Persistent Interception
Just create an ID for your app, which is used by the Device Application Manager to identify your application.
Set the App Launcher.
string appId = "SMSController";
interceptor.EnableApplicationLauncher(appId);If you don't want any more interception, disable it.
interceptor.DisableApplicationLauncher();if (MessageInterceptor.IsApplicationLauncherEnabled(appId)) { // Persistent message interceptor has been enabled, so // pick up the settings from the registry interceptor = new MessageInterceptor(appId); } else { // A persistent message interceptor is not enabled, so // create a new instance and manually setup the // interceptor interceptor = new MessageInterceptor(...); interceptor.MessageCondition = new MessageCondition(...); }
And its DONE.
No comments:
Post a Comment