February 24, 2008

State and Notifications with Windows Mobile

In last post, we got to know about .NET framework using a real-life example of Hotel Industry. There are so many technologies that help us to make our life much easier and hence we can take real life examples to understand them – inside out. I love to explain some of them as I encounter few of them in my day-to-day life – as a hobbyist programmer, as a student, as an intern and as an individual. So, it will help you to understand those “geeky” worlds easily, even if you are novice.
Here, in this blog roll, I’m going to talk about THE BEST feature that is available in Windows Mobile platform – State and Notification APIs OR you may heed it as SNAPI when you will be surrounded by geeks.
Let’s take an example over here. Let’s suppose you are hungry, and then you may open the refrigerator to check, if anything is there to eat. This is like you are checking the STATE of your refrigerator.
But sometimes, even if you’re not hungry and if your mom is leaving for her job, he may tell you, that, there is ice-cream in the fridge, if you want, take it. In this case, she is NOTIFYING you about it.
There are many such events happen on your Windows Mobile that you may want to know, when those happened, what is the status of your device then, like that. SNAPI helps you to monitor such things.
Let’s take a simple example of Incoming Call. Suppose, you want to receive a Message from your device, whenever there will be an Incoming Call. Ok. When there will be an incoming call, you’ll get notified by ringtone or by vibrations. But, here, just to understand the technology, let’s take this example.
So, there are few steps that you need to follow, when building SNAPI based application.
Here, I’m writing a code, which will give you the idea about how you can build such kind of application.

Imports Microsoft.WindowsMobile
'This namespace is required to run Windows Mobile application.
Imports Microsoft.WindowsMobile.Status
'This namespace is required to use SNAPI classes and functions.
Public Class frmMain
Dim state As SystemState = New SystemState(SystemProperty.PhoneIncomingCall)
'First Create object of SystemState and provide PhoneIncomingCall property as a parameter
'Then create a method, which will be called for evey notification OR state change
Private Sub IncomingCallAlert()
'This method is supposed to be called everytime, when there will be new Incoming Call
If MessageBox.Show("Incoming Call...") = Windows.Forms.DialogResult.OK Then
'If there is an Incoming Call, then just show a Message Box with Alert
Me.Hide()
'After showing, hide the form.
End If
End Sub
'Now launch the State and Notification service, whener application is launched.
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Start the notification process, when main form is loaded
AddHandler state.Changed, AddressOf IncomingCallAlert
'Add event handler, so whenever, there will be incoming call,
'IncomingCallAlert() method will be called.
End Sub
End Class

You can find more information about SNAPI on following links.
Besides the example, you can do much more things with SNAPI. To know more about SNAPI on MSDN,
click here
You can get more information about SNAPI properties like PhoneIncomingCall and others, for this
click here.
There is a webcast on building SNAPI enabled Windows Mobile Applications on MSDN Events. To view this webcast
click here.
There is a very nice article written by Don Sorcinelli, to read,
click here.
I hope, this will give you a clear idea about SNAPI and help you to build nice applications for Windows Mobile.
Happy Coding J