Registering the Application with the Device
Now that the application is capable of intercepting all relevant messages, there is one more thing left to do. The application itself may not be always running. For example, the device may be reset for some reason, and when it is rebooted the application would not be loaded unless the nurse launches it specifically. The device will not intercept incoming task messages unless the application is running.
A better method is to register the application with the device's registry so that whenever an incoming message is received (that satisfies the filtering rule), it automatically invokes the application (even when it has not yet been loaded).
To do this, declare an object of type Guid and assigns it a guid value:
public partial class Form1 : Form
{
const string HQPhoneNo = "651234567";
MessageInterceptor msgInterceptor;
Guid appID = new Guid("{283D3327-ADB8-41a0-B9DD-28E2845B5FF3}");
Author's Note: You can create a GUID for development use in Visual Studio 2008 by going to Tools→Create Guid. |
In the Form1_Load event handler, use the static method IsApplicationLauncherEnabled() and check to see whether the application (identified using the appID object) is registered. If it is registered, load the existing MessageInterceptor class using the appID object. If it is not, proceed as normal and register the application using the EnableApplicationLauncher() method, shown in Listing 3.
 | |
Figure 5. Unregister: Adding a menu item control to unregister the application. |
Because the application is automatically registered when it is loaded, it would be good to provide a means to unregister the application. To do so, add a menu item control to the Form (see Figure 5).
To unregister the application, use the DisableApplicationLauncher() method and remove the event handler for the MessageReceived event (as shown in Listing 4).
That's it! From now on, when the application is loaded for the first time, it is automatically registered with the device. Even if the application is unloaded from memory, it will automatically be invoked when an incoming message satisfying the filtering rule is received.
Increasingly Useful
This article has shown you how to send and receive SMS messages using the .NET Compact Framework. Using SMS messaging as a communication medium is gaining popularity as the cellular network gains in coverage and reliability. If you have ideas for using SMS messaging in your Windows Mobile applications, don't hesitate to share your ideas.