RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Build a 'SNAPI' App that Utilizes Windows Mobile 5.0 Managed APIs : Page 2

The new Managed APIs in Windows Mobile 5.0 enable developers to do more, whether it's interacting with Pocket Outlook, working with the new telephony API, or sending and receiving SMSs. This article will help you get started using WM 5 with a great sample application that uses the State and Notification API to automatically sends an SMS message to callers when you are busy.

Getting Notified of Changes
An incoming call is, basically, a change in state. Therefore you can employ the SystemState class to notify you when a call is coming in. To monitor a particular SystemState you need to define in your code an instance of the SystemState class and write event handling code for changes. To define an instance in your code you need to add the following line of code just after the default constructor of your form.

Friend WithEvents IncomingCall As New _
Here I'm declaring IncomingCall as a new instance of the SystemState class and specifying the property of interest to me, which in this case is PhoneIncomingCall. To handle changes to this SystemProperty (PhoneIncomingCall) you need to write an event handler.

Private Sub CheckCallStatus(ByVal Sender As Object, ByVal Args As ChangeEventArgs) _
Handles IncomingCall.Changed
        MessageBox.Show("Call coming thru :-) ")
End Sub
Try it Out!
Open Visual Studio, choose a project type of Smart Device —>Windows Mobile 5.0 Pocket PC or SmartPhone and choose Device Application as the project template. On the default form that is created, press F7 to start writing code. Add references to the two assemblies, Microsoft.WindowsMobile and the Microsoft.WindowsMobile.Status, to your project and then add the imports statement to the start of your code file.

Imports Microsoft.WindowsMobile
Imports Microsoft.WindowsMobile.Status
Next, enter the code for the IncomingCall declaration followed by the CheckCallStaus method defined above. Once you've completed that, run the application by hitting F5 and choose an actual WM5 device as the target. Your application should load up. Now is the time to make a call to your device to test the application. As soon as an incoming call is detected the code for the call CheckCallStatus will execute, displaying the message "Call coming thru :-)".

Author's Note: Besides your application responding to the call, the default WM5 phone handler will also run, asking you to accept or ignore the call. Choose the ignore option.

You've just written your first application to handle a change in a SystemState(PhoneIncomingCall). But even though our message text displays properly, it's not being delivered anywhere useful. The next step is capturing the phone number of the incoming caller. Declare a variable to store a phone number at your form class level; you can add this statement immediately after the IncomingCall declaration:

    Dim PhoneNumber As String
Next, use SystemState in the event handler you just wrote (CheckCallStatus) to retrieve the incoming caller phone number.

PhoneNumber = SystemState.GetValue(SystemProperty.PhoneIncomingCallerNumber)
Add the above line of code as the first line of the method (CheckCallStatus) and edit the text displayed in the MessageBox as shown below:

MessageBox.Show("Incoming Call No: " & PhoneNumber) 
You are now able to detect and respond to an incoming call and store the caller phone number. At this stage it's best to handle situations where errors could creep up. Replace the code in the CheckCallStatus with the following:

If Not _
        String.IsNullOrEmpty(SystemState.GetValue(SystemProperty.PhoneIncomingCallerNumber)) Then

            PhoneNumber = SystemState.GetValue(SystemProperty.PhoneIncomingCallerNumber)
            ' MessageBox.Show("Incoming Call No: " & PhoneNumber)
End If
This new code does two things. First it ensures that only valid phone numbers are stored by checking to see if the value of the incoming phone number is Null or empty. Second, it ensures that the form is brought to the front, in case the user has minimized the application. For now you can see that I've commented out the display of the message box, as you are about to learn how to send that same message via SMS instead.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date