Search

Thursday, September 11, 2008

How to Read SMS and Launch Windows Mobile Application Automatically ?

How to Launch Application from SMS in Windows Mobile Compact framework .NetCF ? or Intercept SMS and Launch Application ? recently i came across this question and after struggling for a while i finally found the solution below is the sample code which is working and tested on windows mobile 6.0 and it will also work on WM 5.0

using Microsoft.WindowsMobile;
using Microsoft.WindowsMobile.PocketOutlook;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;

namespace LaunchAppFromSMS
{
public partial class Form1 : Form
{
private MessageInterceptor mi;

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
RegisterApplication();
}
private void mnuRegisterApp_Click(object sender, EventArgs e)
{
RegisterApplication();
}
private void SmsInterceptor_MessageReceived_OnThread(object sender, MessageInterceptorEventArgs e)
{
this.BringToFront();
SmsMessage msg = e.Message as SmsMessage;
lblSender.Text = msg.From.Address;
lblBody.Text = "Some Message";

}



private void RegisterApplication()
{
///Wheather the Message Interceptor application launcher with the given identifier has already been enabled
bool appRegistered = MessageInterceptor.IsApplicationLauncherEnabled("fireinhole");
if (appRegistered == true)
{
mi = new MessageInterceptor("fireinhole", true); //Load the existing settings
lblBody.Text = "Application is already registered";
}
else
{
//Setup rule and register with application id "miDemo"
mi = new MessageInterceptor(InterceptionAction.NotifyAndDelete, true);

///Notifies the intercepting application that an intercepting message has arrived. When the application
///has finished processing the message, Pocket Outlook deletes the original message.
mi.MessageCondition = new MessageCondition(MessageProperty.Sender,
MessagePropertyComparisonType.Equal, "+99999", false);

string appPath = Assembly.GetExecutingAssembly().GetName().CodeBase;

//Persist the rule in registry for later use. Also stores the application path so that when a matching SMS
//is received, the system can auromatically start your application and process the message
mi.EnableApplicationLauncher("fireinhole", appPath);
appRegistered = true;
if (appRegistered == true)
lblBody.Text = "Application registered successfully.";
}
mi.MessageReceived += SmsInterceptor_MessageReceived_OnThread;
}

private void button1_Click(object sender, EventArgs e)
{
bool appRegistered = MessageInterceptor.IsApplicationLauncherEnabled("fireinhole");
mi.DisableApplicationLauncher();
mi.MessageReceived -= SmsInterceptor_MessageReceived_OnThread;
mi.Dispose();
if (appRegistered == true)
lblBody.Text = "Application UNregistered successfully.";
else
lblBody.Text = "Application could not be unregistered";
}
}
}

do leave a comment if this code helped you and also you can reach me at feedback[at] ceveni [dot] com

0 comments:

Post a Comment

Other Interesting Articles



Related Article Widget by Yogith

Search Powered by Google